Serializing Nullable Types

Jun 2, 2008 at 9:38 PM
Is there any plan in the future for adding serializing of nullable types to the JsonWriter or JavaScriptConvert?

Also since I'm asking for new features I was just wondering why the JsonConverter is an abstract class instead of an interface? I know it would probably break too much code but it would be nice to have it be an interface.
Thanks.
Coordinator
Jun 8, 2008 at 8:59 AM
I guess overload nullable types is something that could be added. I think all it would be is a check to see if the value is null and return 'null' if it is, otherwise call the non-nullable overload value.

Why would you want JsonConverter to be an interface? Are you looking for an interface like IXmlSerializable for Json.NET?
Jun 9, 2008 at 1:50 AM
The nullable types would help. Thanks.

About the interface question, I have written several JsonConverters for some of my objects where serializing the whole object just became too big. For most of them it makes sense to have a different JsonConverter for each use case to keep the data transfer as small as possible, but in some cases I found that the object will only ever need to be serialized one way. In this case I was hoping to make the object itself be be the JsonConverter. If the JsonConverter was an interface instead of an abstract class I could just have the object's class implement the JsonConverter. I know I could just as easily add attributes to the class's properties to determine which ones should and shouldn't be serialized and maybe that's what I should do. The only reason that I brought it up was because I noticed that all of the JsonConverter's methods were empty and so converting it from an abstract class to an interface would be fairly simple and would make it more flexible. This was just any idea I had.

Thank you for responding and for your GREAT JSON library. It really is a time saver.
Sep 22, 2008 at 9:06 PM
I know I'm joining in late, but I'd really like to see nullable type support for the IsoDateTimeConverter in JSON.NET. We've used nullable datetimes throughout our data access layer.
Sep 22, 2008 at 10:24 PM
Edited Sep 22, 2008 at 10:27 PM
I figured out how to make the IsoDateTimeConverter support nullable DateTime properties, using a code snip from http://davidhayden.com/blog/dave/archive/2006/11/26/IsTypeNullableTypeConverter.aspx.

Change the overridden CanConvert() method to the following:

    public override bool CanConvert(Type objectType)
    {
        if (IsNullableType(objectType))
        {
            NullableConverter nc = new NullableConverter(objectType);
            objectType = nc.UnderlyingType;
        }  

        return (typeof(DateTime).IsAssignableFrom(objectType)
        || typeof(DateTimeOffset).IsAssignableFrom(objectType));
    }

    private static bool IsNullableType(Type theType)
    {
        return (theType.IsGenericType && theType.
          GetGenericTypeDefinition().Equals
          (typeof(Nullable<>)));
    }
Coordinator
Sep 24, 2008 at 9:42 AM
Added WriteValue overloads for nullable types and nullable DateTime support to IsoDateTimeConverter and JavaScriptDataTimeConverter.

Source code has been checked into CodePlex.
Oct 14, 2008 at 7:49 PM
James,

Any way to serialize Nullable Ints and booleans correctly with JSON.NET. I am getting errors going from Int64 -> Nullable<Int>. Do I need to use a Converter to make it work?
Coordinator
Oct 27, 2008 at 8:01 AM
Where are you going from Int64 to Nullable<Int32>? Could you give an example. What is the error message?
Oct 27, 2008 at 1:06 PM
James,

I have an example POCO class:
class User{
Guid ID;
string FName;
string LName;
Nullable<Int32> RoleID;
Nullable<Boolean> Active;
}

It converts this to JSON fine and makes RoleID a number and Active a boolean. However, going back to C# object is where I get the problem. It gives an error that it cannot convert Int64 to Nullable<Int32> and the same with boolean to Nullable<Boolean>.

Anything I am doing wrong? I noticed you have a new version today. Should I download and try that one out?
Coordinator
Nov 2, 2008 at 12:46 AM
Thanks for the example. I changed the serializer so that your example will now work. The change has been checked into CodePlex. Get the latest source from the Source Code tab.
Mar 25, 2010 at 5:26 PM

Hi James

I would like to do the following, but unfortunately it does not work:

string jsonText = JsonConvert.SerializeObject(new double?[] {2.4, 4.3});
double?[] d = (double?[])JsonConvert.DeserializeObject(jsonText, typeof(double?[]));

Anything I am doing wrong? Or is there any chance you could make this work as well?

Thanks
Philipp

Coordinator
Mar 28, 2010 at 10:36 PM

Hi

That was a bug. It should now be fixed in the latest version of the source code
http://json.codeplex.com/SourceControl/list/changesets

Mar 29, 2010 at 2:00 PM

Hi, Thanks very much for the quick fix.