SamKr
10/26/2018 - 7:17 AM

Json Functions

Various JSON snippits

// to declare a serializable property:
[JsonProperty(PropertyName = "ListOfStrings")]
internal List<string> ListOfStrings { get; set; } = new List<string>();

// to serialize an object:
string jsonStrings = JsonConvert.SerializeObject(ListOfStrings);

// to serialize and write to file:
File.WriteAllText(jsonFile, JsonConvert.SerializeObject(ListOfStrings));

// to deserialize an object:
ListOfStrings = JsonConvert.DeserializeObject<List<string>>(jsonStrings);

// to deserialize from file:
ListOfStrings = JsonConvert.DeserializeObject<Config>(File.ReadAllText(jsonFile));