コード例 #1
0
        private static object DeserializeArray(JsonReaderBase reader)
        {
            if (reader.ReadChar() != '[')
            {
                throw new FormatException();
            }
            var collection = new List <object>();
            var first      = true;

            while (true)
            {
                reader.SkipSpace();
                if (reader.IsEnd)
                {
                    throw new FormatException("配列の閉じ括弧が見つかりません。");
                }
                if (reader.Peek() == ']')
                {
                    reader.ReadChar();
                    break;
                }
                if (!first)
                {
                    if (reader.Peek() != ',')
                    {
                        throw new FormatException("配列の区切り文字が見つかりません。");
                    }
                    reader.ReadChar();
                }
                reader.SkipSpace();
                collection.Add(Deserialize(reader));
                first = false;
            }
            return(collection.ToArray());
        }
コード例 #2
0
        private static object DeserializeObject(JsonReaderBase reader)
        {
            if (reader.ReadChar() != '{')
            {
                throw new FormatException();
            }
            var dic   = new Dictionary <string, object>();
            var first = true;

            while (true)
            {
                reader.SkipSpace();
                if (reader.IsEnd)
                {
                    throw new FormatException("オブジェクトの閉じ括弧が見つかりません。");
                }
                if (reader.Peek() == '}')
                {
                    reader.ReadChar();
                    break;
                }
                if (!first)
                {
                    if (reader.Peek() != ',')
                    {
                        throw new FormatException("オブジェクトの区切り文字','が見つかりません。");
                    }
                    reader.ReadChar();
                }
                reader.SkipSpace();
                var key = DeserializeString(reader);
                reader.SkipSpace();
                if (reader.Peek() != ':')
                {
                    throw new FormatException("オブジェクトの区切り文字':'が見つかりません。");
                }
                reader.ReadChar();
                reader.SkipSpace();
                var value = Deserialize(reader);
                dic[key] = value;
                first    = false;
            }
            return(dic);
        }
コード例 #3
0
        private static object Deserialize(JsonReaderBase reader)
        {
            reader.SkipSpace();
            switch (reader.Peek())
            {
            case '[':
                return(DeserializeArray(reader));

            case '{':
                return(DeserializeObject(reader));

            case '"':
                return(DeserializeString(reader));

            case 't':     // true
                if (reader.ReadStr(4) != "true")
                {
                    throw new FormatException();
                }
                return(true);

            case 'f':     // false
                if (reader.ReadStr(5) != "false")
                {
                    throw new FormatException();
                }
                return(false);

            case 'n':     // null
                if (reader.ReadStr(4) != "null")
                {
                    throw new FormatException();
                }
                return(null);

            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                return(DeserializeNumber(1, reader));

            case '-':
                reader.ReadChar();
                return(DeserializeNumber(-1, reader));

            default:
                throw new FormatException();
            }
        }