Ejemplo n.º 1
0
        /// <summary> Construct a JSONArray from a JSONTokener.</summary>
        /// <param name="x">A JSONTokener
        /// </param>
        /// <throws>  JSONException If there is a syntax error. </throws>
        public JSONArray(JSONTokener x) : this()
        {
            char c = x.nextClean();
            char q;

            if (c == '[')
            {
                q = ']';
            }
            else if (c == '(')
            {
                q = ')';
            }
            else
            {
                throw x.syntaxError("A JSONArray text must start with '['");
            }
            if (x.nextClean() == ']')
            {
                return;
            }
            x.back();
            for (; ;)
            {
                if (x.nextClean() == ',')
                {
                    x.back();
                    this.myArrayList.Add(null);
                }
                else
                {
                    x.back();
                    this.myArrayList.Add(x.nextValue());
                }
                c = x.nextClean();
                switch (c)
                {
                case ';':
                case ',':
                    if (x.nextClean() == ']')
                    {
                        return;
                    }
                    x.back();
                    break;

                case ']':
                case ')':
                    if (q != c)
                    {
                        throw x.syntaxError("Expected a '" + q + "'");
                    }
                    return;

                default:
                    throw x.syntaxError("Expected a ',' or ']'");
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary> Construct a JSONObject from a JSONTokener.</summary>
        /// <param name="x">A JSONTokener object containing the source string.
        /// </param>
        /// <throws>  JSONException If there is a syntax error in the source string </throws>
        /// <summary>  or a duplicated key.
        /// </summary>
        public JSONObject(JSONTokener x)
            : this()
        {
            char c;
            System.String key;

            if (x.nextClean() != '{')
            {
                throw x.syntaxError("A JSONObject text must begin with '{'");
            }
            for (; ; )
            {
                c = x.nextClean();
                switch (c)
                {

                    case (char)(0):
                        throw x.syntaxError("A JSONObject text must end with '}'");

                    case '}':
                        return;

                    default:
                        x.back();
                        key = x.nextValue().ToString();
                        break;

                }

                /*
                * The key is followed by ':'. We will also tolerate '=' or '=>'.
                */

                c = x.nextClean();
                if (c == '=')
                {
                    if (x.next() != '>')
                    {
                        x.back();
                    }
                }
                else if (c != ':')
                {
                    throw x.syntaxError("Expected a ':' after a key");
                }
                putOnce(key, x.nextValue());

                /*
                * Pairs are separated by ','. We will also tolerate ';'.
                */

                switch (x.nextClean())
                {

                    case ';':
                    case ',':
                        if (x.nextClean() == '}')
                        {
                            return;
                        }
                        x.back();
                        break;

                    case '}':
                        return;

                    default:
                        throw x.syntaxError("Expected a ',' or '}'");

                }
            }
        }
Ejemplo n.º 3
0
        /// <summary> Construct a JSONArray from a JSONTokener.</summary>
        /// <param name="x">A JSONTokener
        /// </param>
        /// <throws>  JSONException If there is a syntax error. </throws>
        public JSONArray(JSONTokener x)
            : this()
        {
            char c = x.nextClean();
            char q;
            if (c == '[')
            {
                q = ']';
            }
            else if (c == '(')
            {
                q = ')';
            }
            else
            {
                throw x.syntaxError("A JSONArray text must start with '['");
            }
            if (x.nextClean() == ']')
            {
                return ;
            }
            x.back();
            for (; ; )
            {
                if (x.nextClean() == ',')
                {
                    x.back();
                    this.myArrayList.Add(null);
                }
                else
                {
                    x.back();
                    this.myArrayList.Add(x.nextValue());
                }
                c = x.nextClean();
                switch (c)
                {

                    case ';':
                    case ',':
                        if (x.nextClean() == ']')
                        {
                            return ;
                        }
                        x.back();
                        break;

                    case ']':
                    case ')':
                        if (q != c)
                        {
                            throw x.syntaxError("Expected a '" + q + "'");
                        }
                        return ;

                    default:
                        throw x.syntaxError("Expected a ',' or ']'");

                }
            }
        }