Ejemplo n.º 1
0
        public void BooleanToken()
        {
            JsonToken token = JsonToken.Boolean(true);

            Assert.AreEqual(JsonTokenClass.Boolean, token.Class);
            Assert.AreEqual("true", token.Text);

            token = JsonToken.Boolean(false);
            Assert.AreEqual(JsonTokenClass.Boolean, token.Class);
            Assert.AreEqual("false", token.Text);
        }
        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)));
            }
            }
        }
Ejemplo n.º 3
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());
 }
Ejemplo n.º 4
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)));
        }
Ejemplo n.º 5
0
 protected override void WriteBooleanImpl(bool value)
 {
     Write(JsonToken.Boolean(value));
 }
Ejemplo n.º 6
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)));
        }
Ejemplo n.º 7
0
 public MockedJsonReader Boolean(bool b)
 {
     return(Append(JsonToken.Boolean(b)));
 }