Exemplo n.º 1
0
        /// <summary>
        /// Read a JSON value (presumes the key has already been read) and set the
        /// underlying value. There's generally no reason to call this method
        /// directly. It is intended to be overridden by an extended type.
        /// </summary>
        /// <param name="path">path to the value being read</param>
        /// <param name="pbr">source reader</param>
        /// <exception cref="Gavaghan.JSON.JSONException">on grammar error</exception>
        /// <exception cref="System.IO.IOException">on read failure</exception>
        public override void Read(string path, PushbackReader pbr)
        {
            char c = JSONValueFactory.Demand(pbr);

            if (c != '[')
            {
                throw new JSONException(path, "Content does not appear to be an array.");
            }

            // empty array is an easy out
            mFactory.SkipWhitespace(pbr);
            c = JSONValueFactory.Demand(pbr);
            if (c == ']')
            {
                return;
            }
            pbr.Unread(c);

            // loop through values
            try
            {
                for (; ;)
                {
                    IJSONValue value = mFactory.Read(path, pbr);
                    mValue.Add(value);

                    // get next non-whitespace
                    mFactory.SkipWhitespace(pbr);
                    c = JSONValueFactory.Demand(pbr);

                    // is end?
                    if (c == ']')
                    {
                        return;
                    }

                    // is more
                    if (c == ',')
                    {
                        mFactory.SkipWhitespace(pbr);
                        continue;
                    }

                    throw new JSONException(path, "Incorrectly formatted array: " + c);
                }
            }
            finally
            {
                mFactory = null;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read a JSON value (presumes the key has already been read) and set the
        /// underlying value. There's generally no reason to call this method
        /// directly. It is intended to be overridden by an extended type.
        /// </summary>
        /// <param name="path">path to the value being read</param>
        /// <param name="pbr">source reader</param>
        /// <exception cref="Gavaghan.JSON.JSONException">on grammar error</exception>
        /// <exception cref="System.IO.IOException">on read failure</exception>
        public virtual void Read(string path, PushbackReader pbr)
        {
            // assert we have an opening brace
            char c = JSONValueFactory.Demand(pbr);

            if (c != '{')
            {
                throw new JSONException(path, "Failed to find '{' at start of JSON object.");
            }

            for (; ;)
            {
                string key;

                // next is either a key or a closing brace
                mFactory.SkipWhitespace(pbr);
                c = JSONValueFactory.Demand(pbr);

                // is it a string?
                if (c == '\"')
                {
                    pbr.Unread(c);
                    key = JSONString.ReadString(path, pbr);
                }
                // is it a closing brace?
                else if (c == '}')
                {
                    break;
                }
                // else, it's poorly formed
                else
                {
                    throw new JSONException(path, "JSON object is not grammatically correct.  Unexpected: " + c);
                }

                // next ought to be a colon
                mFactory.SkipWhitespace(pbr);
                c = JSONValueFactory.Demand(pbr);
                if (c != ':')
                {
                    throw new JSONException(path + "." + key, "Expected ':' after key value");
                }
                mFactory.SkipWhitespace(pbr);

                // next, read a JSONValue
                IJSONValue value = mFactory.Read(path + "." + key, pbr);

                // add it to the map
                Add(key, value);

                // next must be comma or close
                mFactory.SkipWhitespace(pbr);
                c = JSONValueFactory.Demand(pbr);

                if (c == ',')
                {
                    continue;
                }
                if (c == '}')
                {
                    break;
                }

                throw new JSONException(path, "JSON object is not grammatically correct.  Unexpected: " + c);
            }

            mFactory = null;
        }