Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing null in JSON.NET

When serializing arbitrary data via JSON.NET, any property that is null is written to the JSON as

"propertyName" : null

This is correct, of course.

However I have a requirement to automatically translate all nulls into the default empty value, e.g. null strings should become String.Empty, null int?s should become 0, null bool?s should be false, and so on.

NullValueHandling is not helpful, since I dont want to Ignore nulls, but neither do I want to Include them (Hmm, new feature?).

So I turned to implementing a custom JsonConverter.
While the implementation itself was a breeze, unfortunately this still didnt work - CanConvert() is never called for a property that has a null value, and therefore WriteJson() is not called either. Apparently nulls are automatically serialized directly into null, without the custom pipeline.

For example, here is a sample of a custom converter for null strings:

public class StringConverter : JsonConverter {     public override bool CanConvert(Type objectType)     {         return typeof(string).IsAssignableFrom(objectType);     }      ...     public override void WriteJson(JsonWriter writer,                  object value,                  JsonSerializer serializer)     {         string strValue = value as string;          if (strValue == null)         {             writer.WriteValue(String.Empty);         }         else         {             writer.WriteValue(strValue);         }     } } 

Stepping through this in the debugger, I noted that neither of these methods are called for properties that have a null value.

Delving into JSON.NET's sourcecode, I found that (apparently, I didnt go into a lot of depth) there is a special case checking for nulls, and explictly calling .WriteNull().

For what it's worth, I did try implementing a custom JsonTextWriter and overriding the default .WriteNull() implementation...

public class NullJsonWriter : JsonTextWriter {     ...      public override void WriteNull()     {         this.WriteValue(String.Empty);     } } 

However, this can't work well, since the WriteNull() method knows nothing about the underlying datatype. So sure, I can output "" for any null, but that doesnt work well for e.g. int, bool, etc.

So, my question - short of converting the entire data structure manually, is there any solution or workaround for this?

like image 574
AviD Avatar asked Jan 12 '12 11:01

AviD


People also ask

How do I ignore null values in JSON Deserializing?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.

What is serializing in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

Is null JSON serializable?

Apparently nulls are automatically serialized directly into null , without the custom pipeline.


1 Answers

Okay, I think I've come up with a solution (my first solution wasn't right at all, but then again I was on the train). You need to create a special contract resolver and a custom ValueProvider for Nullable types. Consider this:

public class NullableValueProvider : IValueProvider {     private readonly object _defaultValue;     private readonly IValueProvider _underlyingValueProvider;       public NullableValueProvider(MemberInfo memberInfo, Type underlyingType)     {         _underlyingValueProvider = new DynamicValueProvider(memberInfo);         _defaultValue = Activator.CreateInstance(underlyingType);     }      public void SetValue(object target, object value)     {         _underlyingValueProvider.SetValue(target, value);     }      public object GetValue(object target)     {         return _underlyingValueProvider.GetValue(target) ?? _defaultValue;     } }  public class SpecialContractResolver : DefaultContractResolver {     protected override IValueProvider CreateMemberValueProvider(MemberInfo member)     {         if(member.MemberType == MemberTypes.Property)         {             var pi = (PropertyInfo) member;             if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof (Nullable<>))             {                 return new NullableValueProvider(member, pi.PropertyType.GetGenericArguments().First());             }         }         else if(member.MemberType == MemberTypes.Field)         {             var fi = (FieldInfo) member;             if(fi.FieldType.IsGenericType && fi.FieldType.GetGenericTypeDefinition() == typeof(Nullable<>))                 return new NullableValueProvider(member, fi.FieldType.GetGenericArguments().First());         }          return base.CreateMemberValueProvider(member);     } } 

Then I tested it using:

class Foo {     public int? Int { get; set; }     public bool? Boolean { get; set; }     public int? IntField; } 

And the following case:

[TestFixture] public class Tests {     [Test]     public void Test()     {         var foo = new Foo();          var settings = new JsonSerializerSettings { ContractResolver = new SpecialContractResolver() };          Assert.AreEqual(             JsonConvert.SerializeObject(foo, Formatting.None, settings),              "{\"IntField\":0,\"Int\":0,\"Boolean\":false}");     } } 

Hopefully this helps a bit...

Edit – Better identification of the a Nullable<> type

Edit – Added support for fields as well as properties, also piggy-backing on top of the normal DynamicValueProvider to do most of the work, with updated test

like image 175
J. Holmes Avatar answered Sep 29 '22 20:09

J. Holmes