Scourge
Scourge

Reputation: 15

JSON writer and indented output

I apologize if the title is not exactly as clear as it could be as I am not even exactly sure how to properly phrase the question, English isn't my first language and I am rather new to Programming with json.

I have a Json writer that takes various costum inputs and then adds certain Data from Textfields or Comboboxes.

JsonSerializer serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Ignore;

using (StreamWriter sw = new StreamWriter(saveFileDialog.FileName))
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
writer.WritePropertyName("Category");
writer.WriteValue(WeaponCategoryComboBox.SelectedItem.ToString());
writer.WritePropertyName("Type");
writer.WriteValue(WeaponTypeComboBox.SelectedItem.ToString());
writer.WritePropertyName("WeaponSubType");
writer.WriteValue(WeaponSubTypeComboBox.SelectedItem.ToString());

This for example gives me an output like

 "Category": "Missile",
 "Type": "LRM",
 "WeaponSubType": "LRM20",

That works fine so, however, there comes a part in the output format that I am trying to replicated where all of the code is indented and in an additional {} bracket, like

"WeaponEffectID" : "WeaponEffect-Weapon_AC20_Single", //Still the normal code I can replicate)
"Description" : {
"Cost" : 180000,
"Rarity" : 0}

My issue lies with the "Description" as I am unable to think of a way how to put my code in those additional {} or even put in the "Description" : and then leave the second part behind it empty without having it closed with a ",".

Upvotes: 1

Views: 1252

Answers (1)

Hans Kesting
Hans Kesting

Reputation: 39329

The value of that "Description" is not "a string with braces" but "an object with two properties". So you need to start another object:

writer.WritePropertyName("WeaponSubType");
writer.WriteValue("LRM20");
writer.WritePropertyName("Description");
writer.WriteStartObject();
writer.WritePropertyName("Cost");
writer.WriteValue(180000);
writer.WritePropertyName("Rarity");
writer.WriteValue(0);
writer.WriteEndObject();

This will create the JSON syntax you want:

"WeaponSubType": "LRM20",
"Description": {
  "Cost": 180000,
  "Rarity": 0
}

Note that you probably need to add another {} pair around this code to create valid JSON. Just use WriteStartObject() as your first line and WriteEndObject() as last line.

EDIT
Or, if I may suggest another approach:

// using Newtonsoft.Json.Linq
var json = new JObject();
json.Add("Category", "Missile");
json.Add("Type", "LRM");
json.Add("WeaponSubType", "LRM20");
var desc = new JObject();
desc.Add("Cost", 180000);
desc.Add("Rarity", 0);
json.Add("Description", desc);

var jsonstring = json.ToString(Newtonsoft.Json.Formatting.Indented);

and now you can write that jsonstring to a file.

Of course use your inputs instead of my hardcoded values.

And if you want to get fancy, you can get the exact same result with this:

// using Newtonsoft.Json.Linq
var json = new JObject(
    new JProperty("Category", "Missile"),
    new JProperty("Type", "LRM"),
    new JProperty("WeaponSubType", "LRM20"),
    new JProperty("Description",
        new JObject(
            new JProperty("Cost", 180000),
            new JProperty("Rarity", 0)
            )
        )
    );

Upvotes: 2

Related Questions