tomazsaraiva
7/31/2018 - 2:24 PM

JSON

public static class JSON
{
    public static int GetInt(JObject obj, string key)
    {
        if (obj == null)
        {
            return -1;
        }

        JToken token = obj[key];

        if (token != null)
        {
            if (token.Type == JTokenType.Integer)
            {
                return (int)token;
            }

            if (token.Type == JTokenType.String)
            {
                int result = -1;

                int.TryParse(token.ToString(), out result);

                return result;
            }
        }

        return -1;
    }

    public static long GetLong(JObject obj, string key)
    {
        if (obj == null)
        {
            return -1;
        }

        JToken token = obj[key];

        if (token != null)
        {
            if (token.Type == JTokenType.String)
            {
                long result = -1;

                long.TryParse(token.ToString(), out result);

                return result;
            }
        }

        return -1;
    }

    public static double GetDouble(JObject obj, string key)
    {
        if (obj == null)
        {
            return -1;
        }

        JToken token = obj[key];

        if (token != null)
        {
            if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer)
            {
                return (double)token;
            }

            if (token.Type == JTokenType.String && !string.IsNullOrEmpty(token.ToString()))
            {
                return token.Value<double>();
            }
        }

        return -1;
    }

    public static float GetFloat(JObject obj, string key)
    {
        if (obj == null)
        {
            return -1;
        }

        JToken token = obj[key];

        if (token != null)
        {
            if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer)
            {
                return (float)token;
            }

            if (token.Type == JTokenType.String)
            {
                float result = -1;

                float.TryParse(token.ToString(), out result);

                return result;
            }
        }

        return -1;
    }

    public static string GetString(JObject obj, string key)
    {
        if (obj == null)
        {
            return null;
        }

        JToken token = obj[key];

        if (token != null && token.Type == JTokenType.String)
        {
            return token.ToString();
        }

        return null;
    }

    public static bool GetBool(JObject obj, string key)
    {
        if (obj == null)
        {
            return false;
        }

        JToken token = obj[key];

        if (token != null && token.Type == JTokenType.Boolean)
        {
            return token.Value<bool>();
        }

        return false;
    }

    public static DateTime GetDate(JObject obj, string key)
    {
        DateTime date = DateTime.Now;

        if (obj == null)
        {
            return date;
        }

        JToken token = obj[key];

        if (token != null && token.Type == JTokenType.Date)
        {
            DateTime.TryParse(token.ToString(), out date);
        }

        return date;
    }

    public static JObject GetObject(JObject obj, string key)
    {
        if (obj == null)
        {
            return null;
        }

        JToken token = obj[key];

        if (token != null && token.Type == JTokenType.Object)
        {
            return (JObject)token;
        }

        return null;
    }

    public static JArray GetArray(JObject obj, string key)
    {
        if (obj == null)
        {
            return null;
        }

        JToken token = obj[key];

        if (token != null && token.Type == JTokenType.Array)
        {
            return (JArray)token;
        }

        return null;
    }
}