Ming
Ming

Reputation: 740

json.net required property not found in json

I am using Json.net, I got a class as following

public class RecordAlias
    {   
        [JsonProperty(PropertyName = "eId", Required = Required.Always)]
        public string EntityId { get; set; }

        [JsonProperty(PropertyName = "aId", Required = Required.AllowNull)]
        public string AliasId { get; set; }

        [JsonProperty(PropertyName = "iSd", Required = Required.AllowNull)]
        public bool IsSelected { get; set; }
    }

So that following json can be deserialized even through some items don't have property "iSd" in json string, I would expect a default value of that type should be populated if not present, for example, IsSelected should be false except last item

      [{
        "eId" : "30022004",
        "aId" : "1"
    }, {
        "eId" : "30021841",
        "aId" : "1"
    }, {
        "eId" : "30021848",
        "aId" : "1"
        "iSd" : true
    }
]

Any idea how can I achieve this?

Upvotes: 12

Views: 32971

Answers (3)

MSeifert
MSeifert

Reputation: 152725

I made a little table for the Required enum values and their effect based on the Required documentation:

Option Must be present Can be Null value
Required.Default
Required.AllowNull
Required.Always
Required.DisallowNull

In your case the isD is optional you should have used Required.Default (or Required.DisallowNull). Using Required.AllowNull also makes the isD mandatory and it thus throws the Exception when it's missing.

Note that in this case it makes no sense to differentiate between "Optional and may be null" (Required.Default) or "Optional but may not be null" (Required.DisallowNull) because bool is a value type that cannot be null. If you wanted to allow null values you need to use a nullable value type (bool?), however then the default value (when the value is not present) would be null, except when you set it manually (for example to false):

[JsonProperty(PropertyName = "iSd", Required = Required.Default)]
public bool? IsSelected { get; set; } = false;

Upvotes: 15

David Hoerster
David Hoerster

Reputation: 28701

You're not specifying eId in your JSON string, and it's set to be required. You're passing rId...is this the same thing?

If I'm not understanding the question, please let me know and I'll update my answer.

UPDATE: You're indicating that the iSd property is AllowNull. You still have to specify a value for this property in your JSON string, but it can be null. Per the JSON.NET specs:

The property must be defined in JSON but can be a null value.

You need to specify a value for iSd...or mark iSd in your JsonProperty attribute as DefaultValue. For DefaultValue, the spec says:

The property is not required. The default state.

[JsonProperty(PropertyName = "iSd", Required = Required.Default)]
public bool IsSelected { get; set; }

I hope this helps.

Upvotes: 15

Daniel A. White
Daniel A. White

Reputation: 190966

Make the property a bool not a string.

Upvotes: 1

Related Questions