示例#1
0
        public static string?FindParentOfTarget(ref EditableJsonElement current, JsonPointer path)
        {
            foreach (var segment in path.Segments.Take(path.Segments.Length - 1))
            {
                if (current.Object != null)
                {
                    if (current.Object.TryGetValue(segment.Value, out current))
                    {
                        continue;
                    }
                }
                else if (current.Array != null)
                {
                    if (segment.Value == "-")
                    {
                        current = current.Array.Last();
                        continue;
                    }
                    if (int.TryParse(segment.Value, out var index) &&
                        0 <= index && index < current.Array.Count)
                    {
                        current = current.Array[index];
                        continue;
                    }
                }

                return($"Path `{path}` could not be reached.");
            }

            return(null);
        }
示例#2
0
        private static string?RemoveSource(EditableJsonElement fromParent, string lastSegment, JsonPointer path)
        {
            if (fromParent.Object != null)
            {
                if (fromParent.Object.TryGetValue(lastSegment, out _))
                {
                    fromParent.Object.Remove(lastSegment);
                    return(null);
                }
            }
            else if (fromParent.Array != null)
            {
                if (lastSegment == "-")
                {
                    fromParent.Array.RemoveAt(fromParent.Array.Count - 1);
                    return(null);
                }

                if (int.TryParse(lastSegment, out var index) && -1 <= index && index < fromParent.Array.Count)
                {
                    fromParent.Array.RemoveAt(index);
                    return(null);
                }
            }

            return($"Path `{path}` is not present in the instance.");
        }
示例#3
0
        public static string?FindTarget(ref EditableJsonElement current, JsonPointer path)
        {
            foreach (var segment in path.Segments)
            {
                if (current.Object != null)
                {
                    if (current.Object.TryGetValue(segment.Value, out current))
                    {
                        continue;
                    }
                }
                else if (current.Array != null)
                {
                    if (segment.Value == "-")
                    {
                        current = current.Array.Last();
                        continue;
                    }
                    if (int.TryParse(segment.Value, out var index) &&
                        0 <= index && index < current.Array.Count)
                    {
                        if ((index != 0 && segment.Value[0] == '0') ||
                            (index == 0 && segment.Value.Length > 1))
                        {
                            return($"Path `{path}` is not present in the instance.");
                        }

                        current = current.Array[index];
                        continue;
                    }
                }

                return($"Path `{path}` could not be reached.");
            }

            return(null);
        }