Skip to content

How can I determine if byte array contains serialized data #208

@Jericho

Description

@Jericho

I want to write a method that will return a Boolean indicating if a given byte array contains data that was produced by LZ4MessagePackSerializer.Typeless.Serialize.

Is there some sort of "header" in the resulting byte array that I could check? If not, can you suggest some other way?

Here's my first attempt but unfortunately it doesn't produce the expected result:

private static bool ContainsSerializedData(byte[] content)
{
    return MessagePackBinary.GetMessagePackType(content, 0) == MessagePackType.Extension;
}

static void Main()
{
    var content = LZ4MessagePackSerializer.Typeless.Serialize(new MyMessage { });
    Console.WriteLine("Byte array contains serialized data: {0}", ContainsSerializedData(content));

    content = LZ4MessagePackSerializer.Typeless.Serialize(123);
    Console.WriteLine("Byte array contains serialized data: {0}", ContainsSerializedData(content));

    content = LZ4MessagePackSerializer.Typeless.Serialize(new DateTime(2018, 2, 11));
    Console.WriteLine("Byte array contains serialized data: {0}", ContainsSerializedData(content));

    content = LZ4MessagePackSerializer.Typeless.Serialize("Hello World");
    Console.WriteLine("Byte array contains serialized data: {0}", ContainsSerializedData(content));

    content = Encoding.UTF8.GetBytes("Hello World");
    Console.WriteLine("Byte array contains serialized data: {0}", ContainsSerializedData(content));
}

The expected result is true for the first four scenarios and false for the fifth scenario but the result is:

Byte array contains serialized data: True
Byte array contains serialized data: False
Byte array contains serialized data: True
Byte array contains serialized data: False
Byte array contains serialized data: False

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions