Esempio n. 1
0
        public void NullValueReading()
        {
            var writer = new JsonBufferWriter();

            writer.WriteNull();
            AssertBufferedValueScalarOrNull(JsonToken.Null(), writer);
        }
Esempio n. 2
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);
        }
        private JsonToken Parse()
        {
            char c = NextClean();

            if (c == '"' || c == '\'')
            {
                return(Yield(JsonToken.String(NextString(c))));
            }
            switch (c)
            {
            case '{':
                _reader.Back();
                return(ParseObject());

            case '[':
                _reader.Back();
                return(ParseArray());

            default:
            {
                StringBuilder stringBuilder = new StringBuilder();
                char          c2            = c;
                while (c >= ' ' && ",:]}/\\\"[{;=#".IndexOf(c) < 0)
                {
                    stringBuilder.Append(c);
                    c = _reader.Next();
                }
                _reader.Back();
                string text = stringBuilder.ToString().Trim();
                if (text.Length == 0)
                {
                    throw new JsonException("Missing value.");
                }
                if (text == "true" || text == "false")
                {
                    return(Yield(JsonToken.Boolean(text == "true")));
                }
                if (text == "null")
                {
                    return(Yield(JsonToken.Null()));
                }
                if ((c2 >= '0' && c2 <= '9') || c2 == '.' || c2 == '-' || c2 == '+')
                {
                    if (!double.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out double _))
                    {
                        throw new JsonException($"The text '{text}' has the incorrect syntax for a number.");
                    }
                    return(Yield(JsonToken.Number(text)));
                }
                return(Yield(JsonToken.String(text)));
            }
            }
        }
Esempio n. 4
0
 public void JsonTokenString()
 {
     Assert.AreEqual("String:hello", JsonToken.String("hello").ToString());
     Assert.AreEqual("Number:123", JsonToken.Number("123").ToString());
     Assert.AreEqual("Boolean:true", JsonToken.Boolean(true).ToString());
     Assert.AreEqual("Boolean:false", JsonToken.Boolean(false).ToString());
     Assert.AreEqual("Null:null", JsonToken.Null().ToString());
     Assert.AreEqual("Array", JsonToken.Array().ToString());
     Assert.AreEqual("EndArray", JsonToken.EndArray().ToString());
     Assert.AreEqual("Object", JsonToken.Object().ToString());
     Assert.AreEqual("EndObject", JsonToken.EndObject().ToString());
     Assert.AreEqual("Member:test", JsonToken.Member("test").ToString());
     Assert.AreEqual("BOF", JsonToken.BOF().ToString());
     Assert.AreEqual("EOF", JsonToken.EOF().ToString());
 }
Esempio n. 5
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 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)
                {
                    if (s.Length > 2 && (s[1] == 'x' || s[1] == 'X'))
                    {
                        string parsed = TryParseHex(s);
                        if (!ReferenceEquals(parsed, s))
                        {
                            return(Yield(JsonToken.Number(parsed)));
                        }
                    }
                    else
                    {
                        string parsed = TryParseOctal(s);
                        if (!ReferenceEquals(parsed, s))
                        {
                            return(Yield(JsonToken.Number(parsed)));
                        }
                    }
                }
                else
                {
                    double unused;

                    if ((b == '-' && s.Length >= 2 && s[1] == 'I') || // rule out -Infinity that double parsing allows
                        !double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out unused))
                    {
                        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)));
        }
Esempio n. 6
0
 protected override void WriteNullImpl()
 {
     Write(JsonToken.Null());
 }
Esempio n. 7
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)));
        }
Esempio n. 8
0
 public void StringRepresentationForNull()
 {
     Assert.AreEqual("null", JsonBuffer.From(JsonToken.Null()).ToString());
 }
            protected override JsonToken ReadTokenImpl()
            {
                var baseReader = _reader;

                var 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.
                //

                var bufferedMembers = _bufferedMembers;

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

                    var 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.
                            //

                            var valueReader = _memberStructuredValueReader
                                              ?? (_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.
                    //

                    var member = 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.
                //

                var token = baseReader.Token;

                baseReader.Read();
                return(token);
            }
Esempio n. 10
0
 public void NullToken()
 {
     Assert.AreEqual(JsonTokenClass.Null, JsonToken.Null().Class);
 }
Esempio n. 11
0
 public void InEqualityWithAnotherType()
 {
     Assert.IsFalse(JsonToken.Null().Equals(123));
 }
Esempio n. 12
0
 public void InEqualityWithNull()
 {
     Assert.IsFalse(JsonToken.Null().Equals(null));
 }
Esempio n. 13
0
 public MockedJsonReader Null()
 {
     return(Append(JsonToken.Null()));
 }