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);
        }
예제 #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);
        }
예제 #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;
        }
예제 #4
0
        /// <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;
        }
예제 #5
0
        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;
        }
예제 #6
0
파일: JsonBuffer.cs 프로젝트: db48x/KeeFox
        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();
        }
예제 #7
0
 protected override void Write(JsonToken token)
 {
     _storage.Write(token);
 }
예제 #8
0
 public bool Equals(JsonToken other)
 {
     return(Class.Equals(other.Class) && (Text == null || Text.Equals(other.Text)));
 }
예제 #9
0
 public bool Equals(JsonToken other)
 {
     return Class.Equals(other.Class) && (Text == null || Text.Equals(other.Text));
 }
예제 #10
0
 public MockedJsonReader End()
 {
     return(Append(JsonToken.EOF()));
 }
예제 #11
0
 private MockedJsonReader Append(JsonToken token)
 {
     _queue.Enqueue(token);
     return this;
 }
예제 #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;
        }
예제 #13
0
        /// <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));
        }
예제 #14
0
 public void ArrayTokens()
 {
     Assert.AreEqual(JsonTokenClass.Array, JsonToken.Array().Class);
     Assert.AreEqual(JsonTokenClass.EndArray, JsonToken.EndArray().Class);
 }
예제 #15
0
 public JsonReaderBase()
 {
     _token = JsonToken.BOF();
 }
예제 #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)));
        }
예제 #17
0
 public MockedJsonReader Null()
 {
     return(Append(JsonToken.Null()));
 }
예제 #18
0
 public MockedJsonReader Member(string name)
 {
     return(Append(JsonToken.Member(name)));
 }
예제 #19
0
 public MockedJsonReader Member(string name, string value)
 {
     return(Member(name).Append(JsonToken.String(value)));
 }
예제 #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);
 }
예제 #23
0
 public void BOFEOF()
 {
     Assert.AreEqual(JsonTokenClass.BOF, JsonToken.BOF().Class);
     Assert.AreEqual(JsonTokenClass.EOF, JsonToken.EOF().Class);
 }
예제 #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);
        }
예제 #25
0
 public void EqualityWhenSameClassAndText()
 {
     Assert.IsTrue(JsonToken.String("hello").Equals(JsonToken.String("hello")));
 }
예제 #26
0
 public MockedJsonReader String(string s)
 {
     return(Append(JsonToken.String(s)));
 }
예제 #27
0
 public void InEqualityWhenSameClassDifferentText()
 {
     Assert.IsFalse(JsonToken.String("hello").Equals(JsonToken.String("world")));
 }
예제 #28
0
 public MockedJsonReader EndArray()
 {
     return(Append(JsonToken.EndArray()));
 }
예제 #29
0
 public void InEqualityWhenDifferentClassSameText()
 {
     Assert.IsFalse(JsonToken.String("123").Equals(JsonToken.Number("123")));
 }
예제 #30
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);
        }
예제 #31
0
 public void EqualityWithTextlessClass()
 {
     Assert.AreEqual(JsonToken.BOF(), JsonToken.BOF());
 }
예제 #32
0
 public MockedJsonReader Boolean(bool b)
 {
     return(Append(JsonToken.Boolean(b)));
 }
예제 #33
0
 public void InEqualityWithNull()
 {
     Assert.IsFalse(JsonToken.Null().Equals(null));
 }
예제 #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);
 }
예제 #35
0
 public void InEqualityWithAnotherType()
 {
     Assert.IsFalse(JsonToken.Null().Equals(123));
 }
 private void AssertToken(JsonToken token)
 {
     AssertTokenText(token, null);
 }
예제 #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);
 }
예제 #39
0
 public void NullToken()
 {
     Assert.AreEqual(JsonTokenClass.Null, JsonToken.Null().Class);
 }
예제 #40
0
        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()));
        }
예제 #41
0
 public void StringTokenNeverNull()
 {
     Assert.IsNotNull(JsonToken.String(null).Text);
 }
예제 #42
0
파일: JsonToken.cs 프로젝트: s7loves/pesta
 public bool Equals(JsonToken rhs)
 {
     return Class.Equals(rhs.Class) && (Text == null || Text.Equals(rhs.Text));
 }
예제 #43
0
 public void NumberTokenTextCannotBeNull()
 {
     JsonToken.Number(null);
 }
예제 #44
0
 protected JsonReaderBase()
 {
     _token = JsonToken.BOF();
 }
예제 #45
0
 public void NumberTokenTextCannotBeZeroLength()
 {
     JsonToken.Number("");
 }
예제 #46
0
 public JsonReaderBase()
 {
     _token = JsonToken.BOF();
 }
예제 #47
0
 public void ObjectTokens()
 {
     Assert.AreEqual(JsonTokenClass.Object, JsonToken.Object().Class);
     Assert.AreEqual(JsonTokenClass.EndObject, JsonToken.EndObject().Class);
 }
예제 #48
0
 private void Write(JsonToken token)
 {
     Entries.Add(token);
 }
예제 #49
0
        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);
        }
예제 #50
0
 private void Write(JsonToken token)
 {
     TokenList.Add(token);
 }
예제 #51
0
 MockedJsonReader Append(JsonToken token)
 {
     _queue.Enqueue(token);
     return(this);
 }
예제 #52
0
 public JsonPlayer(JsonToken[] tokens)
 {
     Debug.Assert(tokens != null);
     
     _tokens = tokens;
 }
예제 #53
0
 public Entry(JsonToken token, string text)
 {
     Token = token;
     Text = text;
 }
예제 #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);
        }
예제 #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));
        }
예제 #56
0
        /// <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;
        }
예제 #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)));
        }
예제 #58
0
 public Entry(JsonToken token) :
     this(token, null) {}
예제 #59
0
 public MockedJsonReader Number(decimal i)
 {
     return(Append(JsonToken.Number(i.ToString(CultureInfo.InvariantCulture))));
 }