Esempio n. 1
0
        public static JsonNode DeserializeBinary(BinaryReader aReader)
        {
            var type = (JsonNodeType)aReader.ReadByte();

            switch (type)
            {
            case JsonNodeType.Array:
            {
                var count = aReader.ReadInt32();
                var tmp   = new JsonArray();
                for (var i = 0; i < count; i++)
                {
                    tmp.Add(DeserializeBinary(aReader));
                }

                return(tmp);
            }

            case JsonNodeType.Object:
            {
                var count = aReader.ReadInt32();
                var tmp   = new JsonObject();
                for (var i = 0; i < count; i++)
                {
                    var key = aReader.ReadString();
                    var val = DeserializeBinary(aReader);
                    tmp.Add(key, val);
                }

                return(tmp);
            }

            case JsonNodeType.String:
            {
                return(new JsonString(aReader.ReadString()));
            }

            case JsonNodeType.Number:
            {
                return(new JsonNumber(aReader.ReadDouble()));
            }

            case JsonNodeType.Boolean:
            {
                return(new JsonBool(aReader.ReadBoolean()));
            }

            case JsonNodeType.NullValue:
            {
                return(JsonNull.CreateOrGet());
            }

            default:
            {
                throw new Exception("Error deserializing JSON. Unknown tag: " + type);
            }
            }
        }
Esempio n. 2
0
        private static JsonNode ParseElement(string token, bool quoted)
        {
            if (quoted)
            {
                return(token);
            }
            var tmp = token.ToLower();

            switch (tmp)
            {
            case "false":
            case "true":
                return(tmp == "true");

            case "null":
                return(JsonNull.CreateOrGet());
            }

            if (double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out var val))
            {
                return(val);
            }
            return(token);
        }