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.