1

Suppose you have the following class:

public class Class1
{
  public int a;
}

Then, you serialize the class and save it on your database as JSON. Then, you convert the JSON format back into a Class1 instance.

However Class1 is subject to change: meaning the properties could be named differently or having new ones added to my class.

Example of modified Class1:

public class Class1 
{
  public int a;
  public int b;
}

What I'd like to do is keep all of the existing properties I grab from my JSON, while also initializing the new ones.

Keep in mind that Class1 is just an example, and the actual class is going to have many nested objects.

I thought about creating a new instance, but I'm not too sure how to update every property (because some of them are going to be nested, and among them, only some have actually been modified).

Chris Pickford
  • 8,642
  • 5
  • 42
  • 73
Go Fast
  • 23
  • 5
  • "while also initializing the new ones" --> constructor? The constructor will run first, you initialize all properties, and then the json is deserialized into the properties that match. In this case you would have a chance to set `b` to a value first, as well as `a` if you need to, and then `a` would be given a new value coming from the json. – Lasse V. Karlsen May 21 '21 at 08:43
  • 1
    If instead you want to have an existing instance and "deserialize **into** it", then I'm not sure that is supported by any of the json libraries, and nothing inside .NET will easily allow it. – Lasse V. Karlsen May 21 '21 at 08:44
  • Just to reinforce what @LasseV.Karlsen is saying, most JSON libraries **create** new object references, they don't normally de serialize *into* existing references. This is because they construct the object on the fly instead of just assigning the properties of an existing object. – DekuDesu May 21 '21 at 08:46
  • @Lasse V. Karlsen Woudn't the values stores in the JSON be overidden by the default values when I initialize them? What guarantees they won't? Edit: On a second thought that's a dumb question. I just wasn't aware the deserialization from JSON automatically matches the values. Thank you! :) – Go Fast May 21 '21 at 08:50
  • Does this question help? - https://stackoverflow.com/q/56835040/1048425 – GarethD May 21 '21 at 08:56
  • They will be given a new value *if they exist in the json*. For instance if your constructor initialize both the `a` and `b` properties, and the json only contains a value for `a`, only the `a` property on the object will be given a new value, the `b` property will not be touched. – Lasse V. Karlsen May 21 '21 at 09:00
  • However, if you want to update an existing object instance with new data from json, and you're using Newtonsoft.Json, you can use `JsonConvert.PopulateObject(json, objectInstance)` – Lasse V. Karlsen May 21 '21 at 09:02
  • There is not a single answer that will help you, however, since it depends on what **exactly** you want to accomplish. – Lasse V. Karlsen May 21 '21 at 09:03
  • 2
    You also say "the properties can be named differently", this requires a completely different solution. You will probably need to deal with versioning in this case, having multiple versions of the type available, deserialize into the right one for the version of json that you have, and then map from the old instance to a new one. This is why I say that a single answer will not help you, your scope is a bit unclear, vague, and big. – Lasse V. Karlsen May 21 '21 at 09:09
  • Ok, to be perfectly clear, my main requirements is to add new properties to an existing instance that's deserialized. I'll figure out the part of renaming properties later. You are right, it's too convoluted, sorry. – Go Fast May 21 '21 at 09:17

1 Answers1

0

Hi you can do it with initializers:

public class Class1 
{
  public int a { get; set; } = 10;
  public Class2 b { get; set; } = new Class2();
}

You can't (at least with JsonUtility) serialize properties. Only fields.

Go Fast
  • 23
  • 5