Beispiel #1
0
 public override bool Equals(JsonValue other)
 {
     return(Equals((object)other));
 }
        private JsonArray ReadArray(ref char *ptr, ref char *end)
        {
            // check first letter
            Debug.Assert(*ptr == '[');
            ptr++;

            // check array is empty
            SkipWhitespaces(ref ptr, ref end);
            if (IsEndOfJson(ref ptr, ref end))
            {
                // not completed
                throw CreateException(ptr, "array is not closed.");
            }
            if (*ptr == ']')
            {
                ptr++;
                return(JsonArray.Empty);
            }

            // first stage: for smaller arrays

            var smallArray = new JsonValue[SmallArrayLength];

            for (var index = 0; index < smallArray.Length; index++)
            {
                smallArray[index] = ReadValue(ref ptr, ref end);

                // read close bracket or comma
                SkipWhitespaces(ref ptr, ref end);

                if (IsEndOfJson(ref ptr, ref end))
                {
                    // not completed
                    throw CreateException(ptr, "array is not closed.");
                }

                // if end of array, next letter should be ']'.
                if (*ptr == ']')
                {
                    // end of array
                    ptr++;
                    return(new JsonArray(smallArray, index + 1));
                }

                // otherwise, next letter should be ','.
                AssertAndReadNext(ref ptr, ',');
            }

            // second stage: for longer arrays

            // initialize with contents already read
            List <JsonValue> items = new List <JsonValue>(smallArray);

            while (true)
            {
                items.Add(ReadValue(ref ptr, ref end));

                // read close bracket or comma
                SkipWhitespaces(ref ptr, ref end);

                if (IsEndOfJson(ref ptr, ref end))
                {
                    // not completed
                    throw CreateException(ptr, "array is not closed.");
                }

                // if end of array, next letter should be ']'.
                if (*ptr == ']')
                {
                    // end of array
                    ptr++;
                    return(new JsonArray(items.ToArray()));
                }

                // otherwise, next letter should be ','.
                AssertAndReadNext(ref ptr, ',');
            }
        }