Incorrect serialization in C # using JSON.NET

I am trying to take an object structured this way

public class Item { public Guid SourceTypeID {get;set;} public Guid BrokerID {get;set;} public double Latitude {get;set;} public double Longitude {get;set;} public DateTime TimeStamp {get;set;} public object Payload {get;set;} } 

and serialize it using JSON.NET using a call like:

 Item expected = new Item() { SourceTypeID = Guid.NewGuid(), BrokerID = Guid.NewGuid(), Latitude = 33.657145, Longitude = -117.766684, TimeStamp = DateTime.Now, Payload = new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } }; string jsonString = JsonConvert.SerializeObject(expected); 

The payload member of the Item object can potentially contain any primitive from the list of C # primitives (plus several others, such as Guid) or an array of these types (as in the example, an array of bytes), or a "flat" object consisting of any of the previously listed "primitives" (dynamically created).

When I make a call to SerializeObject (), the line that is created contains:

 {"Payload":"AAECAwQFBgcICQ==","SourceTypeID":"d8220a4b-75b1-4b7a-8112-b7bdae956a45", "BrokerID":"951663c4-924e-4c86-a57a-7ed737501dbd", "TimeStamp":"\/Date(1328202421559-0600)\/", "Latitude":33.657145,"Longitude":-117.766684} 

However, when I make a deserialization call, the created element is partially incorrect

 Item actual = JsonConvert.DeserializeObject<Item>(jsonString); actual.SourceTypeID : {00000000-0000-0000-0000-000000000000} actual.BrokerID : {951663c4-924e-4c86-a57a-7ed737501dbd} actual.Latitude : 33.657145; actual.Longitude : -117.766684; actual.TimeStamp : {2/2/2012 11:07:01 AM} actual.Payload : null 

The SourceTypeID element (Guid) and the payload element (object containing bytes []) are invalid. The serialized string seems to contain the correct identifier for guid, but not for the byte array.

I see an alternate signature of a SerializeObject

 SerializeObject(object value, params JsonConverter[] converters); 

Is this the right way to inform the de / serialization engine about types that seem to be processing incorrectly? Since I work with a limited set of "primitives" at the heart of my object, if I could create a set of converters for each of these types, would this solve my problem?

I want to avoid the possibility of reinventing the wheel, if possible, as it seems that this will be a problem that has already been handled gracefully, and I would like to use something like that, if available.

+6
source share
2 answers

This seems like a real bug in JSON.NET.

This is not an idea, but a two-step serialization might be a workaround. Firstly, an object that is actually serialized to / from JSON will consist only of fields / properties of type string . The second object (the one that your server code will use) will be the object that will be converted to strong types manually.

Not perfect, but possible. Alternatively, you might consider using DataContractJsonSerializer , which does a pretty good job handling arrays of bytes and IME commands. The dates are still pretty sucking, but basically this is a Javascript error.

+1
source

The payload will be deserialized as a string unless you place the [JsonProperty] attribute with TypeNameHandling enabled, otherwise the deserializer will not know what to deserialize it as.

I could not duplicate the problem with which you had some properties with zero use of the latest source code at http://json.codeplex.com/SourceControl/list/changesets

+4
source

Source: https://habr.com/ru/post/907503/


All Articles