Esempio n. 1
0
        private JsonBufferReader(JsonBuffer buffer, int index)
        {
            if (buffer.IsEmpty)
                throw new ArgumentException("buffer");

            Debug.Assert(index >= -1);
            Debug.Assert(index < buffer.Length);

            _buffer = buffer;
            _index = index;
        }
Esempio n. 2
0
        public NamedJsonBuffer(string name, JsonBuffer buffer)
        {
            if (name == null) 
                throw new ArgumentNullException("name");

            if (buffer.IsEmpty)
                throw new ArgumentException(null, "buffer");

            _name = Mask.NullString(name);
            _buffer = buffer;
        }
Esempio n. 3
0
        static JsonBuffer()
        {
            JsonBuffer buffer = new JsonBufferStorage(5) // [null,true,false]
                .Write(JsonToken.Array())
                    .Write(JsonToken.Null(), JsonToken.True(), JsonToken.False())
                .Write(JsonToken.EndArray())
                .ToBuffer();

            _null = buffer.Slice(1, 2);
            _true = buffer.Slice(2, 3);
            _false = buffer.Slice(3, 4);
        }
Esempio n. 4
0
 public JsonBuffer[] GetArray()
 {
     JsonBuffer[] values = new JsonBuffer[GetArrayLength()];
     GetArray(values);
     return values;
 }
Esempio n. 5
0
 public void StringRepresentationForFalse()
 {
     Assert.AreEqual("false", JsonBuffer.From(JsonToken.False()).ToString());
 }
Esempio n. 6
0
 public void StringRepresentationForNumber()
 {
     Assert.AreEqual("42", JsonBuffer.From(JsonToken.Number("42")).ToString());
 }
Esempio n. 7
0
 public void StringRepresentationForNull()
 {
     Assert.AreEqual("null", JsonBuffer.From(JsonToken.Null()).ToString());
 }
Esempio n. 8
0
 public void OpInequality()
 {
     Assert.IsTrue(JsonBuffer.From("[1,2,three,4]") != JsonBuffer.From("[1,2,THREE,4]"));
 }
Esempio n. 9
0
 public void GetArrayLength()
 {
     Assert.AreEqual(3, JsonBuffer.From("[1,2,3]").GetArrayLength());
 }
Esempio n. 10
0
 public void GetArrayShort()
 {
     JsonBuffer[] values = new JsonBuffer[2];
     int count = JsonBuffer.From("[1,2,3]").GetArray(values);
     Assert.AreEqual(~2, count);
     Assert.AreEqual(1, (int)values[0].GetNumber());
     Assert.AreEqual(2, (int)values[1].GetNumber());
 }
Esempio n. 11
0
 public void GetArrayLong()
 {
     JsonBuffer[] values = new JsonBuffer[4];
     int count = JsonBuffer.From("[1,2,3]").GetArray(values);
     Assert.AreEqual(3, count);
     Assert.AreEqual(1, (int)values[0].GetNumber());
     Assert.AreEqual(2, (int)values[1].GetNumber());
     Assert.AreEqual(3, (int)values[2].GetNumber());
     Assert.IsTrue(values[3].IsEmpty);
 }
Esempio n. 12
0
 public JsonBufferReader(JsonBuffer buffer) :
     this(buffer, -1)
 {
 }
            protected override JsonToken ReadTokenImpl()
            {
                JsonReader baseReader = _reader;

                int index = _index;

                if (index < 0)
                {
                    //
                    // If the base reader was never started then do it now.
                    // Check for zero depth is the same as BOF.
                    //
                    if (baseReader.Depth == 0)
                    {
                        baseReader.ReadToken(JsonTokenClass.Object);
                        _innerDepth = baseReader.Depth;
                    }

                    _index = 0;
                    return(JsonToken.Object());
                }

                //
                // Buffered members, if any, get served first.
                //

                NamedJsonBufferList bufferedMembers = _bufferedMembers;

                if (bufferedMembers != null && index < bufferedMembers.Count)
                {
                    //
                    // Is there a value to serve?
                    //

                    JsonBuffer value = _memberValue;
                    if (!value.IsEmpty)
                    {
                        if (value.IsStructured) // JSON Array or Object
                        {
                            //
                            // Get a reader on the structured (array or
                            // object) value if not already acquired.
                            //

                            JsonReader valueReader = _memberStructuredValueReader;
                            if (valueReader == null)
                            {
                                valueReader = _memberStructuredValueReader = value.CreateReader();
                            }

                            //
                            // Serve tokens from the value reader until OEF.
                            //

                            if (valueReader.Read())
                            {
                                return(valueReader.Token);
                            }

                            //
                            // Proceed with next.
                            //

                            _memberValue = JsonBuffer.Empty;
                            _memberStructuredValueReader = null;
                            _index++;
                            return(ReadTokenImpl());
                        }
                        else // JSON Null, Boolean, Number or String
                        {
                            Debug.Assert(value.IsNull || value.IsScalar);

                            //
                            // Establish state to proceed with next and
                            // serve token pertaining to scalar value.
                            //

                            _memberValue = JsonBuffer.Empty;
                            _index++;

                            return(value.IsNull
                                       ? JsonToken.Null()
                                       : value.CreateReader().Token);
                        }
                    }

                    //
                    // Serve buffered member name and establish state so
                    // that the value will be served next.
                    //

                    NamedJsonBuffer member = (NamedJsonBuffer)bufferedMembers[index];
                    _memberValue = member.Buffer;
                    return(JsonToken.Member(member.Name));
                }

                //
                // Done with buffered members so now move on to remaining
                // members from the underlying reader.
                //

                if (baseReader == null)
                {
                    return(JsonToken.EOF()); // Done with serving
                }
                //
                // If base reader is on the object end at the same depth as
                // on entry then serve the last end token. The reader is
                // released so that on next read, the above will signal EOF.
                //

                if (baseReader.Depth == _innerDepth &&
                    baseReader.TokenClass == JsonTokenClass.EndObject)
                {
                    _reader = null;
                }

                //
                // Move through the base reader.
                //

                JsonToken token = baseReader.Token;

                baseReader.Read();
                return(token);
            }
        /// <summary>
        /// Attempts to locate a member with a given (case-sensitive) name
        /// and returns a <see cref="JsonReader"/> that can be used to read
        /// the value. Otherwise it returns <c>null</c>.
        /// </summary>
        /// <remarks>
        /// The caller should not use the returned <see cref="JsonReader"/>
        /// instance for any other purpose but reading the value. It is
        /// possible that this method will return the same instance as
        /// <see cref="BaseReader"/> or a separate instance.
        /// </remarks>

        public JsonReader TryReadMember(string name)
        {
            //
            // Is the member already buffered? If yes then return a reader
            // on its buffered value.
            //

            JsonBuffer value = TryPopBufferedMember(name);

            if (!value.IsEmpty)
            {
                return(value.CreateReader());
            }

            //
            // Use the base reader from here on if it has not already been
            // exhausted...
            //

            if (_ended)
            {
                return(null);
            }

            JsonReader reader = BaseReader;

            if (!_started)
            {
                _started = true;
                if (!reader.MoveToContent())
                {
                    throw new JsonException(string.Format(
                                                @"Unexpected EOF while attempting to look for member named '{0}'.",
                                                name));
                }

                JsonTokenClass clazz = reader.TokenClass;
                if (clazz != JsonTokenClass.Object &&
                    clazz != JsonTokenClass.Member)
                {
                    throw new JsonException(string.Format(
                                                @"Found {0} where a JSON Object or Member was expected.", clazz));
                }
            }

            //
            // If the base reader is sitting on the start of an object then
            // move into it. This case should only arise on the first read
            // into a JSON object.
            //

            if (reader.TokenClass == JsonTokenClass.Object)
            {
                reader.Read();
            }

            //
            // Go over the entire JSON object until its end.
            //

            while (reader.TokenClass != JsonTokenClass.EndObject)
            {
                //
                // Read the next member and if it matches what's being
                // sought then simply return the base reader that
                // should be aligned on the value.
                //

                string actualName = reader.ReadMember();
                if (string.CompareOrdinal(actualName, name) == 0)
                {
                    return(reader);
                }

                //
                // Not the sought member so buffer it to be served
                // later when it is sought or as part of the tail.
                //

                NamedJsonBufferList members = _members;
                if (members == null)
                {
                    members = _members = new NamedJsonBufferList(4);
                }
                members.Add(new NamedJsonBuffer(actualName, JsonBuffer.From(reader)));
            }

            _ended = true;

            //
            // Member not found.
            //

            return(null);
        }
Esempio n. 15
0
 public void CannotBeInitializedWithNullName()
 {
     new NamedJsonBuffer(null, JsonBuffer.From("null"));
 }
Esempio n. 16
0
        public int GetArray(JsonBuffer[] values, int index, int count)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException("index", index, null);

            JsonBufferReader reader = CreateReader();

            if (!reader.MoveToContent())
                throw new JsonException("Unexpected EOF.");

            if (reader.TokenClass == JsonTokenClass.Null)
                return 0;

            reader.ReadToken(JsonTokenClass.Array);
            int readCount = 0;

            while (reader.TokenClass != JsonTokenClass.EndArray)
            {
                if (count-- == 0)
                    return ~readCount;

                if (values != null)
                    values[index++] = reader.BufferValue();
                else
                    reader.Skip();

                readCount++;
            }

            return readCount;
        }
Esempio n. 17
0
 public void AnonymousToString()
 {
     Assert.AreEqual("(anonymous): \"foo\"",
                     new NamedJsonBuffer(string.Empty, JsonBuffer.From("foo")).ToString());
 }
Esempio n. 18
0
 public void GetArrayLengthOnNull()
 {
     Assert.AreEqual(0, JsonBuffer.From("[null]").GetArray()[0].GetArrayLength());
 }
Esempio n. 19
0
        public void StringRepresentation()
        {
            string str = JsonBuffer.From("[42,foobar,null,true,false,{x:123,y:456}]").ToString();

            Assert.AreEqual("[42,\"foobar\",null,true,false,{\"x\":123,\"y\":456}]", str);
        }
Esempio n. 20
0
 public void GetMemberCount()
 {
     Assert.AreEqual(3, JsonBuffer.From("{a:1,b:2,c:3}").GetMemberCount());
 }
Esempio n. 21
0
 public void StringRepresentationForString()
 {
     Assert.AreEqual("\"foo\\nbar\"", JsonBuffer.From(JsonToken.String("foo\nbar")).ToString());
 }
Esempio n. 22
0
 public void GetMembersIntoArrayPermitsNull()
 {
     Assert.AreEqual(3, JsonBuffer.From("{a:1,b:2,c:3}").GetMembers(null));
 }
Esempio n. 23
0
 public void StringRepresentationForTrue()
 {
     Assert.AreEqual("true", JsonBuffer.From(JsonToken.True()).ToString());
 }
Esempio n. 24
0
 public void GetMembersCountOnNull()
 {
     Assert.AreEqual(0, JsonBuffer.From("[null]").GetArray()[0].GetMemberCount());
 }
Esempio n. 25
0
 public JsonBufferReader(JsonBuffer buffer)
     : this(buffer, -1)
 {
 }
Esempio n. 26
0
 public void IsObject()
 {
     Assert.IsTrue(JsonBuffer.From("{}").IsObject);
 }
Esempio n. 27
0
            protected override JsonToken ReadTokenImpl()
            {
                JsonReader baseReader = _reader;

                int index = _index;
                if (index < 0)
                {
                    //
                    // If the base reader was never started then do it now.
                    // Check for zero depth is the same as BOF.
                    //
                    if (baseReader.Depth == 0)
                    {
                        baseReader.ReadToken(JsonTokenClass.Object);
                        _innerDepth = baseReader.Depth;
                    }

                    _index = 0;
                    return JsonToken.Object();
                }

                //
                // Buffered members, if any, get served first.
                //

                NamedJsonBufferList bufferedMembers = _bufferedMembers;
                if (bufferedMembers != null && index < bufferedMembers.Count)
                {
                    //
                    // Is there a value to serve?
                    //

                    JsonBuffer value = _memberValue;
                    if (!value.IsEmpty)
                    {
                        if (value.IsStructured) // JSON Array or Object
                        {
                            //
                            // Get a reader on the structured (array or 
                            // object) value if not already acquired.
                            //

                            JsonReader valueReader = _memberStructuredValueReader;
                            if (valueReader == null)
                                valueReader = _memberStructuredValueReader = value.CreateReader();

                            //
                            // Serve tokens from the value reader until OEF.
                            //

                            if (valueReader.Read())
                                return valueReader.Token;

                            //
                            // Proceed with next.
                            //

                            _memberValue = JsonBuffer.Empty;
                            _memberStructuredValueReader = null;
                            _index++;
                            return ReadTokenImpl();
                        }
                        else // JSON Null, Boolean, Number or String
                        {
                            Debug.Assert(value.IsNull || value.IsScalar);

                            //
                            // Establish state to proceed with next and 
                            // serve token pertaining to scalar value.
                            //

                            _memberValue = JsonBuffer.Empty;
                            _index++;
                            
                            return value.IsNull 
                                       ? JsonToken.Null() 
                                       : value.CreateReader().Token;
                        }
                    }

                    //
                    // Serve buffered member name and establish state so 
                    // that the value will be served next.
                    //

                    NamedJsonBuffer member = (NamedJsonBuffer) bufferedMembers[index];
                    _memberValue = member.Buffer;
                    return JsonToken.Member(member.Name);
                }

                //
                // Done with buffered members so now move on to remaining
                // members from the underlying reader.
                //

                if (baseReader == null)
                    return JsonToken.EOF(); // Done with serving

                //
                // If base reader is on the object end at the same depth as
                // on entry then serve the last end token. The reader is
                // released so that on next read, the above will signal EOF.
                //

                if (baseReader.Depth == _innerDepth 
                    && baseReader.TokenClass == JsonTokenClass.EndObject)
                {
                    _reader = null;
                }

                //
                // Move through the base reader.
                //

                JsonToken token = baseReader.Token;
                baseReader.Read();
                return token;
            }
Esempio n. 28
0
 public void IsArray()
 {
     Assert.IsTrue(JsonBuffer.From("[]").IsArray);
 }
Esempio n. 29
0
        public bool Equals(JsonBuffer other)
        {
            if (Length != other.Length)
                return false;

            for (int i = 0; i < Length; i++)
            {
                if (!this[i].Equals(other[i]))
                    return false;
            }

            return true;
        }
Esempio n. 30
0
 public void Equality()
 {
     Assert.AreEqual(JsonBuffer.From("[1,2,3]"), JsonBuffer.From("[1,2,3]"));
 }
Esempio n. 31
0
 public int GetArray(JsonBuffer[] values)
 {
     return GetArray(values, 0, values.Length);
 }
Esempio n. 32
0
 public void Inequality()
 {
     Assert.AreNotEqual(JsonBuffer.From("[1,2,three,4]"), JsonBuffer.From("[1,2,THREE,4]"));
 }
Esempio n. 33
0
 public void OpEquality()
 {
     Assert.IsTrue(JsonBuffer.From("[1,2,3]") == JsonBuffer.From("[1,2,3]"));
 }
Esempio n. 34
0
 public            JsonBuffer[] GetArray()
 {
     JsonBuffer[] values = new JsonBuffer[GetArrayLength()];
     GetArray(values);
     return(values);
 }