| Copyright | (c) 2025 Patrick Brisbin |
|---|---|
| License | AGPL-3 |
| Maintainer | pbrisbin@gmail.com |
| Stability | experimental |
| Portability | POSIX |
| Safe Haskell | Safe-Inferred |
| Language | GHC2021 |
Data.JSON.Patch
Description
Synopsis
- data Patch
- data PatchError
- patchValue :: [Patch] -> Value -> Either PatchError Value
- patchAsValue :: (AsValue patch, FromJSON v, ToJSON v) => patch -> v -> Either PatchError v
Documentation
data PatchError Source #
Instances
| Exception PatchError Source # | |
Defined in Data.JSON.Patch.Error Methods toException :: PatchError -> SomeException # fromException :: SomeException -> Maybe PatchError # displayException :: PatchError -> String # | |
| Show PatchError Source # | |
Defined in Data.JSON.Patch.Error Methods showsPrec :: Int -> PatchError -> ShowS # show :: PatchError -> String # showList :: [PatchError] -> ShowS # | |
patchValue :: [Patch] -> Value -> Either PatchError Value Source #
patchAsValue :: (AsValue patch, FromJSON v, ToJSON v) => patch -> v -> Either PatchError v Source #
A polymorphic version of patchValue
The patch input uses AsValue from aeson-optics, meaning you can supply
a variety of types such as ByteString or Value and it will be parsed into
[ (capturing failure as a Patches]PatchError).
The v input can be any domain type with JSON instances. We don't use
AsValue here as well, even though it provides the same functionality,
because it's unlikely your types will have this instance.
data Person = Person
{ name :: Text
, age :: Int
}
deriving stock Generic
deriving anyclass (FromJSON, ToJSON)
patchPersonR :: PersonId -> Handler Person
patchPersonR id = do
person <- runDB $ get id -- Person "pat" 19
bytes <- getRequestBody -- "[{op:replace, path:/age, value:21}]"
case patchAsValue bytes person of
Left err -> sendResponse 400 $ displayException err
Right updated -> do
runDB $ update id updated
sendResponse 200 updated -- Person "pat" 21
If the patch creates a value that can't parse back to your domain type, that
will also be normalized to PatchError.