0

I'm trying to understand why JSON.Net will modify the original JObject (formSchema) in two cases below where I'm adding JArray and JObject to objects I've defined as vars.

I assume the two vars I created were standalone objects in their own right but it appears they are not.

In the first case, requiredItems is a JArray var, in which I want to add the token "certification" to formSchema, but I've placed the Add method on requiredItems. Not formSchema.

In the second case, I add certifyProperty to formSchema using the Add method on consentProps. The property handily gets added to formSchema without a direct reference to it.

Why is this? Are they linked in memory? Where in the JSON.Net docs is this explained? I cannot find it.

namespace JSONProps
{
  class Program
  {
    static void Main(string[] args)
    {
      string jsonSchema = @"
            {
              ""jsonSchema"": {
                ""type"": ""object"",
                ""title"": ""a title"",
                ""properties"": {
                  ""consent"": {
                    ""type"": ""object"",
                    ""title"": ""Consent of Applicant"",
                    ""required"": [
                      ""applicantConsent""
                    ],
                    ""properties"": {
                      ""applicantConsent"": {
                        ""type"": ""boolean"",
                        ""title"": ""I give my Consent"",
                      },
                    }
                  }
                }
            }
          }
        ";

      // First case
      var formSchema = JObject.Parse(jsonSchema);
      var requiredProps = formSchema["jsonSchema"]["properties"]["consent"]["required"] as JArray;
      requiredProps.Add("certification");

      // Second case
      var consentProps = formSchema["jsonSchema"]["properties"]["consent"]["properties"] as JObject;
      var certifyProperty = JObject.Parse(@" { ""type"" : ""boolean"", ""title"" : ""This is true.""  } ");
      consentProps.Add("certification", certifyProperty);
      Console.WriteLine(formSchema.ToString());
    }
  }
}
$ dotnet run
{
  "jsonSchema": {
    "type": "object",
    "title": "a title",
    "properties": {
      "consent": {
        "type": "object",
        "title": "Consent of Applicant",
        "required": [
          "applicantConsent",
          "certification"
        ],
        "properties": {
          "applicantConsent": {
            "type": "boolean",
            "title": "I give my Consent"
          },
          "certification": {
            "type": "boolean",
            "title": "This is true."
          }
        }
      }
    }
  }
}
dfdumaresq
  • 1,618
  • 4
  • 17
  • 22
  • 1
    `Why does JSON.Net modify the original JObject?` as opposed to? I don't understand what you are asking here, possibly because you haven't said what you were expecting instead. Why wouldn't it modify the original object? (I'm also not sure what you mean by 'original') – Tom W Oct 01 '19 at 18:40
  • 1
    If it did not, then modification would be horribly complicated. `requiredProps` is a reference to an object that is itself referenced up through a hierarchy to `formSchema`. Thus any changes to it are reflected in the top level object. This is no different from making changes to nodes in a tree or linked list. – crashmstr Oct 01 '19 at 18:45
  • 1
    Thinking about it a bit more I'm wondering whether you expected creating variables to take copies. C# reference types don't work like that, and C# doesn't have [copy constructors](https://en.cppreference.com/w/cpp/language/copy_constructor) – Tom W Oct 01 '19 at 18:47
  • 2
    Json.Net did **not** modify the original JObject - it is **your** code that did the modification => here requiredProps.Add(...) and here consentProps.Add(...) – Sir Rufo Oct 01 '19 at 18:59
  • 1
    I am also confused about what you expect should happen, but this is possibly related: [nested json objects dont update / inherit using Json.NET](https://stackoverflow.com/a/29260565/3744182). – dbc Oct 01 '19 at 19:36
  • @crashstr -- Thanks for the clarification and the hint these are nodes in a tree or linked list. – dfdumaresq Oct 01 '19 at 21:07
  • @Tom W. I did expect copies. – dfdumaresq Oct 01 '19 at 21:10
  • @dbc -- thanks for the helpful link! – dfdumaresq Oct 01 '19 at 21:10

0 Answers0