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

[System.Text.Json] About difference between property with null and does not exists #29468

Closed
jkbrito opened this issue May 7, 2019 · 5 comments
Assignees
Labels
area-System.Text.Json json-functionality-doc Missing JSON specific functionality that needs documenting question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@jkbrito
Copy link

jkbrito commented May 7, 2019

How do we deal with the difference between a json property being null, and there is no property?
For example:

public class ObjectOne {

    [JsonProperty(Name = "propertyOne")]
    public string PropertyOne {get;set;}
    
    [JsonProperty(Name = "propertyTwo")]
    public string PropertyTwo {get;set;}

}
{"propertyOne": null, "propertyTwo":null}
{"propertyOne": null }

We could create a struct that contains the information of whether a property exists or not in deserialization. For exemple.

 public struct JsonMetadata<T>
    {
        public JsonMetadata(T obj)
        {
            HasProperty = true;
            Value = obj;
        }

        public bool HasProperty { get; }  
        public T Value { get; }

        public static implicit operator T(JsonMetadata<T> jMetadata) => jMetadata.Value;
        public static implicit operator JsonMetadata<T>(T value) => new JsonMetadata<T>(value);
    }

 public class ObjectOne {

    [JsonProperty(Name = "propertyOne")]
    public string PropertyOne {get;set;}
    
    [JsonProperty(Name = "propertyTwo")]
    public JsonMetadata<string> PropertyTwo {get;set;}

}

Is this possible? Is there any way to handle this case in the current implementation?

@ahsonkhan
Copy link
Member

cc @steveharter

@steveharter
Copy link
Member

Potential workarounds:

  1. Set the property to some placeholder\singleton value which will get overridden during deserialize if the JSON contains the value
  2. Implement the setter for the property(s) and set some private bool to track.
  3. Implement a custom converter and track whether the property exists in the JSON or not.

@JamesNK does Json.NET have a mechanism to detect whether a property was deserialized?

@JamesNK
Copy link
Member

JamesNK commented Jun 11, 2019

No. There is [Required] that will throw an error if there is no JSON for that property, but nothing like this.

All three of the options you listed are good solutions.

@pranavkm
Copy link
Contributor

@JamesNK, did you mean JsonPropertyRequired

MVC users have often asked for a way to identify when a property isn't on the wire. While @steveharter's solutions are acceptable for the trivial cases, it really doesn't scale well if you want the behavior to apply in general. Plus, you're forced to manually perform the checks which seems cumbersome.

Could we consider introducing an attribute like Json.NET to enforce this behavior?

@ahsonkhan
Copy link
Member

I believe @steveharter has provided the relevant info for the question being posed in this issue. Hence closing. The discussion related to the [Required] property can continue in https://github.com/dotnet/corefx/issues/38492.

Thanks.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json json-functionality-doc Missing JSON specific functionality that needs documenting question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

6 participants