public IEnumerable <JsonNode> GetNodes(JsonPointer jsonPointer) { if (jsonPointer.Path.Count == 0) { yield return(this); yield break; } if (Value.ValueType == JsonValueType.Array) { // array if (jsonPointer[0] == "*") { // wildcard foreach (var child in ArrayItems) { foreach (var childChild in child.GetNodes(jsonPointer.Unshift())) { yield return(childChild); } } } else { int index; if (!int.TryParse(jsonPointer[0], out index)) { throw new KeyNotFoundException(); } var child = this[index]; foreach (var childChild in child.GetNodes(jsonPointer.Unshift())) { yield return(childChild); } } } else if (Value.ValueType == JsonValueType.Object) { // object if (jsonPointer[0] == "*") { // wildcard foreach (var kv in ObjectItems) { foreach (var childChild in kv.Value.GetNodes(jsonPointer.Unshift())) { yield return(childChild); } } } else { JsonNode child; try { child = this[jsonPointer[0]]; } catch (KeyNotFoundException) { // key Values.Add(new JsonValue(new StringSegment(JsonString.Quote(jsonPointer[0])), JsonValueType.String, m_index)); // value Values.Add(new JsonValue(new StringSegment(), JsonValueType.Object, m_index)); child = this[jsonPointer[0]]; } foreach (var childChild in child.GetNodes(jsonPointer.Unshift())) { yield return(childChild); } } } else { throw new NotImplementedException(); } }
public IEnumerable <JsonDiff> Diff(JsonNode rhs, JsonPointer path = default(JsonPointer)) { switch (Value.ValueType) { case JsonValueType.Null: case JsonValueType.Boolean: case JsonValueType.Number: case JsonValueType.Integer: case JsonValueType.String: if (!Equals(rhs)) { yield return(new JsonDiff(this, JsonDiffType.ValueChanged, string.Format("{0} => {1}", Value, rhs.Value))); } yield break; } if (Value.ValueType != rhs.Value.ValueType) { yield return(new JsonDiff(this, JsonDiffType.ValueChanged, string.Format("{0} => {1}", Value.ValueType, rhs.Value))); yield break; } if (Value.ValueType == JsonValueType.Object) { var l = ObjectItems.ToDictionary(x => x.Key, x => x.Value); var r = rhs.ObjectItems.ToDictionary(x => x.Key, x => x.Value); foreach (var kv in l) { JsonNode x; if (r.TryGetValue(kv.Key, out x)) { r.Remove(kv.Key); // Found foreach (var y in kv.Value.Diff(x)) { yield return(y); } } else { // Removed yield return(new JsonDiff(kv.Value, JsonDiffType.KeyRemoved, kv.Value.Value.ToString())); } } foreach (var kv in r) { // Addded yield return(new JsonDiff(kv.Value, JsonDiffType.KeyAdded, kv.Value.Value.ToString())); } } else if (Value.ValueType == JsonValueType.Array) { var ll = ArrayItems.GetEnumerator(); var rr = rhs.ArrayItems.GetEnumerator(); while (true) { var lll = ll.MoveNext(); var rrr = rr.MoveNext(); if (lll && rrr) { // found foreach (var y in ll.Current.Diff(rr.Current)) { yield return(y); } } else if (lll) { yield return(new JsonDiff(ll.Current, JsonDiffType.KeyRemoved, ll.Current.Value.ToString())); } else if (rrr) { yield return(new JsonDiff(rr.Current, JsonDiffType.KeyAdded, rr.Current.Value.ToString())); } else { // end break; } } } else { throw new NotImplementedException(); } }
public JsonDiff(JsonNode node, JsonDiffType diffType, string msg) { Path = new JsonPointer(node); DiffType = diffType; Msg = msg; }
public static IEnumerable <ListTreeNode <T> > GetNodes <T>(this ListTreeNode <T> self, JsonPointer jsonPointer) where T : IListTreeItem, IValue <T> { if (jsonPointer.Path.Count == 0) { yield return(self); yield break; } if (self.IsArray()) { // array if (jsonPointer[0][0] == '*') { // wildcard foreach (var child in self.ArrayItems()) { foreach (var childChild in child.GetNodes(jsonPointer.Unshift())) { yield return(childChild); } } } else { int index = jsonPointer[0].ToInt32(); var child = self.ArrayItems().Skip(index).First(); foreach (var childChild in child.GetNodes(jsonPointer.Unshift())) { yield return(childChild); } } } else if (self.IsMap()) { // object if (jsonPointer[0][0] == '*') { // wildcard foreach (var kv in self.ObjectItems()) { foreach (var childChild in kv.Value.GetNodes(jsonPointer.Unshift())) { yield return(childChild); } } } else { ListTreeNode <T> child; try { child = self.ObjectItems().First(x => x.Key.GetUtf8String() == jsonPointer[0]).Value; } catch (Exception) { // key self.AddKey(jsonPointer[0]); // value self.AddValue(default(ArraySegment <byte>), ValueNodeType.Object); child = self.ObjectItems().First(x => x.Key.GetUtf8String() == jsonPointer[0]).Value; } foreach (var childChild in child.GetNodes(jsonPointer.Unshift())) { yield return(childChild); } } } else { throw new NotImplementedException(); } }
public static JsonPointer Pointer <T>(this ListTreeNode <T> self) where T : IListTreeItem, IValue <T> { return(JsonPointer.Create(self)); }
public static JsonPointer Pointer(this JsonNode self) { return(JsonPointer.Create(self)); }