17

I have tried to prevent the property of type object with no new values assigned to its properties using ShouldSerialize Method in Newtonsoft.Json. But I dont know how to implement it, so please help me to solve this...

Here is the sample code

public class Sample1
 {
   public String name{get;set;}
   public int Id{get;set;}; 
 }

And this is my Class containing the above class as one of its properties

public class Container
 {
   public String Cname{get;set;}
   public Sample1 Sample{get;set;}; 

   public bool ShouldSerializeSample()
  {
      //What should I write here to prevent the Sample property from being serialized when its properties are assigned no new values.

  }
 }
LeftyX
  • 35,328
  • 21
  • 132
  • 193
Madhu
  • 2,416
  • 3
  • 15
  • 33
  • What do you mean by "no new values assigned"? Can you provide an example? – Brian Rogers Oct 31 '13 at 05:53
  • It means the property which is of type Object has its own properties with default values. – Madhu Oct 31 '13 at 06:29
  • The answer will depend on what example `Sample1` is. For example, if `Sample1` is a reference type and defaults to `null`, you should return `true` if the `Sample` property is not `null`, otherwise false. If `Sample1` is an object with its own fields, you should return `true` if any of `Sample1`'s fields are non-default values, otherwise `false`. – William Oct 31 '13 at 06:41

1 Answers1

26

Given your example classes, I think you you are looking for something like this:

public bool ShouldSerializeSample()
{
    return (Sample != null && (Sample.Id != 0 || Sample.name != null));
}

Here is a working demo:

class Program
{
    static void Main(string[] args)
    {
        List<Container> list = new List<Container>
        {
            new Container
            {
                Cname = "Will serialize Sample because it has a name",
                Sample = new Sample1 { name = "sample 1" }
            },
            new Container
            {
                Cname = "Will serialize Sample because it has a non-zero Id",
                Sample = new Sample1 { Id = 2 }
            },
            new Container
            {
                Cname = "Will serialize Sample because it has a name and an Id",
                Sample = new Sample1 { name = "sample 3", Id = 3 }
            },
            new Container
            {
                Cname = "Will not serialize Sample because it has default values",
                Sample = new Sample1()
            },
            new Container
            {
                Cname = "Will not serialize Sample because it is null",
                Sample = null
            }
        };

        string json = JsonConvert.SerializeObject(list, Formatting.Indented);
        Console.WriteLine(json);
    }
}

public class Sample1
{
    public String name { get; set; }
    public int Id { get; set; }
}

public class Container
{
    public String Cname { get; set; }
    public Sample1 Sample { get; set; }

    public bool ShouldSerializeSample()
    {
        return (Sample != null && (Sample.Id != 0 || Sample.name != null));
    }
}

Here is the output:

[
  {
    "Cname": "Will serialize Sample because it has a name",
    "Sample": {
      "name": "sample 1",
      "Id": 0
    }
  },
  {
    "Cname": "Will serialize Sample because it has a non-zero Id",
    "Sample": {
      "name": null,
      "Id": 2
    }
  },
  {
    "Cname": "Will serialize Sample because it has a name and an Id",
    "Sample": {
      "name": "sample 3",
      "Id": 3
    }
  },
  {
    "Cname": "Will not serialize Sample because it has default values"
  },
  {
    "Cname": "Will not serialize Sample because it is null"
  }
]
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • @Brain Hi **Thanks** for your help... Its useful. But now i need to use `Reflection` in my `ShouldSerialize` Method, if you have any ideas please share it. – Madhu Nov 01 '13 at 04:10
  • @Madhu You need to be more specific. What are you trying to accomplish with reflection? – Brian Rogers Nov 01 '13 at 17:05