Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate spurious ToString calls during deserialization #873

Conversation

logiclrd
Copy link

@logiclrd logiclrd commented Sep 7, 2016

I noticed some time ago that the ConvertValue method in JsonDeserializer always .ToString()s the object before checking what kind of deserialization must be performed. Most of the types of deserialization do require the string, but the most frequent types of deserialization will not. This becomes pathological in the case of a deep, complex, typed object structure, because each layer gets passed through ConvertValue separately. In this PR, I've reworked the ConvertValue method to avoid the ToString call unless it is strictly required. I added a custom-built Lazy-type class, since as I understand it Lazy<T> does not exist in .NET 3.5. I also added a unit test that ensures the offending ToString calls are not made. (For what it's worth, the ToStringCounter value on the original ConvertValue code has value 121 at the end of the test!)

…ializer.cs to avoid calling .ToString() on the supplied value unless absolutely necessary. Also eliminated repeated calls to type.GetTypeInfo() on UWP, and simplified the code structure, collapsing away repeated #if !WINDOWS_UWP blocks.

Added test Does_Not_ToString_Complex_Objects_During_Deserialization to RestSharp.Tests/JsonTests.cs. Made the JsonDeserializer.ConvertValue method internal so that it can be accessed by the test.
@logiclrd
Copy link
Author

logiclrd commented Sep 8, 2016

Just to clarify that parenthesized last sentence in the opening comment on the PR:

For what it's worth, the ToStringCounter value on the original ConvertValue code has value 121 at the end of the test!

If you have a data structure like this:

public class Nested
{
  public Nested A { get; set; }
  public Nested B { get; set; }
  public Nested C { get; set; }
  public string Value { get; set; }
}

...and you make a call to a service endpoint, and it returns the following JSON:

{
  "A"
  {
    "A":
    {
      "A": { "Value": 0 },
      "B": { "Value": 1 },
      "C": { "Value": 2 },
    },
    "B":
    {
      "A": { "Value": 3 },
      "B": { "Value": 4 },
      "C": { "Value": 5 },
    },
    "C":
    {
      /* (and so on) */
    }
  },
  "B":
  {
    /* (following the same pattern) */
  },
  "C":
  {
    /* (and again) */
  }
}

Then in the course of deserializing the request for the Data field of a RestResponse<Nested>, JsonDeserializer will first deserialize this into a JsonObject graph, and then will call .ToString() on every JsonObject in the graph. The following strings will be created on the heap (and then not used for anything):

  • {"A":{"A":{"A":{"A":{"A":null,"B":null,"C":null,"Value":0},"B":{"A":null,"B":null,"C":null,"Value":1},"C":{"A":null,"B":null,"C":null,"Value":2},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":3},"B":{"A":null,"B":null,"C":null,"Value":4},"C":{"A":null,"B":null,"C":null,"Value":5},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":6},"B":{"A":null,"B":null,"C":null,"Value":7},"C":{"A":null,"B":null,"C":null,"Value":8},"Value":-1},"Value":-1},"B":{"A":{"A":{"A":null,"B":null,"C":null,"Value":9},"B":{"A":null,"B":null,"C":null,"Value":10},"C":{"A":null,"B":null,"C":null,"Value":11},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":12},"B":{"A":null,"B":null,"C":null,"Value":13},"C":{"A":null,"B":null,"C":null,"Value":14},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":15},"B":{"A":null,"B":null,"C":null,"Value":16},"C":{"A":null,"B":null,"C":null,"Value":17},"Value":-1},"Value":-1},"C":{"A":{"A":{"A":null,"B":null,"C":null,"Value":18},"B":{"A":null,"B":null,"C":null,"Value":19},"C":{"A":null,"B":null,"C":null,"Value":20},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":21},"B":{"A":null,"B":null,"C":null,"Value":22},"C":{"A":null,"B":null,"C":null,"Value":23},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":24},"B":{"A":null,"B":null,"C":null,"Value":25},"C":{"A":null,"B":null,"C":null,"Value":26},"Value":-1},"Value":-1},"Value":-1},"B":{"A":{"A":{"A":{"A":null,"B":null,"C":null,"Value":27},"B":{"A":null,"B":null,"C":null,"Value":28},"C":{"A":null,"B":null,"C":null,"Value":29},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":30},"B":{"A":null,"B":null,"C":null,"Value":31},"C":{"A":null,"B":null,"C":null,"Value":32},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":33},"B":{"A":null,"B":null,"C":null,"Value":34},"C":{"A":null,"B":null,"C":null,"Value":35},"Value":-1},"Value":-1},"B":{"A":{"A":{"A":null,"B":null,"C":null,"Value":36},"B":{"A":null,"B":null,"C":null,"Value":37},"C":{"A":null,"B":null,"C":null,"Value":38},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":39},"B":{"A":null,"B":null,"C":null,"Value":40},"C":{"A":null,"B":null,"C":null,"Value":41},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":42},"B":{"A":null,"B":null,"C":null,"Value":43},"C":{"A":null,"B":null,"C":null,"Value":44},"Value":-1},"Value":-1},"C":{"A":{"A":{"A":null,"B":null,"C":null,"Value":45},"B":{"A":null,"B":null,"C":null,"Value":46},"C":{"A":null,"B":null,"C":null,"Value":47},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":48},"B":{"A":null,"B":null,"C":null,"Value":49},"C":{"A":null,"B":null,"C":null,"Value":50},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":51},"B":{"A":null,"B":null,"C":null,"Value":52},"C":{"A":null,"B":null,"C":null,"Value":53},"Value":-1},"Value":-1},"Value":-1},"C":{"A":{"A":{"A":{"A":null,"B":null,"C":null,"Value":54},"B":{"A":null,"B":null,"C":null,"Value":55},"C":{"A":null,"B":null,"C":null,"Value":56},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":57},"B":{"A":null,"B":null,"C":null,"Value":58},"C":{"A":null,"B":null,"C":null,"Value":59},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":60},"B":{"A":null,"B":null,"C":null,"Value":61},"C":{"A":null,"B":null,"C":null,"Value":62},"Value":-1},"Value":-1},"B":{"A":{"A":{"A":null,"B":null,"C":null,"Value":63},"B":{"A":null,"B":null,"C":null,"Value":64},"C":{"A":null,"B":null,"C":null,"Value":65},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":66},"B":{"A":null,"B":null,"C":null,"Value":67},"C":{"A":null,"B":null,"C":null,"Value":68},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":69},"B":{"A":null,"B":null,"C":null,"Value":70},"C":{"A":null,"B":null,"C":null,"Value":71},"Value":-1},"Value":-1},"C":{"A":{"A":{"A":null,"B":null,"C":null,"Value":72},"B":{"A":null,"B":null,"C":null,"Value":73},"C":{"A":null,"B":null,"C":null,"Value":74},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":75},"B":{"A":null,"B":null,"C":null,"Value":76},"C":{"A":null,"B":null,"C":null,"Value":77},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":78},"B":{"A":null,"B":null,"C":null,"Value":79},"C":{"A":null,"B":null,"C":null,"Value":80},"Value":-1},"Value":-1},"Value":-1},"Value":-1}
  • {"A":{"A":{"A":{"A":null,"B":null,"C":null,"Value":0},"B":{"A":null,"B":null,"C":null,"Value":1},"C":{"A":null,"B":null,"C":null,"Value":2},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":3},"B":{"A":null,"B":null,"C":null,"Value":4},"C":{"A":null,"B":null,"C":null,"Value":5},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":6},"B":{"A":null,"B":null,"C":null,"Value":7},"C":{"A":null,"B":null,"C":null,"Value":8},"Value":-1},"Value":-1},"B":{"A":{"A":{"A":null,"B":null,"C":null,"Value":9},"B":{"A":null,"B":null,"C":null,"Value":10},"C":{"A":null,"B":null,"C":null,"Value":11},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":12},"B":{"A":null,"B":null,"C":null,"Value":13},"C":{"A":null,"B":null,"C":null,"Value":14},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":15},"B":{"A":null,"B":null,"C":null,"Value":16},"C":{"A":null,"B":null,"C":null,"Value":17},"Value":-1},"Value":-1},"C":{"A":{"A":{"A":null,"B":null,"C":null,"Value":18},"B":{"A":null,"B":null,"C":null,"Value":19},"C":{"A":null,"B":null,"C":null,"Value":20},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":21},"B":{"A":null,"B":null,"C":null,"Value":22},"C":{"A":null,"B":null,"C":null,"Value":23},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":24},"B":{"A":null,"B":null,"C":null,"Value":25},"C":{"A":null,"B":null,"C":null,"Value":26},"Value":-1},"Value":-1},"Value":-1}
  • {"A":{"A":{"A":null,"B":null,"C":null,"Value":0},"B":{"A":null,"B":null,"C":null,"Value":1},"C":{"A":null,"B":null,"C":null,"Value":2},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":3},"B":{"A":null,"B":null,"C":null,"Value":4},"C":{"A":null,"B":null,"C":null,"Value":5},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":6},"B":{"A":null,"B":null,"C":null,"Value":7},"C":{"A":null,"B":null,"C":null,"Value":8},"Value":-1},"Value":-1}
  • {"A":{"A":null,"B":null,"C":null,"Value":0},"B":{"A":null,"B":null,"C":null,"Value":1},"C":{"A":null,"B":null,"C":null,"Value":2},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":0}
  • {"A":null,"B":null,"C":null,"Value":1}
  • {"A":null,"B":null,"C":null,"Value":2}
  • {"A":{"A":null,"B":null,"C":null,"Value":3},"B":{"A":null,"B":null,"C":null,"Value":4},"C":{"A":null,"B":null,"C":null,"Value":5},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":3}
  • {"A":null,"B":null,"C":null,"Value":4}
  • {"A":null,"B":null,"C":null,"Value":5}
  • {"A":{"A":null,"B":null,"C":null,"Value":6},"B":{"A":null,"B":null,"C":null,"Value":7},"C":{"A":null,"B":null,"C":null,"Value":8},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":6}
  • {"A":null,"B":null,"C":null,"Value":7}
  • {"A":null,"B":null,"C":null,"Value":8}
  • {"A":{"A":{"A":null,"B":null,"C":null,"Value":9},"B":{"A":null,"B":null,"C":null,"Value":10},"C":{"A":null,"B":null,"C":null,"Value":11},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":12},"B":{"A":null,"B":null,"C":null,"Value":13},"C":{"A":null,"B":null,"C":null,"Value":14},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":15},"B":{"A":null,"B":null,"C":null,"Value":16},"C":{"A":null,"B":null,"C":null,"Value":17},"Value":-1},"Value":-1}
  • {"A":{"A":null,"B":null,"C":null,"Value":9},"B":{"A":null,"B":null,"C":null,"Value":10},"C":{"A":null,"B":null,"C":null,"Value":11},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":9}
  • {"A":null,"B":null,"C":null,"Value":10}
  • {"A":null,"B":null,"C":null,"Value":11}
  • {"A":{"A":null,"B":null,"C":null,"Value":12},"B":{"A":null,"B":null,"C":null,"Value":13},"C":{"A":null,"B":null,"C":null,"Value":14},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":12}
  • {"A":null,"B":null,"C":null,"Value":13}
  • {"A":null,"B":null,"C":null,"Value":14}
  • {"A":{"A":null,"B":null,"C":null,"Value":15},"B":{"A":null,"B":null,"C":null,"Value":16},"C":{"A":null,"B":null,"C":null,"Value":17},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":15}
  • {"A":null,"B":null,"C":null,"Value":16}
  • {"A":null,"B":null,"C":null,"Value":17}
  • {"A":{"A":{"A":null,"B":null,"C":null,"Value":18},"B":{"A":null,"B":null,"C":null,"Value":19},"C":{"A":null,"B":null,"C":null,"Value":20},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":21},"B":{"A":null,"B":null,"C":null,"Value":22},"C":{"A":null,"B":null,"C":null,"Value":23},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":24},"B":{"A":null,"B":null,"C":null,"Value":25},"C":{"A":null,"B":null,"C":null,"Value":26},"Value":-1},"Value":-1}
  • {"A":{"A":null,"B":null,"C":null,"Value":18},"B":{"A":null,"B":null,"C":null,"Value":19},"C":{"A":null,"B":null,"C":null,"Value":20},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":18}
  • {"A":null,"B":null,"C":null,"Value":19}
  • {"A":null,"B":null,"C":null,"Value":20}
  • {"A":{"A":null,"B":null,"C":null,"Value":21},"B":{"A":null,"B":null,"C":null,"Value":22},"C":{"A":null,"B":null,"C":null,"Value":23},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":21}
  • {"A":null,"B":null,"C":null,"Value":22}
  • {"A":null,"B":null,"C":null,"Value":23}
  • {"A":{"A":null,"B":null,"C":null,"Value":24},"B":{"A":null,"B":null,"C":null,"Value":25},"C":{"A":null,"B":null,"C":null,"Value":26},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":24}
  • {"A":null,"B":null,"C":null,"Value":25}
  • {"A":null,"B":null,"C":null,"Value":26}
  • {"A":{"A":{"A":{"A":null,"B":null,"C":null,"Value":27},"B":{"A":null,"B":null,"C":null,"Value":28},"C":{"A":null,"B":null,"C":null,"Value":29},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":30},"B":{"A":null,"B":null,"C":null,"Value":31},"C":{"A":null,"B":null,"C":null,"Value":32},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":33},"B":{"A":null,"B":null,"C":null,"Value":34},"C":{"A":null,"B":null,"C":null,"Value":35},"Value":-1},"Value":-1},"B":{"A":{"A":{"A":null,"B":null,"C":null,"Value":36},"B":{"A":null,"B":null,"C":null,"Value":37},"C":{"A":null,"B":null,"C":null,"Value":38},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":39},"B":{"A":null,"B":null,"C":null,"Value":40},"C":{"A":null,"B":null,"C":null,"Value":41},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":42},"B":{"A":null,"B":null,"C":null,"Value":43},"C":{"A":null,"B":null,"C":null,"Value":44},"Value":-1},"Value":-1},"C":{"A":{"A":{"A":null,"B":null,"C":null,"Value":45},"B":{"A":null,"B":null,"C":null,"Value":46},"C":{"A":null,"B":null,"C":null,"Value":47},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":48},"B":{"A":null,"B":null,"C":null,"Value":49},"C":{"A":null,"B":null,"C":null,"Value":50},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":51},"B":{"A":null,"B":null,"C":null,"Value":52},"C":{"A":null,"B":null,"C":null,"Value":53},"Value":-1},"Value":-1},"Value":-1}
  • {"A":{"A":{"A":null,"B":null,"C":null,"Value":27},"B":{"A":null,"B":null,"C":null,"Value":28},"C":{"A":null,"B":null,"C":null,"Value":29},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":30},"B":{"A":null,"B":null,"C":null,"Value":31},"C":{"A":null,"B":null,"C":null,"Value":32},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":33},"B":{"A":null,"B":null,"C":null,"Value":34},"C":{"A":null,"B":null,"C":null,"Value":35},"Value":-1},"Value":-1}
  • {"A":{"A":null,"B":null,"C":null,"Value":27},"B":{"A":null,"B":null,"C":null,"Value":28},"C":{"A":null,"B":null,"C":null,"Value":29},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":27}
  • {"A":null,"B":null,"C":null,"Value":28}
  • {"A":null,"B":null,"C":null,"Value":29}
  • {"A":{"A":null,"B":null,"C":null,"Value":30},"B":{"A":null,"B":null,"C":null,"Value":31},"C":{"A":null,"B":null,"C":null,"Value":32},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":30}
  • {"A":null,"B":null,"C":null,"Value":31}
  • {"A":null,"B":null,"C":null,"Value":32}
  • {"A":{"A":null,"B":null,"C":null,"Value":33},"B":{"A":null,"B":null,"C":null,"Value":34},"C":{"A":null,"B":null,"C":null,"Value":35},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":33}
  • {"A":null,"B":null,"C":null,"Value":34}
  • {"A":null,"B":null,"C":null,"Value":35}
  • {"A":{"A":{"A":null,"B":null,"C":null,"Value":36},"B":{"A":null,"B":null,"C":null,"Value":37},"C":{"A":null,"B":null,"C":null,"Value":38},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":39},"B":{"A":null,"B":null,"C":null,"Value":40},"C":{"A":null,"B":null,"C":null,"Value":41},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":42},"B":{"A":null,"B":null,"C":null,"Value":43},"C":{"A":null,"B":null,"C":null,"Value":44},"Value":-1},"Value":-1}
  • {"A":{"A":null,"B":null,"C":null,"Value":36},"B":{"A":null,"B":null,"C":null,"Value":37},"C":{"A":null,"B":null,"C":null,"Value":38},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":36}
  • {"A":null,"B":null,"C":null,"Value":37}
  • {"A":null,"B":null,"C":null,"Value":38}
  • {"A":{"A":null,"B":null,"C":null,"Value":39},"B":{"A":null,"B":null,"C":null,"Value":40},"C":{"A":null,"B":null,"C":null,"Value":41},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":39}
  • {"A":null,"B":null,"C":null,"Value":40}
  • {"A":null,"B":null,"C":null,"Value":41}
  • {"A":{"A":null,"B":null,"C":null,"Value":42},"B":{"A":null,"B":null,"C":null,"Value":43},"C":{"A":null,"B":null,"C":null,"Value":44},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":42}
  • {"A":null,"B":null,"C":null,"Value":43}
  • {"A":null,"B":null,"C":null,"Value":44}
  • {"A":{"A":{"A":null,"B":null,"C":null,"Value":45},"B":{"A":null,"B":null,"C":null,"Value":46},"C":{"A":null,"B":null,"C":null,"Value":47},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":48},"B":{"A":null,"B":null,"C":null,"Value":49},"C":{"A":null,"B":null,"C":null,"Value":50},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":51},"B":{"A":null,"B":null,"C":null,"Value":52},"C":{"A":null,"B":null,"C":null,"Value":53},"Value":-1},"Value":-1}
  • {"A":{"A":null,"B":null,"C":null,"Value":45},"B":{"A":null,"B":null,"C":null,"Value":46},"C":{"A":null,"B":null,"C":null,"Value":47},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":45}
  • {"A":null,"B":null,"C":null,"Value":46}
  • {"A":null,"B":null,"C":null,"Value":47}
  • {"A":{"A":null,"B":null,"C":null,"Value":48},"B":{"A":null,"B":null,"C":null,"Value":49},"C":{"A":null,"B":null,"C":null,"Value":50},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":48}
  • {"A":null,"B":null,"C":null,"Value":49}
  • {"A":null,"B":null,"C":null,"Value":50}
  • {"A":{"A":null,"B":null,"C":null,"Value":51},"B":{"A":null,"B":null,"C":null,"Value":52},"C":{"A":null,"B":null,"C":null,"Value":53},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":51}
  • {"A":null,"B":null,"C":null,"Value":52}
  • {"A":null,"B":null,"C":null,"Value":53}
  • {"A":{"A":{"A":{"A":null,"B":null,"C":null,"Value":54},"B":{"A":null,"B":null,"C":null,"Value":55},"C":{"A":null,"B":null,"C":null,"Value":56},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":57},"B":{"A":null,"B":null,"C":null,"Value":58},"C":{"A":null,"B":null,"C":null,"Value":59},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":60},"B":{"A":null,"B":null,"C":null,"Value":61},"C":{"A":null,"B":null,"C":null,"Value":62},"Value":-1},"Value":-1},"B":{"A":{"A":{"A":null,"B":null,"C":null,"Value":63},"B":{"A":null,"B":null,"C":null,"Value":64},"C":{"A":null,"B":null,"C":null,"Value":65},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":66},"B":{"A":null,"B":null,"C":null,"Value":67},"C":{"A":null,"B":null,"C":null,"Value":68},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":69},"B":{"A":null,"B":null,"C":null,"Value":70},"C":{"A":null,"B":null,"C":null,"Value":71},"Value":-1},"Value":-1},"C":{"A":{"A":{"A":null,"B":null,"C":null,"Value":72},"B":{"A":null,"B":null,"C":null,"Value":73},"C":{"A":null,"B":null,"C":null,"Value":74},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":75},"B":{"A":null,"B":null,"C":null,"Value":76},"C":{"A":null,"B":null,"C":null,"Value":77},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":78},"B":{"A":null,"B":null,"C":null,"Value":79},"C":{"A":null,"B":null,"C":null,"Value":80},"Value":-1},"Value":-1},"Value":-1}
  • {"A":{"A":{"A":null,"B":null,"C":null,"Value":54},"B":{"A":null,"B":null,"C":null,"Value":55},"C":{"A":null,"B":null,"C":null,"Value":56},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":57},"B":{"A":null,"B":null,"C":null,"Value":58},"C":{"A":null,"B":null,"C":null,"Value":59},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":60},"B":{"A":null,"B":null,"C":null,"Value":61},"C":{"A":null,"B":null,"C":null,"Value":62},"Value":-1},"Value":-1}
  • {"A":{"A":null,"B":null,"C":null,"Value":54},"B":{"A":null,"B":null,"C":null,"Value":55},"C":{"A":null,"B":null,"C":null,"Value":56},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":54}
  • {"A":null,"B":null,"C":null,"Value":55}
  • {"A":null,"B":null,"C":null,"Value":56}
  • {"A":{"A":null,"B":null,"C":null,"Value":57},"B":{"A":null,"B":null,"C":null,"Value":58},"C":{"A":null,"B":null,"C":null,"Value":59},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":57}
  • {"A":null,"B":null,"C":null,"Value":58}
  • {"A":null,"B":null,"C":null,"Value":59}
  • {"A":{"A":null,"B":null,"C":null,"Value":60},"B":{"A":null,"B":null,"C":null,"Value":61},"C":{"A":null,"B":null,"C":null,"Value":62},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":60}
  • {"A":null,"B":null,"C":null,"Value":61}
  • {"A":null,"B":null,"C":null,"Value":62}
  • {"A":{"A":{"A":null,"B":null,"C":null,"Value":63},"B":{"A":null,"B":null,"C":null,"Value":64},"C":{"A":null,"B":null,"C":null,"Value":65},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":66},"B":{"A":null,"B":null,"C":null,"Value":67},"C":{"A":null,"B":null,"C":null,"Value":68},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":69},"B":{"A":null,"B":null,"C":null,"Value":70},"C":{"A":null,"B":null,"C":null,"Value":71},"Value":-1},"Value":-1}
  • {"A":{"A":null,"B":null,"C":null,"Value":63},"B":{"A":null,"B":null,"C":null,"Value":64},"C":{"A":null,"B":null,"C":null,"Value":65},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":63}
  • {"A":null,"B":null,"C":null,"Value":64}
  • {"A":null,"B":null,"C":null,"Value":65}
  • {"A":{"A":null,"B":null,"C":null,"Value":66},"B":{"A":null,"B":null,"C":null,"Value":67},"C":{"A":null,"B":null,"C":null,"Value":68},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":66}
  • {"A":null,"B":null,"C":null,"Value":67}
  • {"A":null,"B":null,"C":null,"Value":68}
  • {"A":{"A":null,"B":null,"C":null,"Value":69},"B":{"A":null,"B":null,"C":null,"Value":70},"C":{"A":null,"B":null,"C":null,"Value":71},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":69}
  • {"A":null,"B":null,"C":null,"Value":70}
  • {"A":null,"B":null,"C":null,"Value":71}
  • {"A":{"A":{"A":null,"B":null,"C":null,"Value":72},"B":{"A":null,"B":null,"C":null,"Value":73},"C":{"A":null,"B":null,"C":null,"Value":74},"Value":-1},"B":{"A":{"A":null,"B":null,"C":null,"Value":75},"B":{"A":null,"B":null,"C":null,"Value":76},"C":{"A":null,"B":null,"C":null,"Value":77},"Value":-1},"C":{"A":{"A":null,"B":null,"C":null,"Value":78},"B":{"A":null,"B":null,"C":null,"Value":79},"C":{"A":null,"B":null,"C":null,"Value":80},"Value":-1},"Value":-1}
  • {"A":{"A":null,"B":null,"C":null,"Value":72},"B":{"A":null,"B":null,"C":null,"Value":73},"C":{"A":null,"B":null,"C":null,"Value":74},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":72}
  • {"A":null,"B":null,"C":null,"Value":73}
  • {"A":null,"B":null,"C":null,"Value":74}
  • {"A":{"A":null,"B":null,"C":null,"Value":75},"B":{"A":null,"B":null,"C":null,"Value":76},"C":{"A":null,"B":null,"C":null,"Value":77},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":75}
  • {"A":null,"B":null,"C":null,"Value":76}
  • {"A":null,"B":null,"C":null,"Value":77}
  • {"A":{"A":null,"B":null,"C":null,"Value":78},"B":{"A":null,"B":null,"C":null,"Value":79},"C":{"A":null,"B":null,"C":null,"Value":80},"Value":-1}
  • {"A":null,"B":null,"C":null,"Value":78}
  • {"A":null,"B":null,"C":null,"Value":79}
  • {"A":null,"B":null,"C":null,"Value":80}

The data structure does not need to recursively nest, of course; the members of Nested could be of another class Nested2, which in turn has members of a class Nested3, which in turn has members of a class Nested4, and the results would be the same.

The reason the code this PR is changing creates these strings is that it might be deserializing a primitive value, or an enum member name, in which case it needs the value in string form. However, if the code flow skips over all those branches and ends up at the call to CreateAndMap, then the string is never used and all that work is wasted.

The changes in this PR eliminate all this unnecessary JSON serialization. The value is only converted to a string if the data type is one of the simpler, primitive types, which pretty much guarantees that it won't be a JsonObject or JsonArray, and is probably already a System.String anyway, in which case the .ToString() isn't actually doing any work.

@logiclrd
Copy link
Author

logiclrd commented Nov 7, 2016

@hallem I notice you merged in a whole bunch of changes back in April, and these are the most recent changes merged in. I was wondering whether this PR might be under consideration? :-)

@alexeyzimarev
Copy link
Member

The main issue here is that SimpleJson.NET is not being updated for four years. We plan to move back to JSON.NET.

@logiclrd
Copy link
Author

logiclrd commented Sep 6, 2017

Okay, cool beans :-) As long as the problem goes away in the end!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants