Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
data / data-json-merge-patchOpenRuns on Liberas

JSON Merge Patch

document (a JSON text) with patch (a JSON text) applied as RFC 7386 defines it, in canonical form: if patch is not an object, the result is patch; otherwise the result is document if it is an object, else {}, with each member of patch applied in turn: a null member removes that name, any other is merged recursively into the member of the same name (absent counts as not an object). Nulls inside arrays and in non-object patches are kept. Only invalid JSON is an error. JSON texts are RFC 8259 JSON of at most 1,048,576 UTF-8 bytes (whitespace: space, tab, LF, CR; a BOM is an error) under I-JSON rules: a duplicate member name (after unescaping), a \u escape of an unpaired surrogate, nesting over 128 deep ([] is depth 1), or a number that rounds to ±infinity is an error; numbers are read as the nearest double (ties to even), so 1e-400 is 0. JSON output is RFC 8785 (JCS) canonical: no whitespace; members sorted by the UTF-16 code units of their names; in strings only " and \ are escaped and U+0000-U+001F written \b \t \n \f \r or \u00xx (lowercase hex), all else literal; numbers as ECMAScript's Number.prototype.toString writes the double (-0 as 0, 1e21 as 1e+21, 1e-7 as 1e-7).

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init data-json-merge-patch --lang rust
cd data-json-merge-patch && liberas build && liberas submit

Save a token once with liberas login (make it on your page, /me, after signing in). init scaffolds the interface, the vectors and a stub in Rust (--lang js for light specs).

Interface (WIT)11 lines›
data-json-merge-patch.wit
package root:component;

world root {
  export liberas:data/patch@0.1.0;
}
package liberas:data@0.1.0 {
  interface patch {
    apply: func(document: string, patch: string) -> result<string, string>;
  }
}
Test vectors22›
data-json-merge-patch.cases.json
[
  {
    "args": {
      "patch": "{\"a\":\"c\"}",
      "document": "{\"a\":\"b\"}"
    }
  },
  {
    "args": {
      "patch": "{\"b\":\"c\"}",
      "document": "{\"a\":\"b\"}"
    }
  },
  {
    "args": {
      "patch": "{\"a\":null}",
      "document": "{\"a\":\"b\"}"
    }
  },
  {
    "args": {
      "patch": "{\"a\":null}",
      "document": "{\"a\":\"b\",\"b\":\"c\"}"
    }
  },
  {
    "args": {
      "patch": "{\"a\":\"c\"}",
      "document": "{\"a\":[\"b\"]}"
    }
  },
  {
    "args": {
      "patch": "{\"a\":[\"b\"]}",
      "document": "{\"a\":\"c\"}"
    }
  },
  {
    "args": {
      "patch": "{\"a\":{\"b\":\"d\",\"c\":null}}",
      "document": "{\"a\":{\"b\":\"c\"}}"
    }
  },
  {
    "args": {
      "patch": "{\"a\":[1]}",
      "document": "{\"a\":[{\"b\":\"c\"}]}"
    }
  },
  {
    "args": {
      "patch": "[\"c\",\"d\"]",
      "document": "[\"a\",\"b\"]"
    }
  },
  {
    "args": {
      "patch": "[\"c\"]",
      "document": "{\"a\":\"b\"}"
    }
  },
  {
    "args": {
      "patch": "null",
      "document": "{\"a\":\"foo\"}"
    }
  },
  {
    "args": {
      "patch": "\"bar\"",
      "document": "{\"a\":\"foo\"}"
    }
  },
  {
    "args": {
      "patch": "{\"a\":1}",
      "document": "{\"e\":null}"
    }
  },
  {
    "args": {
      "patch": "{\"a\":\"b\",\"c\":null}",
      "document": "[1,2]"
    }
  },
  {
    "args": {
      "patch": "{\"a\":{\"bb\":{\"ccc\":null}}}",
      "document": "{}"
    }
  },
  {
    "args": {
      "patch": "{\"title\":\"Hello!\",\"phoneNumber\":\"+01-123-456-7890\",\"author\":{\"familyName\":null},\"tags\":[\"example\"]}",
      "document": "{\"title\":\"Goodbye!\",\"author\":{\"givenName\":\"John\",\"familyName\":\"Doe\"},\"tags\":[\"example\",\"sample\"],\"content\":\"This will be unchanged\"}"
    }
  },
  {
    "args": {
      "patch": "{\"a\":[null,{\"b\":null}]}",
      "document": "{\"a\":1}"
    }
  },
  {
    "args": {
      "patch": "{\"a\":{\"b\":{\"c\":null}}}",
      "document": "{\"a\":{\"b\":1}}"
    }
  },
  {
    "args": {
      "patch": "",
      "document": "{}"
    }
  },
  {
    "args": {
      "patch": "{}",
      "document": "{x}"
    }
  },
  {
    "args": {
      "patch": "{\"a\":2,\"a\":3}",
      "document": "{\"a\":1}"
    }
  },
  {
    "args": {
      "patch": "{}",
      "document": "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 22 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 4,000,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.