-1

Let's say at first I have a record with only one parameter "int no". Later, as the program evolves, new param with a default value is added resulting:

record SimpleRecord(int no, string paramStr = "New param that did not existed");

now parsing previously saved json with two different libs:

 string jsonText = "{\"no\":10}";
 SimpleRecord? parsedJson1 = System.Text.Json.JsonSerializer.Deserialize<SimpleRecord>(jsonText);
 SimpleRecord? parsedJson2 = Newtonsoft.Json.JsonConvert.DeserializeObject<SimpleRecord>(jsonText);

I would expect parsed record as System.Text.Json -> {SimpleRecord { no = 10, paramStr = New param that did not existed }} Newton parsed record is {SimpleRecord { no = 10, paramStr = }}

paramStr is null. How to achieve the same result as System.Text.Json with Newton library? (I have existing project with Newton so trying to avoid rewriting).

2 Answers2

0

So there's a few ways to approach it, first you can add a[DefaultValue(" ")] attribute to you're parameter (for more info see documentation). ex:

[DefaultValue(" ")]
public string FullName
{
    get { return FirstName + " " + LastName; }
}

Second option would be to define a default value in you're class like this (if you are using c# 6 or above). ex:

public string c { get; set; } = "foo";

For more examples you could look at this answer

Dmyto Holota
  • 356
  • 1
  • 4
  • 14
  • 1
    The question asks about records, a C# 9 feature. The primary feature of records is immutability. Besides, the property *already* has a default value. `string paramStr = "New param that did not existed"` is equivalent to `public string paramStr {get;init;}="blah....";` – Panagiotis Kanavos Jul 19 '22 at 08:58
0

I prefer to add a special constructor for Newtonsoft.Json

record  record SimpleRecord(int no, string paramStr = "New param that did not existed")
{
    [Newtonsoft.Json.JsonConstructor]
    public  SimpleRecord(int no, string paramStr, string msg="JsonConstructor")
                                                                    :this(no, paramStr)
   {
      if (string.IsNullOrEmpty(paramStr)) this.paramStr = "New param that did not existed";
    }
}

the third parameter is just a fake to cheat a compiller

Serge
  • 40,935
  • 4
  • 18
  • 45
  • This only will work with nullable values/objects. When a new member will be not nullable like int, there always will be value which is 0. In this case, there be no way to distinguish if it parsed 0 from a file, or no value is initialized. – Audrius Gailius Jul 20 '22 at 07:35
  • 1
    @AudriusGailius You are wrong , it has nothing to do with nullables, it works with default values the same way as usuall, only manually. What about default record constructor if default will int? But for example int? is a nullable one, Tell me what class is not nullable? Or show the json that will not work correctly. – Serge Jul 20 '22 at 10:38
  • You are right. I was wrong. Probably there is no other way around it. Just to create a new constructor. Of course, it will result in much more code for the Records. – Audrius Gailius Jul 27 '22 at 06:51