1
Vote

Need to update the ReadString() method of the JSonClassHelper.cs

description

First of all, thank you very much for a such a wonderful library!

This Library I use mostly with the NewtonsoftJSon library. And with the recent release of the Newtonsoft JSON, the last line in the method throws a System.InvalidCastException.
    public static string ReadString(JToken token)
    {
        var value = GetJToken<JValue>(token);
        if (value == null) return null;
        return (string)value.Value;
    }
This happens not for all the strings. But strings that look like dates. For example if the JSON has something like "start_time": "2012-07-27T11:30:00" and when you try to read this, the NewtonSoft identifies the value.Value as DateTime and when you try to cast it to string, it throws the cast exception.

A simple solution that I have followed for this to work with the upgraded version of the newtonsoft json is to call the ToString() method.
    public static string ReadString(JToken token)
    {
        var value = GetJToken<JValue>(token);
        if (value == null) return null;
        return value.Value.ToString();
    }
Wanted to share this with others as well and probably if this makes sense, please do the change to the main helper.

Thanks again for such a neat library!
Well wisher,
Braga

comments