neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.36k stars 267 forks source link

Can not deserialize class having property as byte[] #229

Open MittalNitish opened 4 years ago

MittalNitish commented 4 years ago

Using version: 1.3.7

When I am trying to deserialize the following JSON : {"Body":[123,34,72,105,115,116,111,114,121,83,97,109,112],"Properties":{"key":"0bf5f58a-1d20-415c-8b1d-21cb801dbe40"}} to following type:

public class Custom
{
    public byte[] Body { get; set; }
    public Dictionary<string, object> Properties { get; set; }
}

it fails with error:

Utf8Json.JsonParsingException: expected:'String Begin Token', actual:'[', at offset:8
   at Utf8Json.JsonReader.ReadStringSegmentCore(Byte[]& resultBytes, Int32& resultOffset, Int32& resultLength)
   at Utf8Json.JsonReader.ReadString()
   at Utf8Json.Formatters.ByteArrayFormatter.Deserialize(JsonReader& reader, IJsonFormatterResolver formatterResolver)
   at Utf8Json.Formatters.utf8tests_CustomFormatter1.Deserialize(JsonReader& , IJsonFormatterResolver )
   at Utf8Json.JsonSerializer.Deserialize[T](Byte[] bytes, Int32 offset, IJsonFormatterResolver resolver)
   at Utf8Json.JsonSerializer.Deserialize[T](Byte[] bytes, IJsonFormatterResolver resolver)
   at Utf8Json.JsonSerializer.Deserialize[T](String json, IJsonFormatterResolver resolver)
   at Utf8Json.JsonSerializer.Deserialize[T](String json)
   at utf8tests.Program.Main(String[] args) in C:\HelperPrograms\utf8tests\utf8tests\Program.cs:line 21

However, it works if I change the the type to IList, ex:

public class Custom
{
    public IList<byte> Body { get; set; }
    public Dictionary<string, object> Properties { get; set; }
}

We do not want to use IList for the Body property. Is there a way we can make it work using array itself?

Attaching sample to reproduce issue utf8tests.zip .