public JsonTextReader(TextReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            _parser = new TextParser(reader.ReadToEnd());
            _token = JsonToken.BOF;
            _stack = new Stack();
            _stack.Push(ParseMethod);
        }
Exemple #2
0
        public JsonReader CreatePlayer()
        {
            int count = _tokenList == null ? 0 : _tokenList.Count;

            JsonToken[] tokens = new JsonToken[count + 2];

            if (count > 0)
                _tokenList.CopyTo(tokens, 1);

            tokens[0] = JsonToken.BOF();
            tokens[tokens.Length - 1] = JsonToken.EOF();

            return new JsonPlayer(tokens);
        }
Exemple #3
0
        private void Write(JsonToken token)
        {
            if (_tokens == null)
            {
                _tokens = new JsonToken[16];
            }
            else if (_count == _tokens.Length)
            {
                JsonToken[] tokens = new JsonToken[_tokens.Length * 2];
                _tokens.CopyTo(tokens, 0);
                _tokens = tokens;
            }

            _tokens[_count++] = token;
        }
        /// <summary>
        /// Reads the next token and returns true if one was found.
        /// </summary>

        public sealed override bool Read()
        {
            if (!EOF)
            {
                if (TokenClass == JsonTokenClass.EndObject || TokenClass == JsonTokenClass.EndArray)
                    _depth--;

                _token = ReadTokenImpl();

                if (TokenClass == JsonTokenClass.Object || TokenClass == JsonTokenClass.Array)
                    _depth++;
            }
            
            return !EOF;
        }
        public JsonBufferStorage Write(JsonToken token)
        {
            if (_tokens == null)
            {
                _tokens = new JsonToken[16];
            }
            else if (_count == _tokens.Length)
            {
                JsonToken[] tokens = new JsonToken[_tokens.Length * 2];
                _tokens.CopyTo(tokens, 0);
                _tokens = tokens;
            }

            _tokens[_count++] = token;
            return this;
        }
Exemple #6
0
        public static JsonBuffer From(JsonToken token)
        {
            JsonTokenClass clazz = token.Class;
            
            if (clazz == JsonTokenClass.Null)
                return _null;

            if (!clazz.IsScalar)
                throw new ArgumentException("Token must represent a JSON scalar value or null.", "token");

            if (clazz == JsonTokenClass.Boolean)
                return token.Equals(JsonToken.True()) ? _true : _false;

            JsonBufferStorage storage = new JsonBufferStorage(1);
            storage.Write(token);
            return storage.ToBuffer();
        }
Exemple #7
0
 protected override void Write(JsonToken token)
 {
     _storage.Write(token);
 }
Exemple #8
0
 public bool Equals(JsonToken other)
 {
     return(Class.Equals(other.Class) && (Text == null || Text.Equals(other.Text)));
 }
Exemple #9
0
 public bool Equals(JsonToken other)
 {
     return Class.Equals(other.Class) && (Text == null || Text.Equals(other.Text));
 }
Exemple #10
0
 public MockedJsonReader End()
 {
     return(Append(JsonToken.EOF()));
 }
 private MockedJsonReader Append(JsonToken token)
 {
     _queue.Enqueue(token);
     return this;
 }
Exemple #12
0
        /// <summary>
        /// Reads the next token and returns true if one was found.
        /// </summary>

        public sealed override bool Read()
        {
            if (!EOF)
            {
                if (Depth > MaxDepth)
                    throw new Exception("Maximum allowed depth has been exceeded.");

                if (TokenClass == JsonTokenClass.EndObject || TokenClass == JsonTokenClass.EndArray)
                    _depth--;

                _token = ReadTokenImpl();

                if (TokenClass == JsonTokenClass.Object || TokenClass == JsonTokenClass.Array)
                    _depth++;
            }
            
            return !EOF;
        }
        /// <summary>
        /// Reads the next token ensuring that it matches the specified 
        /// token. If not, an exception is thrown.
        /// </summary>

        public void ReadToken(JsonToken token)
        {
            if (ReadToken() != token)
                throw new JsonException(string.Format("Found {0} where {1} was expected.", Token.ToString(), token));
        }
Exemple #14
0
 public void ArrayTokens()
 {
     Assert.AreEqual(JsonTokenClass.Array, JsonToken.Array().Class);
     Assert.AreEqual(JsonTokenClass.EndArray, JsonToken.EndArray().Class);
 }
 public JsonReaderBase()
 {
     _token = JsonToken.BOF();
 }
Exemple #16
0
        /// <summary>
        /// Parses the next token from the input and returns it.
        /// </summary>
        JsonToken Parse()
        {
            var ch = NextClean();

            //
            // String
            //

            if (ch == '"' || ch == '\'')
            {
                return(Yield(JsonToken.String(NextString(ch))));
            }

            //
            // Object
            //

            if (ch == '{')
            {
                _reader.Back();
                return(ParseObject());
            }

            //
            // Array
            //

            if (ch == '[')
            {
                _reader.Back();
                return(ParseArray());
            }

            //
            // Handle unquoted text. This could be the values true, false, or
            // null, or it can be a number. An implementation (such as this one)
            // is allowed to also accept non-standard forms.
            //
            // Accumulate characters until we reach the end of the text or a
            // formatting character.
            //

            var sb = new StringBuilder();
            var b  = ch;

            while (ch >= ' ' && ",:]}/\\\"[{;=#".IndexOf(ch) < 0)
            {
                sb.Append(ch);
                ch = _reader.Next();
            }

            _reader.Back();

            var s = sb.ToString().Trim();

            if (s.Length == 0)
            {
                throw SyntaxError("Missing value.");
            }


            //
            // Boolean
            //

            if (s == JsonBoolean.TrueText || s == JsonBoolean.FalseText)
            {
                return(Yield(JsonToken.Boolean(s == JsonBoolean.TrueText)));
            }

            //
            // Null
            //

            if (s == JsonNull.Text)
            {
                return(Yield(JsonToken.Null()));
            }

            //
            // Number
            //
            // Try converting it. We support the 0- and 0x- conventions.
            // If a number cannot be produced, then the value will just
            // be a string. Note that the 0-, 0x-, plus, and implied
            // string conventions are non-standard, but a JSON text parser
            // is free to accept non-JSON text forms as long as it accepts
            // all correct JSON text forms.
            //

            if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+')
            {
                if (b == '0' && s.Length > 1 && s.IndexOfAny(NumNonDigitChars) < 0)
                {
                    if (s.Length > 2 && (s[1] == 'x' || s[1] == 'X'))
                    {
                        var parsed = TryParseHex(s);
                        if (!ReferenceEquals(parsed, s))
                        {
                            return(Yield(JsonToken.Number(parsed)));
                        }
                    }
                    else
                    {
                        var parsed = TryParseOctal(s);
                        if (!ReferenceEquals(parsed, s))
                        {
                            return(Yield(JsonToken.Number(parsed)));
                        }
                    }
                }
                else
                {
                    if (!JsonNumber.IsValid(s))
                    {
                        throw SyntaxError(string.Format("The text '{0}' has the incorrect syntax for a number.", s));
                    }

                    return(Yield(JsonToken.Number(s)));
                }
            }

            //
            // Treat as String in all other cases, e.g. when unquoted.
            //

            return(Yield(JsonToken.String(s)));
        }
Exemple #17
0
 public MockedJsonReader Null()
 {
     return(Append(JsonToken.Null()));
 }
Exemple #18
0
 public MockedJsonReader Member(string name)
 {
     return(Append(JsonToken.Member(name)));
 }
Exemple #19
0
 public MockedJsonReader Member(string name, string value)
 {
     return(Member(name).Append(JsonToken.String(value)));
 }
Exemple #20
0
 public MockedJsonReader EndObject()
 {
     return(Append(JsonToken.EndObject()));
 }
 private void AssertTokenText(JsonToken token, string text)
 {
     Assert.IsTrue(_reader.Read());
     Assert.AreEqual(token, _reader.Token, "Found {0} (with text \x201c{1}\x201d) when expecting {2} (with text \x201c{3}\x201d).", _reader.Token, _reader.Text, token, text);
     if (text != null)
         Assert.AreEqual(text, _reader.Text);
 }
 private void AssertMember(string name, JsonToken valueToken, string valueText)
 {
     AssertTokenText(JsonToken.Member, name);
     AssertTokenText(valueToken, valueText);
 }
Exemple #23
0
 public void BOFEOF()
 {
     Assert.AreEqual(JsonTokenClass.BOF, JsonToken.BOF().Class);
     Assert.AreEqual(JsonTokenClass.EOF, JsonToken.EOF().Class);
 }
Exemple #24
0
        public JsonReader CreatePlayer()
        {
            if (Bracket != JsonWriterBracket.Pending && 
                Bracket != JsonWriterBracket.Closed)
            {
                throw new InvalidOperationException("JSON data cannot be read before it is complete.");
            }

            JsonToken[] tokens = new JsonToken[_count + 2];
            
            if (_count > 0)
                Array.Copy(_tokens, 0, tokens, 1, _count);
            
            tokens[0] = JsonToken.BOF();
            tokens[tokens.Length - 1] = JsonToken.EOF();

            return new JsonPlayer(tokens);
        }
Exemple #25
0
 public void EqualityWhenSameClassAndText()
 {
     Assert.IsTrue(JsonToken.String("hello").Equals(JsonToken.String("hello")));
 }
Exemple #26
0
 public MockedJsonReader String(string s)
 {
     return(Append(JsonToken.String(s)));
 }
Exemple #27
0
 public void InEqualityWhenSameClassDifferentText()
 {
     Assert.IsFalse(JsonToken.String("hello").Equals(JsonToken.String("world")));
 }
Exemple #28
0
 public MockedJsonReader EndArray()
 {
     return(Append(JsonToken.EndArray()));
 }
Exemple #29
0
 public void InEqualityWhenDifferentClassSameText()
 {
     Assert.IsFalse(JsonToken.String("123").Equals(JsonToken.Number("123")));
 }
        /// <summary> 
        /// Yields control back to the reader's user while updating the
        /// reader with the new found token and its text.
        /// </summary>

        private JsonToken Yield(JsonToken token)
        {
            return Yield(token, null);
        }
Exemple #31
0
 public void EqualityWithTextlessClass()
 {
     Assert.AreEqual(JsonToken.BOF(), JsonToken.BOF());
 }
Exemple #32
0
 public MockedJsonReader Boolean(bool b)
 {
     return(Append(JsonToken.Boolean(b)));
 }
Exemple #33
0
 public void InEqualityWithNull()
 {
     Assert.IsFalse(JsonToken.Null().Equals(null));
 }
Exemple #34
0
 private static void AssertBufferedValueScalarOrNull(JsonToken expected, JsonBufferWriter writer)
 {
     JsonBuffer buffer = writer.GetBuffer();
     JsonBufferReader reader = buffer.CreateReader();
     reader.Read();
     reader.Read();
     JsonBuffer value = reader.BufferValue();
     if (expected.Class == JsonTokenClass.Null)
         Assert.IsTrue(value.IsNull);
     else
         Assert.IsTrue(value.IsScalar);
     JsonBufferReader vr = value.CreateReader();
     Assert.AreEqual(1, vr.Depth);
     Assert.AreEqual(expected, vr.Token);
     vr.Read();
     vr.ReadToken(JsonTokenClass.EndArray);
     Assert.IsTrue(vr.EOF);
 }
Exemple #35
0
 public void InEqualityWithAnotherType()
 {
     Assert.IsFalse(JsonToken.Null().Equals(123));
 }
 private void AssertToken(JsonToken token)
 {
     AssertTokenText(token, null);
 }
Exemple #37
0
 public void HashCodeNonZero()
 {
     Assert.AreNotEqual(0, JsonToken.EOF().GetHashCode());
     Assert.AreNotEqual(0, JsonToken.String("string").GetHashCode());
 }
 private void AssertMember(string name, JsonToken valueToken)
 {
     AssertMember(name, valueToken, null);
 }
Exemple #39
0
 public void NullToken()
 {
     Assert.AreEqual(JsonTokenClass.Null, JsonToken.Null().Class);
 }
        public void SkipTo(JsonToken token)
        {
            // BUGBUG: Depth check missing bug!
            // This loop would exit prematurely if it find the sought token at
            // a depth lower than where it started, such as in the case of
            // nested structures.
            
            while (Read())
            {
                if (Token == token)
                    return;
            }

            throw new JsonException(string.Format("Found EOF while attempting to skip to {0}.", token.ToString()));
        }
Exemple #41
0
 public void StringTokenNeverNull()
 {
     Assert.IsNotNull(JsonToken.String(null).Text);
 }
Exemple #42
0
 public bool Equals(JsonToken rhs)
 {
     return Class.Equals(rhs.Class) && (Text == null || Text.Equals(rhs.Text));
 }
Exemple #43
0
 public void NumberTokenTextCannotBeNull()
 {
     JsonToken.Number(null);
 }
Exemple #44
0
 protected JsonReaderBase()
 {
     _token = JsonToken.BOF();
 }
Exemple #45
0
 public void NumberTokenTextCannotBeZeroLength()
 {
     JsonToken.Number("");
 }
 public JsonReaderBase()
 {
     _token = JsonToken.BOF();
 }
Exemple #47
0
 public void ObjectTokens()
 {
     Assert.AreEqual(JsonTokenClass.Object, JsonToken.Object().Class);
     Assert.AreEqual(JsonTokenClass.EndObject, JsonToken.EndObject().Class);
 }
 private void Write(JsonToken token)
 {
     Entries.Add(token);
 }
        public JsonReader CreatePlayer()
        {
            int count = _entries == null ? 0 : _entries.Count;
            
            JsonToken[] entries = new JsonToken[count + 2];
            
            if (count > 0)
                _entries.CopyTo(entries, 1);
            
            entries[0] = JsonToken.BOF();
            entries[entries.Length - 1] = JsonToken.EOF();

            return new JsonPlayer(entries);
        }
Exemple #50
0
 private void Write(JsonToken token)
 {
     TokenList.Add(token);
 }
Exemple #51
0
 MockedJsonReader Append(JsonToken token)
 {
     _queue.Enqueue(token);
     return(this);
 }
Exemple #52
0
 public JsonPlayer(JsonToken[] tokens)
 {
     Debug.Assert(tokens != null);
     
     _tokens = tokens;
 }
 public Entry(JsonToken token, string text)
 {
     Token = token;
     Text = text;
 }
Exemple #54
0
        public JsonReader CreatePlayer()
        {
            JsonToken[] tokens = new JsonToken[_count + 2];
            
            if (_count > 0)
                Array.Copy(_tokens, 0, tokens, 1, _count);
            
            tokens[0] = JsonToken.BOF();
            tokens[tokens.Length - 1] = JsonToken.EOF();

            return new JsonPlayer(tokens);
        }
Exemple #55
0
        /// <summary>
        /// Yields control back to the reader's user while updating the
        /// reader with the new found token and its text.
        /// </summary>

        private JsonToken Yield(JsonToken token)
        {
            return(Yield(token, null));
        }
        /// <summary> 
        /// Yields control back to the reader's user while updating the
        /// reader with the new found token, its text and the next 
        /// continuation point into the reader.
        /// </summary>
        /// <remarks>
        /// By itself, this method cannot affect the stack such tha control 
        /// is returned back to the reader's user. This must be done by 
        /// Yield's caller by way of explicit return.
        /// </remarks>

        private JsonToken Yield(JsonToken token, Continuation continuation)
        {
            if (continuation != null)
                _stack.Push(continuation);
            
            return token;
        }
Exemple #57
0
        /// <summary>
        /// Parses the next token from the input and returns it.
        /// </summary>

        private JsonToken Parse()
        {
            char ch = NextClean();

            //
            // String
            //

            if (ch == '"' || ch == '\'')
            {
                return(Yield(JsonToken.String(NextString(ch))));
            }

            //
            // Object
            //

            if (ch == '{')
            {
                _reader.Back();
                return(ParseObject());
            }

            //
            // Array
            //

            if (ch == '[')
            {
                _reader.Back();
                return(ParseArray());
            }

            //
            // Handle unquoted text. This could be the values true, false, or
            // null, or it can be a number. An implementation (such as this one)
            // is allowed to also accept non-standard forms.
            //
            // Accumulate characters until we reach the end of the text or a
            // formatting character.
            //

            StringBuilder sb = new StringBuilder();
            char          b  = ch;

            while (ch >= ' ' && ",:]}/\\\"[{;=#".IndexOf(ch) < 0)
            {
                sb.Append(ch);
                ch = _reader.Next();
            }

            _reader.Back();

            string s = sb.ToString().Trim();

            if (s.Length == 0)
            {
                throw new JsonException("Missing value.");
            }


            //
            // Boolean
            //

            if (s == JsonBoolean.TrueText || s == JsonBoolean.FalseText)
            {
                return(Yield(JsonToken.Boolean(s == JsonBoolean.TrueText)));
            }

            //
            // Null
            //

            if (s == JsonNull.Text)
            {
                return(Yield(JsonToken.Null()));
            }

            //
            // Number
            //

            if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+')
            {
                double unused;
                if (!double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out unused))
                {
                    throw new JsonException(string.Format("The text '{0}' has the incorrect syntax for a number.", s));
                }

                return(Yield(JsonToken.Number(s)));
            }

            //
            // Treat as String in all other cases, e.g. when unquoted.
            //

            return(Yield(JsonToken.String(s)));
        }
 public Entry(JsonToken token) :
     this(token, null) {}
Exemple #59
0
 public MockedJsonReader Number(decimal i)
 {
     return(Append(JsonToken.Number(i.ToString(CultureInfo.InvariantCulture))));
 }