2

How do you know the difference when an item in the json has the value null. Or if the value isn't at all in the json. Both will end up as null in the Entityobject. (made it up)

public class Entityobject
    {
        public Document document { get; set; }
        public int randomint { get; set; }
        public string randomstr{ get; set; }
    }

Entityobject entity_obj= JsonConvert.DeserializeObject<Entityobject>(json);

so for example if the json is:

String json = "{ 
randomstr = null,
randomint = 1
}"

Now document will be null as well of course. But how do I know the difference? To clarify I can't set other values up front. And I actually prefer a better answer then: Just make another object which as other initial values then null. Also I still want to send null in the json. So I also can't send another value(like "delete" or so) and make it null afterwards. So to end the question: I want to know the difference between a null given in the json and a null which happens because it wasn't included in the json string.

Hopefully this is clear enough. Thank you in advance:)

Riddim
  • 143
  • 1
  • 8
  • Do you just want to test if a specific JSON string has the property? Or do you want also to deserialize into `Entityobject`? – Yacoub Massad Jan 21 '16 at 20:42

2 Answers2

1

It's not very straightforward, but you could craft your object so that calling the setter of a property will set another field of property:

class Foo
{
    private string _bar;
    public string Bar
    {
        get { return _bar; }
        set
        {
            _bar = value;
            BarSpecified = true;
        }
    }

    [JsonIgnore]
    public bool BarSpecified { get; set; }
}

If Bar is present in the JSON, BarSpecified will be true, otherwise it will be false.

Note that the name BarSpecified is not a random choice; the suffix Specified has a special meaning for JSON.NET (and XmlSerializer as well): it controls whether or not the property is serialized.

So, if you deserialize the object and serialize it again, the new JSON will be identical to the original one (in terms of whether or not the property is present in the JSON).

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Good answer, and good to learn. Going to try this and the other answer. And maybe answer myself or accept one of you:) – Riddim Jan 21 '16 at 20:57
1

You could set a default value for each field in your EntityObject. That way, you can ensure that if the value really equals null it was assigned null explicitly.

For example (C# 5):

public class Entityobject
{
    private Document _document = Document.EmptyDocument;
    public Document document
    {
        get
        {
            return _document;
        }
        set
        {
            _document = value;
        }
    }

    private int? _randomint = 0;
    public int? randomint
    {
        get
        {
            return _randomint;
        }
        set
        {
            _randomint = value;
        }
    }

    private string _randomstr = String.Empty;
    public string randomstr
    {
        get
        {
            return _randomstr;
        }
        set
        {
            _randomstr = value;
        }
    }
}

Even easier in C# 6:

public class Entityobject
{
    public Document document { get; set; } = Document.EmptyDocument;
    public int randomint { get; set; } = 0;
    public string randomstr{ get; set; } = String.Empty;
}
Matt Hensley
  • 908
  • 8
  • 18
  • Like I said, I actually did not want to to do this... But maybe it is still the best solution. I'm going to try something and maybe answer myself or accept one of you:). – Riddim Jan 21 '16 at 20:58
  • Yeah, I noticed that but I am not sure you have much of a choice in this situation. Without using defaults @Thomas Levesque's answer seems like the best option. – Matt Hensley Jan 21 '16 at 21:02