0

While messing around with a class which I'd like to serialise into a JSON, I encountered a situation which I'm not sure how to best resolve. It has to do with a JSON structure that has both fixed and random attributes (Fixed: always the same attribute name, Random: their names are random). Also, the fixed attributes are always of type string and the random attributes are always of type MyJsonClass2.

For example:

{
 "fixedName1": "value1",
 "fixedName2": "value2",
 "randomName1": { ... },
 "randomNameN": { ... }
}

This can be represented using a Dictionary<string, object>. However because the fixedName1 and fixedName2 are always given, I'd like to have them as fixed declarations and only have the variables in a dictionary as follows:

class MyJson
{
    public string fixedName1 = "blah1";
    public string fixedName2 = "blah2";

    public Dictionary<string, MyJsonClass2> randomNames = new Dictionary<string, MyJsonClass2>();
}

The problem here is that when this is serializsed, the values from randomNames are enclosed in an attribute called randomNames, which makes sense, however this is not what I'd like to see. Is there are way to tell the Newtonsoft JSON serializer to ignore the dictionary name? Or what is the best way to define such a class?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Eric
  • 1,321
  • 2
  • 15
  • 30
  • You could probably create a `JsonConverter`. Not sure if there's a better option. – ProgrammingLlama Dec 05 '18 at 06:32
  • You could implement a custom JsonConverter. However, what you describe appears a little odd. Why do you want this? – Jonathan Wilson Dec 05 '18 at 06:33
  • `JsonTypedExtensionDataAttribute` and `TypedExtensionDataConverter` from [this answer](https://stackoverflow.com/a/40094403/3744182) to [How to deserialize a child object with dynamic (numeric) key names?](https://stackoverflow.com/q/40088941/3744182) might be what you need. Agree? – dbc Dec 05 '18 at 07:03

1 Answers1

1

In your json some of your key/value data is fixed and some of key/value pair data is random or simply say that its dynamic.

Then you can declare properties for fixed key/value pair. but problem occurred when its dynamic and question arises that you asked.

So Newtonsoft.json has one feature that to be use here and that is [JsonExtensionData]. Read more

Extension data is now written when an object is serialized. Reading and writing extension data makes it possible to automatically round-trip all JSON without adding every property to the .NET type you’re deserializing to. Only declare the properties you’re interested in and let extension data do the rest.

Now you can add properties for fixed key/value paired data like this.

public string fixedName1 {get;set;}
public string fixedName2 {get;set;}

And for dynamic or random key/value pair you need to decorate your Dictionary property with [JsonExtensionData]. like

[JsonExtensionData]
public Dictionary<string, JToken> randomNames {get;set;}

So your class will be look like

class MyJson
{
    public string fixedName1 {get;set;}
    public string fixedName2 {get;set;}

    [JsonExtensionData]
    public Dictionary<string, JToken> randomNames {get;set;}
}

Now try to serialize your data with above class you will never get Dictionary name in your output serialized json.

You can try above class like

MyJson myJson = new MyJson();
myJson.fixedName1 = "ABC";
myJson.fixedName2 = "PQR";
myJson.randomNames = new Dictionary<string, JToken>();
myJson.randomNames.Add("randomName1", JToken.FromObject(new MyJsonClass2 { Name = "QWERTY" }));
myJson.randomNames.Add("randomNameN", JToken.FromObject(new MyJsonClass2 { Name = "ZXCVBNM" }));

string json = JsonConvert.SerializeObject(myJson);

Output:

enter image description here

er-sho
  • 9,581
  • 2
  • 13
  • 26
  • Thank you for the solution. I wanted to test it completely before I ticked your answer, but then I got side-tracked with some other work ... but I've checked it now, the output now looks great :) – Eric Jan 21 '19 at 07:26
  • Glad to hear, and Welcome :) – er-sho Jan 21 '19 at 07:28