Beispiel #1
0
        /// <summary>
        /// Parses the dictionary.
        /// </summary>
        /// <param name="ignored">if set to <c>true</c> [ignored].</param>
        /// <param name="cache">The cache.</param>
        /// <param name="handler">The handler.</param>
        /// <param name="endToken">The end token.</param>
        /// <returns></returns>
        public object ParseDictionary(bool ignored, ReadCache cache, IDictionaryReadHandler handler, JsonToken endToken)
        {
            IDictionaryReader dr = (handler != null) ? handler.DictionaryReader() : dictionaryBuilder;

            object d = dr.Init();

            while (jp.NextToken() != endToken)
            {
                object key = ParseVal(true, cache);
                if (key is Tag)
                {
                    object val;
                    jp.Read(); // advance to read value
                    string       tag = ((Tag)key).GetValue();
                    IReadHandler val_handler;
                    if (TryGetHandler(tag, out val_handler))
                    {
                        if (this.jp.TokenType == JsonToken.StartObject && val_handler is IDictionaryReadHandler)
                        {
                            // use map reader to decode value
                            val = ParseDictionary(false, cache, (IDictionaryReadHandler)val_handler);
                        }
                        else if (this.jp.TokenType == JsonToken.StartArray && val_handler is IListReadHandler)
                        {
                            // use array reader to decode value
                            val = ParseList(false, cache, (IListReadHandler)val_handler);
                        }
                        else
                        {
                            // read value and decode normally
                            val = val_handler.FromRepresentation(ParseVal(false, cache));
                        }
                    }
                    else
                    {
                        // default decode
                        val = this.Decode(tag, ParseVal(false, cache));
                    }
                    jp.Read(); // advance to read end of object or array
                    return(val);
                }
                else
                {
                    jp.Read(); // advance to read value
                    d = dr.Add(d, key, ParseVal(false, cache));
                }
            }

            return(dr.Complete(d));
        }
        public void CanIgnoreWhiteSpace()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader("   false   true   null"));

            // act
            var token1 = reader.NextToken();
            var token2 = reader.NextToken();
            var token3 = reader.NextToken();

            // assert
            Assert.Equal(JsonToken.False, token1);
            Assert.Equal(JsonToken.True, token2);
            Assert.Equal(JsonToken.Null, token3);
        }
        public void CanReadComma()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader(","));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(JsonToken.Comma, token);
        }
        public void CanReadBooleanTrue()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader("true"));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(JsonToken.True, token);
        }
        public void CanReadExponent(string exponent)
        {
            // arrange
            var reader = new JsonTextReader(new StringReader(exponent));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(new JsonToken(JsonTokenKind.Fractional, exponent), token);
        }
        public void CanReadDecimal()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader("123.456"));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(new JsonToken(JsonTokenKind.Fractional, "123.456"), token);
        }
        public void CanReadUnicodeString(string escaped, string unicode)
        {
            // arrange
            var reader = new JsonTextReader(new StringReader($"\"{escaped}\""));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(new JsonToken(JsonTokenKind.String, unicode), token);
        }
        public void CanReadString()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader("\"hello\""));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(new JsonToken(JsonTokenKind.String, "hello"), token);
        }
        public void CanReadNull()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader("null"));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(JsonToken.Null, token);
        }
        public void CanReadInteger()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader("1234567"));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(new JsonToken(JsonTokenKind.Integer, "1234567"), token);
        }