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

Readonly property corrupts normal property object references. #486

Closed
MagicShoebox opened this issue Feb 13, 2015 · 1 comment
Closed

Readonly property corrupts normal property object references. #486

MagicShoebox opened this issue Feb 13, 2015 · 1 comment

Comments

@MagicShoebox
Copy link

Stack Overflow Thread

If the Deserializer encounters an object reference ("$ref": "2") that was originally defined in a readonly property (get; only), it fails to deserialize and returns null instead.

Class

public class Parent
{
    public Child ReadOnlyChild
    {
        get
        {
            return Child;
        }
    }

    public Child Child {get; set;}
}
public class Child
{
}

Serialization:

Parent p = new Parent() { Child = new Child() };
JsonConvert.SerializeObject(p, new JsonSerializerSettings()
{ Formatting = Formatting.Indented,
  PreserveReferencesHandling = PreserveReferencesHandling.Objects });

Serialized:

{
  "$id": "1",
  "ReadOnlyChild": {
    "$id": "2",
  },
  "Child": {
    "$ref": "2"
  }
}

Deserialized (& reserialized to show the change):

{
  "$id": "1",
  "ReadOnlyChild": null,
  "Child": null
}

Note that sometimes [JsonProperty(Order = #)] is needed to force the serializer to act on ReadOnlyChild first. Regardless, the above Serialized JSON block will fail to deserialize properly, even if the class has been modified to deserialize Child first.

To clarify, the problem is that the Child property is being read as null, not that ReadOnlyChild is unset.

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

No branches or pull requests

2 participants