How do I use the ShouldSerialize [MemberName] () method for a property of type Object?

I tried to prevent a property of an object of a type with no new values ​​assigned to its properties using the ShouldSerialize method in Newtonsoft.Json. But I don't know how to implement it, so please help me to solve this problem ...

Here is some 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.

  }
 }

      

+13


source to share


1 answer


Given your class examples, I think you are looking for something like this:

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

      

Here's 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's the result:

[
  {
    "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"
  }
]

      

+16


source







All Articles