@hackage jsonpatch0.2.0.0

JSON Patch parsing and application

jsonpatch

Hackage Stackage Nightly Stackage LTS CI

Haskell package for parsing and applying JSON Patches.

Example

Typical use cases need only one import:

import Data.JSON.Patch

Our example will make use of a few more libraries:

import Control.Exception (displayException)
import Data.Aeson (Value, Result(..), fromJSON)
import Data.Aeson.Encode.Pretty
import Data.Aeson.QQ (aesonQQ)
import Data.ByteString.Lazy qualified as BSL

The FromJSON instance can be used to build a [Patch]:

patch :: [Patch]
patch = fromResult $ fromJSON [aesonQQ|
  [
    { "op": "replace", "path": "/baz", "value": "boo" },
    { "op": "add", "path": "/hello", "value": ["world"] },
    { "op": "remove", "path": "/foo" }
  ]
|]


-- | Unsafe unwrapping for the sake of example
fromResult :: Result a -> a
fromResult (Success a) = a

The patches can then be applied to a document:

result :: Either PatchError Value
result = applyPatches patch [aesonQQ|
  {
    "baz": "qux",
    "foo": "bar"
  }
|]

The result is in Either PatchError, with displayException available to get a user-friendly message.

main :: IO ()
main = either (fail . displayException) (BSL.putStr . encodePretty) result

The above program outputs:

{
  "baz": "boo",
  "hello": ["world"]
}

Quality

The full test suite from json-patch/json-patch-tests passes. However, some error cases have poor (or misleading) error messages at this time.

License

This package is licensed AGPLv3. See COPYING.