Exemplo n.º 1
0
        /// <inheritdoc />
        internal override Result Apply(ref JToken token, JsonSerializer serializer)
        {
            var pointer = JTokenPointer.Get(token, Path);

            if (pointer.IsFailure)
            {
                return(Result.Fail(pointer.Error));
            }

            var jValue = Value.GetJToken(serializer);

            switch (pointer.Value)
            {
            case JRootPointer _:
            {
                token = jValue;
                return(Result.Ok());
            }

            case JObjectPointer jObjectPointer:
            {
                return(jObjectPointer.SetValue(jValue));
            }

            case JArrayPointer jArrayPointer:
            {
                return(jArrayPointer.SetValue(jValue));
            }

            default:
                throw new JsonPatchException(JsonPatchMessages.UnknownPathPointer);
            }
        }
Exemplo n.º 2
0
        public void RootArrayPointer()
        {
            JArray jArray = JArray.Parse("[]");

            var pointer = JTokenPointer.Get(jArray, "");

            pointer.IsSuccess.ShouldBeTrue();
            pointer.Value.ShouldBeOfType <JRootPointer>();
        }
Exemplo n.º 3
0
        public void FinalTokenIsNeitherObjectNorArray()
        {
            JObject jObject = JObject.Parse("{ \"var\": 3 }");

            var pointer = JTokenPointer.Get(jObject, "/var/bar");

            pointer.IsFailure.ShouldBeTrue();
            pointer.Error.ShouldContain("'/var/bar'");
        }
Exemplo n.º 4
0
        public void PointerToAbsentObjectProperty()
        {
            JObject jObject = JObject.Parse("{ \"var\": [] }");

            var pointer = JTokenPointer.Get(jObject, "/boo/var");

            pointer.IsFailure.ShouldBeTrue();
            pointer.Error.ShouldContain("'/boo'");
        }
Exemplo n.º 5
0
        public void RootObjectPointer()
        {
            JObject jObject = JObject.Parse("{}");

            var pointer = JTokenPointer.Get(jObject, "");

            pointer.IsSuccess.ShouldBeTrue();
            pointer.Value.ShouldBeOfType <JRootPointer>();
        }
Exemplo n.º 6
0
        public void PointerThroughPrimitiveType()
        {
            JObject jObject = JObject.Parse("{ \"var\": 3 }");

            var pointer = JTokenPointer.Get(jObject, "/var/bar/foo");

            pointer.IsFailure.ShouldBeTrue();
            pointer.Error.ShouldContain("'/var'");
            pointer.Error.ShouldContain("'Integer'");
        }
Exemplo n.º 7
0
        public void PointerToPropertyInArray()
        {
            JArray jArray = JArray.Parse("[ {}, {} ]");

            var pointer = JTokenPointer.Get(jArray, "/var/bar");

            pointer.IsFailure.ShouldBeTrue();
            pointer.Error.ShouldContain("''");
            pointer.Error.ShouldContain("'var'");
        }
Exemplo n.º 8
0
        public void PointerToAbsentArrayIndex_InTheEndOfPath()
        {
            JArray jArray = JArray.Parse("[ {}, {} ]");

            var pointer = JTokenPointer.Get(jArray, "/4");

            pointer.IsFailure.ShouldBeTrue();
            pointer.Error.ShouldContain("''");
            pointer.Error.ShouldContain("'4'");
        }
Exemplo n.º 9
0
        public void PointerToArrayInsideObject()
        {
            JObject jObject = JObject.Parse("{ \"var\": [] }");

            var pointer = JTokenPointer.Get(jObject, "/var/-");

            pointer.IsSuccess.ShouldBeTrue();
            var jArrayPointer = pointer.Value.ShouldBeOfType <JArrayPointer>();

            var(jArr, pathPart) = jArrayPointer;

            jObject.GetValue("var").ShouldBeSameAs(jArr);
            pathPart.ShouldBe("-");
        }
Exemplo n.º 10
0
        public void PointerToObjectInsideArray()
        {
            JArray jArray = JArray.Parse("[ {}, {} ]");

            var pointer = JTokenPointer.Get(jArray, "/1/bar");

            pointer.IsSuccess.ShouldBeTrue();
            var jObjectPointer = pointer.Value.ShouldBeOfType <JObjectPointer>();

            var(jObj, pathPart) = jObjectPointer;

            jArray[1].ShouldBeSameAs(jObj);
            pathPart.ShouldBe("bar");
        }
Exemplo n.º 11
0
        public void NestedObjectPropertyPointer()
        {
            JObject jObject = JObject.Parse("{ \"var\": {} }");

            var pointer = JTokenPointer.Get(jObject, "/var/boo");

            pointer.IsSuccess.ShouldBeTrue();
            var jObjectPointer = pointer.Value.ShouldBeOfType <JObjectPointer>();

            var(jObj, pathPart) = jObjectPointer;

            jObject.GetValue("var").ShouldBeSameAs(jObj);
            pathPart.ShouldBe("boo");
        }
Exemplo n.º 12
0
        public void RootArrayItemPointer()
        {
            JArray jArray = JArray.Parse("[]");

            var pointer = JTokenPointer.Get(jArray, "/0");

            pointer.IsSuccess.ShouldBeTrue();
            var jArrayPointer = pointer.Value.ShouldBeOfType <JArrayPointer>();

            var(jArr, pathPart) = jArrayPointer;

            jArray.ShouldBeSameAs(jArr);
            pathPart.ShouldBe("0");
        }
Exemplo n.º 13
0
        public void SpecialSymbolsInPaths()
        {
            JObject jObject = JObject.Parse("{ \"foo/bar~\": {} }");

            var pointer = JTokenPointer.Get(jObject, "/foo~1bar~0/7");

            pointer.IsSuccess.ShouldBeTrue();
            var jObjectPointer = pointer.Value.ShouldBeOfType <JObjectPointer>();

            var(jObj, pathPart) = jObjectPointer;

            jObject.GetValue("foo/bar~").ShouldBeSameAs(jObj);
            pathPart.ShouldBe("7");
        }