11

I ma using Newtonsofts' Json.Net to serialize some and array of objects to json. The objects have a common set of properties but also have Meta property which is a dictionary

During serialization I want the key value pairs to be added to my json object as if they where root level properties, like this...

 {
    id: 1,
    name:'jeff',
    food:'spinch',
    spoon: 'ýes'
 }  

Not like this:

 {
    id: 1,
    name:'jeff',
    meta:{
       food:'spinch',
       spoon: 'ýes'
    }
 } 

I have dug through JsonSerializerSettings but cant seem to spot where I can jump in and override???

David
  • 8,340
  • 7
  • 49
  • 71
  • You can have a look at this possible duplicate: http://stackoverflow.com/questions/4557220/convert-dictionary-to-listkeyvaluepair – Bhushan Firake Nov 28 '12 at 20:21

3 Answers3

4

You can do this by creating your own JsonConverter and then adding an attribute to the class you want to serialize [JsonConverter(typeof(MyConverter))]

Example here - http://www.lostechies.com/blogs/rhouston/archive/2008/02/25/a-custom-converter-for-json-net.aspx

Brandon Cuff
  • 1,438
  • 9
  • 24
Stuart
  • 66,722
  • 7
  • 114
  • 165
  • 1
    Or simply use the KeyValuePairConverter http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_Converters_KeyValuePairConverter.htm – ba__friend Mar 23 '11 at 11:20
  • 1
    Can you expand on that bassfriend - how do you use that in a class? Would you inherit from it and pass in all the class properties plus the dictionary? – Stuart Mar 23 '11 at 11:52
  • http://stackoverflow.com/questions/5124889/serialize-net-dictionarystring-string-into-json-key-value-pair-object – ba__friend Mar 23 '11 at 12:36
  • 1
    but to merge the class properties at the same level as the keys&values as the op has asked you'll have to do something more, won't you? – Stuart Mar 23 '11 at 13:01
2

If your dictionary is a string to object dictionary could can simply use the [JsonExtensionData] attribute:

[JsonExtensionData]
public Dictionary<string, object> Meta { get; set; }

See How to serialize a Dictionary as part of its parent object using Json.Net.

Community
  • 1
  • 1
Dejan
  • 9,150
  • 8
  • 69
  • 117
0

You could use the .Net DataContractJsonSerializer.

For custom serialization, see:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.idatacontractsurrogate.aspx

One advantage of using IDataContractSurrogate (compared to simply adding properties to your class for serialization) is that you don't have to mix actual properties and serialization properties together in the same class.

Another advantage (compared to having to do custom serialization against a property bag, ala KeyValuePairConverter) is that you only have to add attributes to properties on your classes (the actual type and the surrogate type) and you can write all your conversion/custom serialization code directly against those types. This keeps your code higher level, and lets the framework deal with the exact transport mechanism.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183