Exemple #1
0
 public JsonValue(JsonValueType type)
 {
     _type = type;
     //TODO: no spans on the heap
     //_value = default(Utf8Span);
     _object = null;
 }
Exemple #2
0
 public JsonValue(JsonDynamicObject obj)
 {
     //TODO: no spans on the heap
     //_value = default(Utf8Span);
     _object = obj;
     _type   = JsonValueType.Object;
 }
Exemple #3
0
 public JsonValue(Utf8Span value, JsonValueType type = JsonValueType.String)
 {
     //TODO: no spans on the heap
     //_value = value;
     _object = null;
     _type   = type;
 }
Exemple #4
0
 public JsonProperty(JsonDynamicObject obj, Utf8String name)
 {
     _object = obj;
     _name   = name;
 }
Exemple #5
0
 public JsonValue(JsonReader.JsonValueType type)
 {
     _type   = type;
     _value  = default(Utf8String);
     _object = null;
 }
Exemple #6
0
 public JsonValue(JsonDynamicObject obj)
 {
     _value  = default(Utf8String);
     _object = obj;
     _type   = JsonReader.JsonValueType.Object;
 }
Exemple #7
0
 public JsonValue(Utf8String value, JsonReader.JsonValueType type = JsonReader.JsonValueType.String)
 {
     _value  = value;
     _object = null;
     _type   = type;
 }
Exemple #8
0
        public static JsonDynamicObject Parse(ReadOnlySpan <byte> utf8, int expectedNumberOfProperties = -1)
        {
            Stack <JsonDynamicObject> stack = new Stack <JsonDynamicObject>();

            if (expectedNumberOfProperties == -1)
            {
                expectedNumberOfProperties = utf8.Length >> 3;
            }
            var properties = new Dictionary <JsonProperty, JsonValue>(expectedNumberOfProperties);

            stack.Push(new JsonDynamicObject(properties));

            var reader = new JsonReader(new Utf8String(utf8));

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                case JsonReader.JsonTokenType.Property:
                    var name     = reader.GetName();
                    var type     = reader.GetJsonValueType();
                    var value    = reader.GetValue();
                    var current  = stack.Peek();
                    var property = new JsonProperty(current, name);
                    switch (type)
                    {
                    case JsonReader.JsonValueType.String:
                        current._properties[property] = new JsonValue(value);
                        break;

                    case JsonReader.JsonValueType.Object:         // TODO: could this be lazy? Could this reuse the root JsonObject (which would store non-allocating JsonDom)?
                        var newObj = new JsonDynamicObject(properties);
                        current._properties[property] = new JsonValue(newObj);
                        stack.Push(newObj);
                        break;

                    case JsonReader.JsonValueType.True:
                        current._properties[property] = new JsonValue(type);
                        break;

                    case JsonReader.JsonValueType.False:
                        current._properties[property] = new JsonValue(type);
                        break;

                    case JsonReader.JsonValueType.Null:
                        current._properties[property] = new JsonValue(type);
                        break;

                    case JsonReader.JsonValueType.Number:
                        current._properties[property] = new JsonValue(value, type);
                        break;

                    case JsonReader.JsonValueType.Array:
                        throw new NotImplementedException("array support not implemented yet.");

                    default:
                        throw new NotSupportedException();
                    }
                    break;

                case JsonReader.JsonTokenType.ObjectStart:
                    break;

                case JsonReader.JsonTokenType.ObjectEnd:
                    if (stack.Count != 1)
                    {
                        stack.Pop();
                    }
                    break;

                case JsonReader.JsonTokenType.ArrayStart:
                    throw new NotImplementedException("array support not implemented yet.");

                case JsonReader.JsonTokenType.ArrayEnd:
                case JsonReader.JsonTokenType.Value:
                    break;

                default:
                    throw new NotSupportedException();
                }
            }

            return(stack.Peek());
        }
Exemple #9
0
 public JsonProperty(JsonDynamicObject obj, Utf8Span name)
 {
     _object = obj;
     //TODO: no spans on the heap
     //_name = name;
 }