Esempio n. 1
0
        internal override object BaseDeserialize(TextReader r, string path)
        {
            if (JsonParserUtil.PeekIsNull(r, true, path))
            {
                return(null);
            }
            var result = new List <TElement>();
            var parens = JsonParserUtil.ReadStartArray(r);
            var c      = JsonParserUtil.PeekNextChar(r, true);

            while (c != ']')
            {
                result.Add(_elementSerializer.Deserialize(r, path));
                c = JsonParserUtil.PeekNextChar(r, true);
                if (c != ',' && c != ']')
                {
                    throw new JsonDeserializationException(string.Format("Expected ']' at '{0}'", path));
                }
                else if (c == ',')
                {
                    JsonParserUtil.ReadNextChar(r, true);
                }
            }
            JsonParserUtil.ReadEndArray(r, parens);
            return(typeof(TEnumerable).IsArray ? result.ToArray() : Activator.CreateInstance(typeof(TEnumerable), result));
        }
Esempio n. 2
0
        internal override object BaseDeserialize(TextReader r, string path)
        {
            if (JsonParserUtil.PeekIsNull(r, true, path))
            {
                return(null);
            }
            var c = JsonParserUtil.ReadNextChar(r, true);

            if (c != '"')
            {
                throw new JsonDeserializationException(string.Format("Expected '\"' at '{0}'", path));
            }
            var escape = false;

            c = JsonParserUtil.ReadNextChar(r, true);
            var b = new StringBuilder();

            while (c != '"' || escape)
            {
                if (escape)
                {
                    switch (c)
                    {
                    case 'b': c = '\b'; break;

                    case 'f': c = '\f'; break;

                    case 'n': c = '\n'; break;

                    case 'r': c = '\r'; break;

                    case 't': c = '\t'; break;

                    case 'v': c = '\v'; break;

                    case 'x': c = JsonParserUtil.ReadCharCode(r, 16, 2, 2); break;

                    case 'u': c = JsonParserUtil.ReadCharCode(r, 16, 4, 4); break;
                    }
                }
                escape = !escape && (c == '\\');
                if (!escape)
                {
                    b.Append(c);
                }
                else if (char.IsDigit(JsonParserUtil.PeekNextChar(r, false)))
                {
                    b.Append(JsonParserUtil.ReadCharCode(r, 8, 1, 3));
                }
                c = JsonParserUtil.ReadNextChar(r, false);
            }
            return(b.ToString());
        }
Esempio n. 3
0
        public static bool PeekIsNull(TextReader r, bool ignoreWhitespace, string path)
        {
            var c = JsonParserUtil.PeekNextChar(r, ignoreWhitespace);

            if (char.ToLowerInvariant(c) == 'n')
            {
                r.Read();
                if (char.ToLowerInvariant((char)r.Read()) == 'u' && char.ToLowerInvariant((char)r.Read()) == 'l' && char.ToLowerInvariant((char)r.Read()) == 'l')
                {
                    return(true);
                }
                throw new JsonDeserializationException(string.Format("Expected 'null' at '{0}'", path));
            }
            return(false);
        }
Esempio n. 4
0
        internal override object BaseDeserialize(TextReader r, string path)
        {
            var token = JsonParserUtil.GetNextToken(r);

            return(token == string.Empty || token.Equals("null", StringComparison.OrdinalIgnoreCase) ? null : (object)bool.Parse(token));
        }