Move() public method

Removes value at specified location and add it to the target location. Will result in, for example: { "op": "move", "from": "/a/b/c", "path": "/a/b/d" }
public Move ( string from, string path ) : JsonPatchDocument
from string source location
path string target location
return JsonPatchDocument
Ejemplo n.º 1
0
        public void MovePropertyValue_ForExpandoObject_WithCustomNamingStrategy()
        {
            // Arrange
            var contractResolver = new DefaultContractResolver
            {
                NamingStrategy = new TestNamingStrategy()
            };

            dynamic targetObject = new ExpandoObject();

            targetObject.customStringProperty        = "A";
            targetObject.customAnotherStringProperty = "B";

            var patchDocument = new JsonPatchDocument();

            patchDocument.Move("StringProperty", "AnotherStringProperty");
            patchDocument.ContractResolver = contractResolver;

            // Act
            patchDocument.ApplyTo(targetObject);
            var cont = targetObject as IDictionary <string, object>;

            cont.TryGetValue("customStringProperty", out var valueFromDictionary);

            // Assert
            Assert.Equal("A", targetObject.customAnotherStringProperty);
            Assert.Null(valueFromDictionary);
        }
        public void Move_FallsbackToPropertyName_WhenJsonPropertyAttributeName_IsEmpty()
        {
            // Arrange
            var patchDocument = new JsonPatchDocument <JsonPropertyWithNoPropertyName>();

            // Act
            patchDocument.Move(m => m.StringProperty, m => m.StringProperty2);

            // Assert
            var fromPath = patchDocument.Operations.First().from;

            Assert.Equal("/StringProperty", fromPath);
            var toPath = patchDocument.Operations.First().path;

            Assert.Equal("/StringProperty2", toPath);
        }
Ejemplo n.º 3
0
        public void Move_WithExpression_FallsbackToPropertyName_WhenJsonPropertyName_IsEmpty()
        {
            // Arrange
            var patchDoc = new JsonPatchDocument <JsonPropertyWithNoPropertyName>();

            patchDoc.Move(m => m.StringProperty, m => m.StringProperty2);
            var serialized = JsonConvert.SerializeObject(patchDoc);

            // Act
            var deserialized =
                JsonConvert.DeserializeObject <JsonPatchDocument <JsonPropertyWithNoPropertyName> >(serialized);

            // Assert
            var fromPath = deserialized.Operations.First().from;

            Assert.Equal("/StringProperty", fromPath);
            var toPath = deserialized.Operations.First().path;

            Assert.Equal("/StringProperty2", toPath);
        }