Beispiel #1
0
    /// <summary>
    /// Overwrites data with information from the provided JSON. This method will throw an Exception
    /// if the text cannot be split into exactly two pieces (left and right of an initial ':')
    /// </summary>
    public void FromString(string aText)
    {
        aText = aText.Trim();
        string[] words = Ferr_JSON._Split(aText, ':', true);

        if (words.Length != 2)
        {
            throw new Exception("Bad JSON Object: " + aText);
        }
        name = words[0].Trim().Replace("\"", "");
        val  = new Ferr_JSONValue();
        val.FromString(words[1]);
    }
Beispiel #2
0
    /// <summary>
    /// Recursively converts a JSON string into a JSON data structure. Throws an exception
    /// if given an empty string. Unrecognized data is converted into a string.
    /// </summary>
    public void FromString(string aValue)
    {
        string text = aValue.Trim();
        float  fVal = 0;

        if (text.Length <= 0)
        {
            throw new Exception("Bad JSON value: " + aValue);
        }

        if (text[0] == '{')
        {
            type = Ferr_JSONType.Object;
            text = text.Remove(0, 1);
            text = text.Remove(text.Length - 1, 1);
            string         [] items  = Ferr_JSON._Split(text, ',', true);
            Ferr_JSONObject[] result = new Ferr_JSONObject[items.Length];
            for (int i = 0; i < items.Length; i++)
            {
                result[i] = new Ferr_JSONObject(items[i]);
            }
            data = result;
        }
        else if (text[0] == '[')
        {
            type = Ferr_JSONType.Array;
            text = text.Remove(0, 1);
            text = text.Remove(text.Length - 1, 1);
            string        [] items  = Ferr_JSON._Split(text, ',', true);
            Ferr_JSONValue[] result = new Ferr_JSONValue[items.Length];
            for (int i = 0; i < items.Length; i++)
            {
                result[i] = new Ferr_JSONValue();
                result[i].FromString(items[i]);
            }
            data = result;
        }
        else if (text[0] == '\"')
        {
            type = Ferr_JSONType.String;
            text = text.Remove(0, 1);
            text = text.Remove(text.Length - 1, 1);
            data = text;
        }
        else if (float.TryParse(text, out fVal))
        {
            type = Ferr_JSONType.Number;
            data = fVal;
        }
        else
        {
            string lower = text.ToLower();
            if (lower == "false" || lower == "true")
            {
                type = Ferr_JSONType.Bool;
                data = bool.Parse(text);
            }
            else if (lower == "null")
            {
                type = Ferr_JSONType.Object;
                data = null;
            }
            else
            {
                type = Ferr_JSONType.String;
                data = text;
            }
        }
    }