How do I deal with blank strings in the json being sent for deserialization?

Feb 2, 2009 at 7:47 PM
At first I thought I could replace blank strings with null and use nullable types to convert the values to default values before assigning them to the properties however I am now getting an exception when trying to serialize my object before sending it to the client:

Newtonsoft.Json.JsonSerializationException: JsonConverter JavaScriptDateTimeConverter on System
.Nullable`1[System.DateTime] at
Newtonsoft.Json.Serialization.JsonTypeReflector.GetConverter(ICustomAttributeProvider
attributeProvider, Type targetConvertedType) in
d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\Serialization\JsonTypeReflector.cs:line
138\r\n at
Newtonsoft.Json.Serialization.JsonTypeReflector.CreateMemberMappings(Type
objectType) in
d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\Serialization\JsonTypeReflector.cs:line
100\r\n at
Newtonsoft.Json.Serialization.JsonTypeReflector.GetMemberMappings(Type
objectType) in
d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\Serialization\JsonTypeReflector.cs:line
73\r\n at Newtonsoft.Json.JsonSerializer.GetMemberMappings(Type
objectType) in
d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonSerializer.cs:line
368\r\n at Newtonsoft.Json.JsonSerializer.SerializeObject(JsonWriter
writer, Object value) in
d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonSerializer.cs:line
773\r\n at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter
writer, Object value, JsonConverter memberConverter) in
d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonSerializer.cs:line
674\r\n at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter
jsonWriter, Object value) in
d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonSerializer.cs:line
624\r\n at Newtonsoft.Json.JsonConvert.SerializeObject(Object value,
Formatting formatting, JsonSerializerSettings settings) in
d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonConvert.cs:line
497\r\n at Newtonsoft.Json.JsonConvert.SerializeObject(Object value) in
d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonConvert.cs:line
447\r\n at _Default.getDate() in X:\1275213 Alberta
Ltd\sideProjects\sandbox\dateTestCase\Default.aspx.vb:line 60


Using the following code:

         Private _now As Date
        <JsonConverter(GetType(JavaScriptDateTimeConverter))> Public Property now() As Nullable(Of Date)
            Get
                Return _now
            End Get
            Set(ByVal value As Nullable(Of Date))
                If value Is Nothing Then
                    _now = #1/1/1900# 'default value if null
                Else
                    _now = value
                End If
            End Set
        End Property