Exemplo n.º 1
0
 /// <summary>
 /// Put a value into this Json array
 /// </summary>
 /// <param name="o">The value to add to this Json array</param>
 /// <returns>This Json array</returns>
 public JSONArray Put(int index, JSONArray o)
 {
     items.Insert(index, o);
     return(this);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Maps name to value, clobbering any existing name/value mapping with the same name
 /// </summary>
 /// <param name="name">The field name for the mapping</param>
 /// <param name="value">The value to be mapped to the field name</param>
 /// <returns>This json object with the mapping added</returns>
 public JSONObject Put(string name, JSONArray value)
 {
     items[name] = value;
     return(this);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Put a value into this Json array
 /// </summary>
 /// <param name="o">The value to add to this Json array</param>
 /// <returns>This Json array</returns>
 public JSONArray Put(JSONArray o)
 {
     items.Add(o);
     return(this);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Private method for parsing a Json string into a Json array
        /// </summary>
        /// <param name="json">The Json string to parse into a Json array</param>
        /// <returns>The parsed Json array after deserializing the Json string</returns>
        private JSONArray ParseJSONArray(string json)
        {
            JSONArray jo = new JSONArray();

            int  squareBracketCounter = 0;
            int  curlyBracketCounter  = 0;
            bool hasEnteredArray      = false;

            bool inLiteral          = false;
            bool inNestedJsonObject = false;

            bool escapingCharacter = false;

            bool buildingPrimitiveValue = false;
            bool expectingValue         = false;

            StringBuilder buffer = new StringBuilder();

            for (int i = 0; i < json.Length; i++)
            {
                char c = json[i];
                if (inLiteral && !(!escapingCharacter && c == '"'))
                {
                    //Handle escape characters in string literals
                    if (inLiteral && c == '\\')
                    {
                        if (escapingCharacter)
                        {
                            escapingCharacter = false;
                            buffer.Append('\\');
                        }
                        else
                        {
                            escapingCharacter = true;
                        }
                    }
                    else if (c != '"')
                    {
                        //Look for the next character in an escape character if we are escaping
                        if (escapingCharacter)
                        {
                            if (c == 'b')
                            {
                                buffer.Append('\b');
                            }
                            else if (c == 'f')
                            {
                                buffer.Append('\f');
                            }
                            else if (c == 'n')
                            {
                                buffer.Append('\n');
                            }
                            else if (c == 'r')
                            {
                                buffer.Append('\r');
                            }
                            else if (c == 't')
                            {
                                buffer.Append('\t');
                            }
                            else if (c == '"')
                            {
                                buffer.Append('\"');
                            }
                            else
                            {
                                //We were escaping a character but this isn't a valid one
                                throw new MalformedJsonException();
                            }
                            escapingCharacter = false;
                        }
                        else
                        {
                            //Add to the buffer if inside a literal
                            buffer.Append(c);
                        }
                    }
                    else if (c == '"' && escapingCharacter && inLiteral)
                    {
                        buffer.Append('"'); escapingCharacter = false;
                    }
                }
                else if (inNestedJsonObject && c != '}')
                {
                    //Add to the buffer if inside a nested Json object
                    buffer.Append(c);
                }
                else if (char.IsWhiteSpace(c))
                { //Whitespace characters
                    if (buildingPrimitiveValue)
                    {
                        if (buffer.Length > 0)
                        {
                            jo.Put(ParseValue(buffer.ToString()));
                            buffer.Length          = 0;
                            buildingPrimitiveValue = false;
                            expectingValue         = false;
                        }
                    }
                }
                else if (c == '[')
                {
                    squareBracketCounter++;
                    if (squareBracketCounter == 1 && hasEnteredArray)
                    {
                        throw new MalformedJsonException();
                    }
                    hasEnteredArray = true;
                    expectingValue  = true;
                }
                else if (c == ']')
                {
                    squareBracketCounter--;
                    if (squareBracketCounter < 0)
                    {
                        throw new MalformedJsonException();
                    }
                    else if (squareBracketCounter == 0)
                    {
                        //We weren't in an object and a bracket appeared
                        //The object should be ending in this case and this is the final value
                        if (buffer.Length > 0)
                        {
                            jo.Put(ParseValue(buffer.ToString()));
                            buffer.Length = 0;
                        }
                        expectingValue = false;
                    }
                }
                else if (c == '{')
                {
                    curlyBracketCounter++;
                    if (curlyBracketCounter > 0)
                    {
                        inNestedJsonObject = true;
                        buffer.Append(c);
                    }
                }
                else if (c == '}')
                {
                    curlyBracketCounter--;
                    if (curlyBracketCounter < 0)
                    {
                        throw new MalformedJsonException();
                    }
                    else if (curlyBracketCounter == 0)
                    {
                        if (inNestedJsonObject)
                        {
                            inNestedJsonObject = false;
                            buffer.Append(c);

                            //Parse the inner object and add it
                            jo.Put(new JSONObject(buffer.ToString()));
                            buffer.Length = 0;
                        }
                    }
                }
                else if (c == '"')
                { //Here we either just started or ended a literal
                    inLiteral = !inLiteral;
                    //If we're looking for a name and we just exited a literal
                    if (!inLiteral) //If we just ended a literal...
                    {
                        //We were expecting a value so we must have a string literal
                        jo.Put(buffer.ToString()); buffer.Length = 0;
                    }
                }
                else if (c == ',')
                {
                    //This would mean we didn't enclose a literal and are looking for a value
                    //The buffer should be a value
                    if (buffer.Length > 0)
                    {
                        jo.Put(ParseValue(buffer.ToString()));
                        buffer.Length  = 0;
                        expectingValue = true;
                    }
                }
                else
                {
                    //Only accept other characters if in an object and looking for a value
                    if (hasEnteredArray && expectingValue)
                    {
                        buffer.Append(c);
                        buildingPrimitiveValue = true;
                    }
                    else
                    {
                        throw new MalformedJsonException();
                    }
                }
            }

            //parse complete
            return(jo);
        }