I am using Json.Net for .Net 4.5 and when using populate object on the following object it increments the List's with the content of the json rather than setting its value.
Json.Net
JsonConvert.PopulateObject(string, object)
Class
class MySettingSubClass
{
public List<string> MyStringList1 = new List<string>(){"one", "two", "three"}
}
class MySetting
{
public string MyString = "MyString";
public int MyInt = 5;
public MySettingSubClass MyClassObject = new MySettingSubClass();
public List<string> MyStringList2 = new List<string>{"one", "two", "three"};
}
When they initially load, all is correct, however reloading from JSON both MyStringLists are duplicated "one", "two", "three", "one", "two", "three"
You should tell Json.Net to replace the arrays, like this:
var serializerSettings = new JsonSerializerSettings {ObjectCreationHandling = ObjectCreationHandling.Replace};
JsonConvert.PopulateObject(jasonString, myObject, serializerSettings)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With