コード例 #1
0
ファイル: JsonBuilder.cs プロジェクト: jvlppm/Json
        static dynamic Build(JsonReader reader)
#endif
        {
            var token = reader.ReadToken();
            switch (token.Type)
            {
                case TokenType.SpecialChar:
                    reader.PutBack(token);

                    if (token.Value == "{")
                        return BuildObject(reader);

                    else if (token.Value == "[")
                        return BuildList(reader);

                    throw new SemanticException(new[] { "'{'", "'['" }, token.Value, reader.Position);

                case TokenType.Number:
                    if (token.Value.Contains(".") || token.Value.Length > 18)
                        return decimal.Parse(token.Value, System.Globalization.CultureInfo.InvariantCulture);
                    if (token.Value.Length >= 10)
                    {
                        int smallResult;
                        if (int.TryParse(token.Value, out smallResult))
                            return smallResult;

                        return Int64.Parse(token.Value);
                    }
                    return int.Parse(token.Value);

                case TokenType.KeyWord:
                    switch (token.Value)
                    {
                        case "null":
                            return null;

                        case "false":
                        case "true":
                            return token.Value == "true";
                    }

                    throw new SemanticException(new[] { "Boolean", "Number", "String", "Array", "Object" }, token.Value, reader.Position);

                default:
                    return token.Value;
            }
        }
コード例 #2
0
ファイル: JsonBuilder.cs プロジェクト: jvlppm/Json
 static object Build(JsonReader reader)
コード例 #3
0
ファイル: JsonBuilder.cs プロジェクト: jvlppm/Json
 static IList<dynamic> BuildList(JsonReader reader)
コード例 #4
0
ファイル: JsonBuilder.cs プロジェクト: jvlppm/Json
        static IList<object> BuildList(JsonReader reader)
#endif
        {
            var values = new List<dynamic>();

            var nextToken = reader.ReadToken();
            if (nextToken.Type != TokenType.SpecialChar || nextToken.Value != "[")
                throw new SemanticException("'['", nextToken.Value, reader.Position);

            nextToken = reader.ReadToken();

            if (nextToken.Type != TokenType.SpecialChar || nextToken.Value != "]")
            {
                reader.PutBack(nextToken);

                do
                {
                    values.Add(Build(reader));
                    nextToken = reader.ReadToken();
                } while (nextToken.Type == TokenType.SpecialChar && nextToken.Value == ",");

                if (nextToken.Type != TokenType.SpecialChar || nextToken.Value != "]")
                    throw new SemanticException("']'", nextToken.Value, reader.Position);
            }

            return values;
        }
コード例 #5
0
ファイル: JsonBuilder.cs プロジェクト: jvlppm/Json
        static dynamic BuildObject(JsonReader reader)
#endif
        {
#if NET35
            var obj = new Dictionary<string, object>();
#else
            var obj = new ExpandoObject() as IDictionary<string, object>;
#endif
            var nextToken = reader.ReadToken();

            if (nextToken.Type != TokenType.SpecialChar || nextToken.Value != "{")
                throw new SemanticException("{", nextToken.Value, reader.Position);

            nextToken = reader.ReadToken();

            if (nextToken.Type != TokenType.SpecialChar || nextToken.Value != "}")
            {
                reader.PutBack(nextToken);

                do
                {
                    var property = reader.ReadToken();
                    if (property.Type != TokenType.String)
                        throw new SemanticException("String", property.Type.ToString(), reader.Position);

                    nextToken = reader.ReadToken();
                    if (nextToken.Type != TokenType.SpecialChar || nextToken.Value != ":")
                        throw new SemanticException("':'", nextToken.Value, reader.Position);

                    var value = JsonBuilder.Build(reader);
                    obj.Add(property.Value, value);

                    nextToken = reader.ReadToken();

                } while (nextToken.Type == TokenType.SpecialChar && nextToken.Value == ",");

                if (nextToken.Type != TokenType.SpecialChar || nextToken.Value != "}")
                    throw new SemanticException(new[] { "'}'", "','" }, nextToken.Value, reader.Position);
            }

            return obj;
        }