public static KeyValue[] DeSerializeOne(System.Text.Json.JsonProperty elem) { string realname = elem.Name; if (realname.Equals("Lines", StringComparison.OrdinalIgnoreCase)) { ; // debug } if (elem.Value.ValueKind == System.Text.Json.JsonValueKind.Object) { KeyValue[] vals = elem.Value.EnumerateObject().ToList().SelectMany(v => DeSerializeOne(v)).ToArray(); KeyValues valv = new KeyValues(); valv.AddRange(vals); return(new KeyValue[] { new KeyValue(realname, valv) }); } else if (elem.Value.ValueKind == System.Text.Json.JsonValueKind.Array) { var arrayvals = elem.Value.EnumerateArray().ToList(); KeyValue[] vals = arrayvals.Select(v => new KeyValue(realname, ReadValue(v)) { IsMultiples = true }).ToArray(); return(vals); } else { object val = ReadValue(elem.Value); return(new KeyValue[] { new KeyValue(realname, val) }); } }
private void FromJsonSub(ParamValues datavalue, System.Text.Json.JsonProperty elem, string lastpath) { if (elem.Value.ValueKind == System.Text.Json.JsonValueKind.Array) { // Array invalids } else if (elem.Value.ValueKind == System.Text.Json.JsonValueKind.Object) { string newlastpath = lastpath + elem.Name + "/"; foreach (var jsubitem in elem.Value.EnumerateObject().ToList()) { FromJsonSub(datavalue, jsubitem, newlastpath); } } else { string fullname = lastpath + elem.Name; ParamValuesNode dataelement = new ParamValuesNode(); dataelement.Name = fullname; dataelement.Value = Nglib.DATA.KEYVALUES.KeyValuesSerializerJson.ReadValue(elem.Value); datavalue.Add(dataelement); } }
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()); }