0

I have following JSON

{
  "PROPERTY1": "vvv",
  "PROPERTY2": "aa",
}

That i want to deserialize with following data model class

public class Class1
{
    public Class1()
    {

    }

    [JsonProperty(Required = Required.Always)]
    public string PROPERTY1{ get; set; }

    [JsonProperty(Required = Required.Always)]
    public string PROPERTY2{ get; set; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public decimal PROPERTY3 { get; set; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string PROPERTY4 { get; set; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public SomeObject PROPERTY5 { get; set; }

}

And use following serialization code

deserializedResponse = JsonConvert.DeserializeObject<Class1>(response);

As a result i expect to receive Class1 with PROPERTY1 and PROPERTY2, Actually i get all 4 properties while PROPERTY3 is 0 , PROPERTY4 = null, PROPERTY5 = "".

Tried to pass serialize settings as 2 parameter

  JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
    {       
        NullValueHandling = NullValueHandling.Ignore
    };

Not helped.

I want Class1 Object with no PROPERTY3 and PROPERTY4 and PROPERTY5 if they not present in JSON

Any suggestions?

Alexander Gorelik
  • 3,985
  • 6
  • 37
  • 53
  • 3
    What's the issue? Of course you'll still have those properties they're part of the class. They'll just have their default values (hence `null` and `0`). The null handling is for serializing, they'll be ignored when writing out. If you want a dynamic type, use `dynamic` and not a concrete class. – Lloyd Jul 25 '16 at 11:30
  • Also, your JSON is not valid. Should be { "PROPERTY1":"vvv", "PROPERTY2":"aa" } – Antipod Jul 25 '16 at 11:33
  • @Lloyd Typo :-) I want Class1 Object with no PROPERTY3 and PROPERTY4 if they not present in JSON – Alexander Gorelik Jul 25 '16 at 11:35
  • @AlexanderGorelik Then try `dynamic` as you can't strip properties out of your class adhoc - http://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net – Lloyd Jul 25 '16 at 11:54
  • @Lloyd this is the easiest way to handle de-serialization but i want solid structure and not documentation on the dynamic structure. – Alexander Gorelik Jul 25 '16 at 11:57
  • 1
    @AlexanderGorelik You can't it's one or the other I'm afraid. – Lloyd Jul 25 '16 at 13:54

3 Answers3

2
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public decimal PROPERTY3 { get; set; }

Your property is a value type. That is it always has a value. It gets a 0 and this changes when you assign. You have to convert to Nullable types to have that property becoming null.

You can also use dynamic. See here: Deserialize json object into dynamic object using Json.net

This allows you to to use just the properties that exist. Other questions have answers that explain how to check for existance of properties: How do I check if a property exists on a dynamic anonymous type in c#?

Community
  • 1
  • 1
Sascha
  • 10,231
  • 4
  • 41
  • 65
1

If you don't want PROPERTY3 and PROPERTY4 if they are not in the json, you should deserialize like this :

deserializedResponse = JsonConvert.DeserializeObject<object>(response, jsonSerializerSettings);

I mean instead of deserialize to a class1 you should deserialize to a generic object. I'm just not sure about the porpouse.

  • In class 1 Property1 and Property2 are mandatory and other properties are optional.I will throw exception if first two are not present, and will check the other 3 to complete my validation process. I want to check the properties existence and not their value. – Alexander Gorelik Jul 25 '16 at 11:55
  • @AlexanderGorelik Even then you should use Nullable types or default value handling. Everything else is either reflection or dynamic programming which can be a serious performance issue... – Sascha Jul 25 '16 at 12:19
1

In this case, you should create an inheritance :

Class1 with PROPERTY1 and PROPERTY2.

Class2, derived of Class1 with PROPERTY3, PROPERTY4 and PROPERTY5.

NullValueHandling is used only for Serialization purpose.

Didier Aupest
  • 3,227
  • 2
  • 23
  • 35