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

Exception deserializing JArray on JToken-typed property pre-initialized with JValue #2529

Closed
FstTesla opened this issue May 14, 2021 · 0 comments · Fixed by #2530
Closed

Comments

@FstTesla
Copy link

Destination type

class Something
{
    public JToken Foo { get; set; } = JValue.CreateNull();
}

Source JSON

{ "foo": [] }

Expected behavior

Successfully deserialized a Something object with property Foo equal to an empty JArray.

Actual behavior

Exception JsonSerializationException: Deserialized JSON type 'Newtonsoft.Json.Linq.JArray' is not compatible with expected type 'Newtonsoft.Json.Linq.JValue'.

Steps to reproduce

JsonConvert.DeserializeObject<Something>("{ \"foo\": [] }");

Observations

Issue is new in version 13.0.1 and did not affect version 12.0.3.
Probably this is an unwanted side effect of fixing issue #2484.

The error comes from the difference between the types of the pre-initialized value of the property (null :: JValue) and the one provided in the input ([] :: JArray).
Therefore any of the following changes does not raise the exception:

  • removing the property initializer of Foo;
  • replacing the property initializer of Foo with an instance of JArray (even a non-empty one);
  • removing the foo property in the input;
  • replacing the foo property in the input with a JValue-compatible JSON snippet.
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 a pull request may close this issue.

1 participant