Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Ignoring Fields and Properties Conditionally During Serialization Using JSON.Net?

Tags:

c#

json.net

How to Ignoring Fields and Properties Conditionally During Serialization Using JSON.Net? I can't inherit from JsonIgnoreAttribute because it's a sealed class. What should I do?

like image 425
Mohamad Shiralizadeh Avatar asked Dec 16 '15 05:12

Mohamad Shiralizadeh


People also ask

How do I ignore JSON property based on condition?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

How do I ignore a field in JSON?

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON.

Which of the following attributes of the JSON can be used to prevent a property from being serialized or Deserialized?

To ignore individual properties, you can use the [JsonIgnore] attribute to prevents a property from being serialized or deserialized.


2 Answers

You can use JSON.NET's ShouldSerialize-syntax. There's a good example on JSON.NET site:

http://www.newtonsoft.com/json/help/html/ConditionalProperties.htm

public class Employee
{
    public string Name { get; set; }
    public Employee Manager { get; set; }

    public bool ShouldSerializeManager()
    {
        // don't serialize the Manager property if an employee is their own manager
        return (Manager != this);
    }
}

If ShouldSerialize doesn't fit your needs, you can take full control of the serialization with the ContractResolvers: http://www.newtonsoft.com/json/help/html/ContractResolver.htm

like image 165
Mikael Koskinen Avatar answered Oct 21 '22 21:10

Mikael Koskinen


I found the answer. I inherit from JsonConverter and create a new convertor.

public class CustomJsonConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var list = (IList)value;

        JArray s = new JArray();

        foreach (var item in list)
        {
            JToken token = JToken.FromObject(item);
            JObject obj = new JObject();

            foreach (JProperty prop in token)
            {
                if (prop.Name != "Title") // your logic here
                    obj.Add(prop);
            }

            s.Add(obj);
        }

        s.WriteTo(writer);

    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
    }

    public override bool CanRead
    {
        get { return false; }
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType != typeof(IList);
    }
}
like image 24
Mohamad Shiralizadeh Avatar answered Oct 21 '22 19:10

Mohamad Shiralizadeh