GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - pbrisbin/jsonpatch: Haskell package for parsing and applying JSON Patches

This repository was archived by the owner on Sep 30, 2025. It is now read-only.
BranchesTags
Open more actions menu

Latest commit

 

History

40 Commits
40 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
examples
examples
 
 
headroom-templates
headroom-templates
 
 
src/Data
src/Data
 
 
test
test
 
 
.gitignore
.gitignore
 
 
.headroom.yaml
.headroom.yaml
 
 
.hlint.yaml
.hlint.yaml
 
 
.releaserc.yaml
.releaserc.yaml
 
 
.restyled.yaml
.restyled.yaml
 
 
.stack-all
.stack-all
 
 
CHANGELOG.md
CHANGELOG.md
 
 
COPYING
COPYING
 
 
README.lhs
README.lhs
 
 
README.md
README.md
 
 
fourmolu.yaml
fourmolu.yaml
 
 
jsonpatch.cabal
jsonpatch.cabal
 
 
justfile
justfile
 
 
package.yaml
package.yaml
 
 
spec_tests.json
spec_tests.json
 
 
stack-lts20.yaml
stack-lts20.yaml
 
 
stack-lts21.yaml
stack-lts21.yaml
 
 
stack-lts22.yaml
stack-lts22.yaml
 
 
stack-lts23.yaml
stack-lts23.yaml
 
 
stack-nightly.yaml
stack-nightly.yaml
 
 
stack.yaml
stack.yaml
 
 
stack.yaml.lock
stack.yaml.lock
 
 
tests.json
tests.json
 
 
weeder.toml
weeder.toml
 
 

Repository files navigation

Note

All of my GitHub repositories have been archived and will be migrated to Codeberg as I next work on them. This repository either now lives, or will live, at:

https://codeberg.org/pbrisbin/jsonpatch

If you need to report an Issue or raise a PR, and this migration hasn't happened yet, send an email to [email protected].

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 (FromJSON, ToJSON, Result(..), Value, fromJSON)
import Data.Aeson.Encode.Pretty
import Data.Aeson.QQ (aesonQQ)
import Data.ByteString (ByteString)
import Data.ByteString.Lazy qualified as BSL
import Data.Text (Text)
import GHC.Generics (Generic)

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 = patchValue 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"]
}

AsValue Example

The polymorphic patchAsValue function is also available, which provides the following benefits over patchValue:

  1. The patches argument can be any AsValue (from aeson-optics), meaning you can give it directly a ByteString, Value, or Text. Parse errors turning it into [Patch] will be normalized to PatchError.
  2. The target argument can be any type with FromJSON and ToJSON. This means you can patch any of your domain types directly. AsValue would've worked here too, but your domain types are far less likely to have that instance.
data Dog = Dog
  { name :: Text
  , isGood :: Bool
  }
  deriving stock Generic
  deriving anyclass (FromJSON, ToJSON)

fido :: Dog
fido = Dog "fido" False -- gasp!

bytes :: ByteString
bytes = "[{ \"op\":\"replace\", \"path\":\"/isGood\", \"value\":true }]"

result2 :: Either PatchError Dog
result2 = patchAsValue bytes fido
main :: IO ()
main = either (fail . displayException) (BSL.putStr . encodePretty) result2

The above program outputs:

{
  "isGood": true,
  "name": "fido"
}

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.

About

Haskell package for parsing and applying JSON Patches

hackage.haskell.org/package/jsonpatch

Resources

Readme
AGPL-3.0 license
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.