byte[] encoding in base64,sbyte[] does not.
byte[] bs = {100,101};
Newtonsoft.Json.JsonConvert.SerializeObject(bs);//"ZGU="
sbyte[] sbs = {100,101};
Newtonsoft.Json.JsonConvert.SerializeObject(sbs);//"[100,101]"
To serialize a collection - a generic list, array, dictionary, or your own custom collection - simply call the serializer with the object you want to get JSON for. Json.NET will serialize the collection and all of the values it contains.
Despite being deprecated by Microsoft in . NET Core 3.0, the wildly popular Newtonsoft. Json JSON serializer still rules the roost in the NuGet package manager system for . NET developers.
DeserializeObject Method. Deserializes the JSON to a . NET object.
Specifies the settings on a JsonSerializer object. Newtonsoft.Json.
It's just the way JSON.Net works, and I would suspect most serialisers do the same. Check the documentation which explicitly says that byte[]
is serialised as a base64 encoded string.
All other arrays are treated as you would expect, as simple JSON array types where the elements are serialised according to the rules for the array's type. Meaning that sbyte[]
will be serialised as an array of integers.
The serialization guide in the documentation for JSON.Net shows that there is no serialization rule for sbyte[]
array. There is, however, an entry for sbyte
. sbyte
will be serialized as int
.
byte[]
arrays will be serialized to a base64
string since it is explicitly defined in the guide, and as a convenience.
Since there is no rule for sbyte[]
in the serialization guide the array will be treated like any other array, and it's members will be serialized according to the guide. Therefore, sbyte[]
is serialised to int[]
since sbyte
serilaizes to int
.
Please see image below:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With