1

I have this class as object to serialize:

public class JsonServerToClient
{
    public JsonServerToClient()
    {
        query = new List<Query>();
    }
    public String authKey { get; set; }
    public List<Query> query { get; set; }

    public struct Query
    {
        public int error { get; set; }
        public DataTable result { get; set; }
    }

}

I use JsonConvert.SerializeObject(objectAbove); to serialize JSON.

and JsonConvert.DeserializeObject<JsonServerToClient>(text); to deserialize it.

All works fine when the result is full of data, but if it is null I get this JSON:

{
    "authKey": "pippo",
    "query": [
        {
            "error": -1,
            "result": null
        }
    ]
}

The problem is when I try to deserialize this I get an exception:

Eccezione non gestita di tipo 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll

Ulteriori informazioni: Unexpected end when deserializing array. Path '', line 1, position 56.

3 Answers 3

1

UPDATE

This issue was fixed in Json.Net version 8.0.1. The workaround below is no longer needed.


It appears that the DataTableConverter that ships with Json.Net (as of v6.0.3) does not handle the case where the data table itself is null. You can work around this issue by subclassing the DataTableConverter and overriding the ReadJson method like this:

class CustomDataTableConverter : DataTableConverter
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
        {
            return null;
        }
        return base.ReadJson(reader, objectType, existingValue, serializer);
    }
}

When you deserialize, be sure to specify the custom converter:

var obj = JsonConvert.DeserializeObject<JsonServerToClient>(json, 
          new CustomDataTableConverter());
Sign up to request clarification or add additional context in comments.

Comments

0

Which version of Json.NET are you using? As per this post, it seems to be fixed with Json.NET 3.5r6

1 Comment

Runtime Version v4.0.30319
0

I have same issues using Json.Net to parse DateTime field from the json response. I tried the following Json Helper class and it handled the datetime field properly.public class JsonHelper { /// <summary> /// JSON Serialization /// </summary> public static string JsonSerializer<T>(T t) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); MemoryStream ms = new MemoryStream(); ser.WriteObject(ms, t); string jsonString = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return jsonString; } /// <summary> /// JSON Deserialization /// </summary> public static T JsonDeserialize<T>(string jsonString) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); T obj = (T)ser.ReadObject(ms); return obj; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.