public void Process(PatchContext context, PatchOperation operation) { var current = context.Source; var message = EditableJsonElementHelpers.FindParentOfTarget(ref current, operation.Path); if (message != null) { context.Message = message; return; } var from = context.Source; message = EditableJsonElementHelpers.FindTarget(ref from, operation.From); if (message != null) { context.Message = message; return; } if (operation.Path.Segments.Length == 0) { context.Source = from; return; } var last = operation.Path.Segments.Last(); if (current.Object != null) { current.Object[last.Value] = from; return; } if (current.Array != null) { if (last.Value == "-") { current.Array.Add(from); return; } if (!int.TryParse(last.Value, out var index) || 0 > index || index > current.Array.Count) { context.Message = $"Path `{operation.Path}` is not present in the instance."; return; } if ((index != 0 && last.Value[0] == '0') || (index == 0 && last.Value.Length > 1)) { context.Message = $"Path `{operation.Path}` is not present in the instance."; return; } current.Array.Insert(index, from); return; } context.Message = $"Path `{operation.Path}` is not present in the instance."; }
public void Process(PatchContext context, PatchOperation operation) { if (operation.Path.Segments.Length == 0) { context.Message = "Cannot remove root value."; return; } var current = context.Source; var message = EditableJsonElementHelpers.FindParentOfTarget(ref current, operation.Path); if (message != null) { context.Message = message; return; } var last = operation.Path.Segments.Last(); if (current.Object != null) { if (!current.Object.TryGetValue(last.Value, out _)) { context.Message = $"Path `{operation.Path}` is not present in the instance."; return; } current.Object.Remove(last.Value); return; } if (current.Array != null) { if (last.Value == "-") { current.Array.RemoveAt(current.Array.Count - 1); return; } if (!int.TryParse(last.Value, out var index) || 0 > index || index >= current.Array.Count) { context.Message = $"Path `{operation.Path}` is not present in the instance."; return; } if ((index != 0 && last.Value[0] == '0') || (index == 0 && last.Value.Length > 1)) { context.Message = $"Path `{operation.Path}` is not present in the instance."; return; } current.Array.RemoveAt(index); return; } context.Message = $"Path `{operation.Path}` is not present in the instance."; }
/// <summary> /// Applies the patch to a JSON document. /// </summary> /// <param name="source">The JSON document.</param> /// <returns>A result object containing the output JSON and a possible error message.</returns> public PatchResult Apply(JsonElement source) { var context = new PatchContext(new EditableJsonElement(source)); foreach (var operation in Operations) { operation.Handle(context); if (context.Message != null) { break; } context.Index++; } return(new PatchResult(context)); }
public void Process(PatchContext context, PatchOperation operation) { var current = context.Source; var message = EditableJsonElementHelpers.FindTarget(ref current, operation.Path); if (message != null) { context.Message = message; return; } if (current.ToElement().IsEquivalentTo(operation.Value)) { return; } context.Message = $"Path `{operation.Path}` is not equal to the indicated value."; }
internal PatchResult(PatchContext context) { Result = context.Source.ToElement(); Error = context.Message; Operation = context.Index; }