2

I am new into json.net, so any help would be appreciated.

I am using the net 2.0 version of json.net, because i need to integrate it in Unity3d engine Suggested answer (using nullvaluehandling) just don't work! Should i consider this as restiction of the oldest version?

So, a have an object like this:

Object ()
{
   ChildObject ChildObject1;
   ChildObject ChildObject2;
}

ChildObject ()
{
   Property1;
   Property2;
}

I want json.net to serialize only not null properties of all objects, including child objects. So if i instatiate only property1 of ChildObject1 and property2 of ChildObject2, the string from JsonConvert will be like this:

{
  "ChildObject1": 
        {
           "Property1": "10"
        },
  "ChildObject1": 
        {
          "Property2":"20"
        }
}

But default behaviour creates string like this:

{
  "ChildObject1": 
           {
             "Property1": "10", 
             "Property2": "null" 
           },
  "ChildObject2": 
           {
             "Property1": "null, 
             "Property2":"20"
           }
}

I know about NullValueHandling, but it is not working corretly in my case! It ignore only null properties of the parent object (object that we are serializing), but if this parent object has some childs, it will not ignore null properties of this child objects. My situation is different.

If even one property of a child object is not null, it serialize it to a string with one not-null property and others null.


UPD example of my code:

i have nested classes

My object is like this:

public class SendedMessage
{
    public List<Answer> Answers { get; set; }
    public AnswerItem Etalon {get; set;}
}

public class Answer () 
{
public AnswerItem Item { get; set; }
}

   public class AnswerItem () 
   {
   public string ID { get; set; }
   public bool Result { get; set; }
   public int Number { get; set; }
   }

i instatiate a SendedMessage object like this:

new SendedMessage x;
x.Etalon = new AnswerItem();
x.Etalon.Result = true;

than i use

string ignored = JsonConvert.SerializeObject(x,
          Formatting.Indented,
          new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

and it make this string:

{ "Etalon": {"ID" = "null", "Result" = "false", Number" = "null"}}

so it really ignore the null object Answers (a List), but do not ignore the null properties of a child objects.

Thanks for help!

Rodoleia
  • 21
  • 1
  • 5
  • 1
    Possible duplicate of [How to ignore a property in class if null, using json.net](http://stackoverflow.com/questions/6507889/how-to-ignore-a-property-in-class-if-null-using-json-net) – JeetDaloneboy Sep 19 '16 at 11:00
  • refer this : http://stackoverflow.com/questions/33027409/json-net-serialize-jobject-while-ignoring-null-properties – JeetDaloneboy Sep 19 '16 at 11:00
  • well, no, it's different situation. NullvalueHandling.Ignore **not ignore the null properties of child objects** of the serialized object. – Rodoleia Sep 19 '16 at 11:41
  • Can you share a [mcve] of your problem? I believe `new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })` should apply recursively to all object levels. – dbc Sep 19 '16 at 19:25
  • I cannot reproduce your problem, [`JsonSerializerSettings.NullValueHandling`](http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonSerializerSettings_NullValueHandling.htm) works as expected. See https://dotnetfiddle.net/FpG0lk. – dbc Sep 19 '16 at 19:37
  • @dbc, i did not think it is important, but maybe a problem is that i am using a version of json.net which is builded to be compatible with unity3d game engine? (.net 3.5 or even 2.0, i am not sure) – Rodoleia Sep 20 '16 at 15:07
  • It might be. Using the current version of Json.NET none of us can reproduce your problem. Please give details of the version of Json.NET you are using, including where it came from as well as the value for `typeof(JsonSerializer).Assembly.FullName` – dbc Sep 20 '16 at 19:01

1 Answers1

3

use this :

string ignored = sonConvert.SerializeObject(movie,
        Formatting.Indented,
        new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

here is the example:

public class Movie
{
    public string Name { get; set; }
    public string Description { get; set; }
    public string Classification { get; set; }
    public string Studio { get; set; }
    public DateTime? ReleaseDate { get; set; }
    public List<string> ReleaseCountries { get; set; }
}

static void Main(string[] args)
{
    Movie movie = new Movie();
    movie.Name = "Bad Boys III";
    movie.Description = "It's no Bad Boys";

    string included = JsonConvert.SerializeObject(movie,
        Formatting.Indented,new JsonSerializerSettings { });

            // {
            //   "Name": "Bad Boys III",
            //   "Description": "It's no Bad Boys",
            //   "Classification": null,
            //   "Studio": null,
            //   "ReleaseDate": null,
            //   "ReleaseCountries": null
            // }

            string ignored = JsonConvert.SerializeObject(movie,
              Formatting.Indented,
              new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

            // {
            //   "Name": "Bad Boys III",
            //   "Description": "It's no Bad Boys"
            // }
        }
JeetDaloneboy
  • 399
  • 2
  • 16
  • you can also put the data-attribute on the properties that you want (to ignore) – Roelant M Sep 19 '16 at 11:10
  • @JeetDaloneboy, i am using this method right now, and it leads to the problem that maked me answer this question. NullValueHandling.Ignore ignores only child objects that are fully null. If a have a child object in an object that i am serializing, and that child has 10 properties, and only one of that properties is not null , it will lead to the string with 9 null and only one meaningful property. I want json to ignore nested null properties. – Rodoleia Sep 19 '16 at 11:32
  • @JeetDaloneboy, i added a code example as an answer, cause it's too long for a comment – Rodoleia Sep 19 '16 at 12:52
  • thanks a lot for your help, @JeetDaloneboy, but the your suggested answer just don't work in my environment. My code is exactly the same! Maybe it counts that i am using json.net version for Unity3d? (it means .net 2.0) – Rodoleia Sep 20 '16 at 15:00