Пример #1
0
        /**
         * Appends {@code value} to the array already mapped to {@code name}. If
         * this object has no mapping for {@code name}, this inserts a new mapping.
         * If the mapping exists but its value is not an array, the existing
         * and new values are inserted in order into a new array which is itself
         * mapped to {@code name}. In aggregate, this allows values to be added to a
         * mapping one at a time.
         *
         * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
         *     Integer, Long, Double, {@link #NULL} or null. May not be {@link
         *     Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.
         */
        public JsonObject Accumulate(string name, object value)
        {
            object current = nameValuePairs[checkName(name)];

            if (current == null)
            {
                return(Put(name, value));
            }

            // check in accumulate, since array.put(Object) doesn't do any checking
            if (Json.IsNumber(value))
            {
                Json.CheckDouble((double)value);
            }

            if (current is JsonArray)
            {
                JsonArray array = (JsonArray)current;
                array.Put(value);
            }
            else
            {
                JsonArray array = new JsonArray();
                array.Put(current);
                array.Put(value);
                nameValuePairs[name] = array;
            }
            return(this);
        }
Пример #2
0
        /**
         * Reads a sequence of values and the trailing closing brace ']' of an
         * array. The opening brace '[' should have already been read. Note that
         * "[]" yields an empty array, but "[,]" returns a two-element array
         * equivalent to "[null,null]".
         */
        private JsonArray ReadArray()
        {
            JsonArray result = new JsonArray();

            /* to cover input that ends with ",]". */
            bool hasTrailingSeparator = false;

            while (true)
            {
                switch (NextCleanInternal())
                {
                case -1:
                    throw SyntaxError("Unterminated array");

                case ']':
                    if (hasTrailingSeparator)
                    {
                        result.Put(null);
                    }
                    return(result);

                case ',':
                    goto case ';';

                case ';':
                    /* A separator without a value first means "null". */
                    result.Put(null);
                    hasTrailingSeparator = true;
                    continue;

                default:
                    pos--;
                    break;
                }

                result.Put(NextValue());

                switch (NextCleanInternal())
                {
                case ']':
                    return(result);

                case ',':
                    goto case ';';

                case ';':
                    hasTrailingSeparator = true;
                    continue;

                default:
                    throw SyntaxError("Unterminated array");
                }
            }
        }
Пример #3
0
        public void Test_0039_set_JSONArray_hasValue()
        {
            JsonObject json  = new JsonObject();
            JsonArray  inner = new JsonArray();

            inner.Put(1234);
            json.Put("ids", inner);
            Assert.AreEqual("{\"ids\":[1234]}", json.ToString());
            Assert.AreEqual("[1234]", json.GetJsonArray("ids").ToString());
        }
Пример #4
0
        /**
         * Returns an array with the values corresponding to {@code names}. The
         * array contains null for names that aren't mapped. This method returns
         * null if {@code names} is either null or empty.
         */
        public JsonArray ToJSONArray(JsonArray names)
        {
            JsonArray result = new JsonArray();

            if (names == null)
            {
                return(null);
            }
            int length = names.Length();

            if (length == 0)
            {
                return(null);
            }
            for (int i = 0; i < length; i++)
            {
                string name = Json.ToString(names.Opt(i));
                result.Put(Opt(name));
            }
            return(result);
        }