Giter Club home page Giter Club logo

tavis.jsonpatch's People

Contributors

darrelmiller avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tavis.jsonpatch's Issues

Unnecessary quote (") symbol found in JSON Arrays

It looks like Tavis.JsonPatch puts an extra " at the beginning and end of JSON arrays.


The following example (taken from jsonpatch.com) fails:

The original document
{
  "baz": "qux",
  "foo": "bar"
}
The patch
[
  { "op": "replace", "path": "/baz", "value": "boo" },
  { "op": "add", "path": "/hello", "value": ["world"] },
  { "op": "remove", "path": "/foo"}
]
The result
{
   "baz": "boo",
   "hello": ["world"]
}

This can be written in C#, xUnit.net, using Tavis.JsonPatch as:

[Fact]
public void PatchFromJsonStringReturnsCorrectResult()
{
    // Arrange
    var jsonString = @"
        {
            ""baz"": ""qux"",
            ""foo"": ""bar""
        }";
    var operations = ConvertFrom(JToken.Parse(@"
        [
            { ""op"": ""replace"", ""path"": ""/baz"", ""value"": ""boo"" },
            { ""op"": ""add"", ""path"": ""/hello"", ""value"": [""world""] },
            { ""op"": ""remove"", ""path"": ""/foo""}
        ]"));
    var patchDocument = new PatchDocument(operations.ToArray());

    // Act
    var actual = JToken.Parse(jsonString);
    patchDocument.ApplyTo(actual);

    // Assert
    var expected = JToken.Parse(@"
    {
        ""baz"": ""boo"",
        ""hello"": [""world""]
    }");
    Assert.Equal(expected, actual);
}

Output:

Test 'Xyz.PatchFromJsonStringReturnsCorrectResult' failed: Assert.Equal() Failure
Position: First difference is at position 0
Expected: JObject { "baz": "boo", "hello": [
            "world"
          ] }
Actual:   JObject { "baz": "boo", "hello": "[\r\n  \"world\"\r\n]" }
    Xyz.cs(307,0): at Xyz.PatchFromJsonStringReturnsCorrectResult()

It looks like Tavis.JsonPatch puts an extra " at the beginning and end of JSON arrays, but it shouldn't.


The ConvertFrom function is defined as:

private static IEnumerable<Operation> ConvertFrom(JToken operations)
{
    foreach (dynamic operation in operations)
    {
        var op = operation.op.ToString();
        var path = operation.path.ToString();

        if (op == "replace")
        {
            var value = operation.value.ToString();
            yield return new ReplaceOperation
            {
                Path  = new JsonPointer(path),
                Value = new JValue(value)
            };
        }
        else if (op == "add")
        {
            var value = operation.value.ToString();
            yield return new AddOperation
            {
                Path  = new JsonPointer(path),
                Value = new JValue(value)
            };
        }
        else if (op == "remove")
        {
            yield return new RemoveOperation
            {
                Path  = new JsonPointer(path)
            };
        }
    }
}

Invalid test operation

Hi,
Your test operation is incorrect : it compares Path(target) with target.

The code should be :

protected override void Test(TestOperation operation)
{
    var existingValue = operation.Path.Find(_target);
    if (!existingValue.Equals(operation.Value))
    {
        throw new InvalidOperationException("Value at " + operation.Path.ToString() + " does not match.");
    }
}

Note: I'm unable to make a PR from where I'm writing

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.