aeson-diff-generic-0.0.2: Apply a json-patch to any haskell datatype.

Safe HaskellNone
LanguageHaskell98

Data.Aeson.Diff.Generic

Contents

Description

This library allows you to apply a json-patch document (rfc6902) directly to a haskell datatype. A JSON Patch document is a sequence of instructions to modify a JSON value. This library allows you to modify the haskell datatype directly by matching it with its JSON representation. It is suitable for use with the HTTP PATCH method.

Synopsis

patch operations

patch :: JsonPatch a => Patch -> a -> Result a Source #

Apply a json patch document to the data.

applyOperation :: JsonPatch a => Operation -> a -> Result a Source #

Apply a single operation to the data.

class (Eq s, ToJSON s, FromJSON s, Typeable s) => JsonPatch s where Source #

This class defines all operations necessary for applying patches. Instances can be written by hand, however it's easier to use the fieldLens class instead, or to derive it automatically using the template haskell functions from the Data.Aeson.Diff.Generic.TH module. The default implementation is based on fieldLens, which matches a string to a existentially quantified getter and setter (the GetSet datatype). The Instances can be found in the Data.Aeson.Diff.Generic.Instances module, which this module exports.

Methods

getAtPointer :: Pointer -> s -> (forall v. JsonPatch v => v -> r) -> Result r Source #

Retrieve the value at the pointer. To get back a json Value use toJSON as the helper function, to get back a Dynamic use toDyn.

getAtPointer :: FieldLens s => Pointer -> s -> (forall v. JsonPatch v => v -> r) -> Result r Source #

Retrieve the value at the pointer. To get back a json Value use toJSON as the helper function, to get back a Dynamic use toDyn.

deleteAtPointer :: Pointer -> s -> (forall v. JsonPatch v => v -> r) -> Result (r, s) Source #

Remove the value at pointer from the data. The original value is returned. To get back a json Value use toJSON as the helper function, to get back a Dynamic use toDyn.

deleteAtPointer :: FieldLens s => Pointer -> s -> (forall v. JsonPatch v => v -> r) -> Result (r, s) Source #

Remove the value at pointer from the data. The original value is returned. To get back a json Value use toJSON as the helper function, to get back a Dynamic use toDyn.

addAtPointer :: Pointer -> s -> r -> (forall v. JsonPatch v => r -> Result v) -> Result s Source #

Add a value at the pointer. To insert a json Value, use fromJSON as the helper function, to insert a Dynamic use getDynamic.

addAtPointer :: FieldLens s => Pointer -> s -> r -> (forall v. JsonPatch v => r -> Result v) -> Result s Source #

Add a value at the pointer. To insert a json Value, use fromJSON as the helper function, to insert a Dynamic use getDynamic.

copyPath :: Pointer -> Pointer -> s -> Result s Source #

copyPath src dst s. Copy the value at src to dest in s. If the types don't match an error is returned

copyPath :: FieldLens s => Pointer -> Pointer -> s -> Result s Source #

copyPath src dst s. Copy the value at src to dest in s. If the types don't match an error is returned

movePath :: Pointer -> Pointer -> s -> Result s Source #

movePath src dst s. Move the value from src to dest in s. If the types don't match an error is returned

movePath :: FieldLens s => Pointer -> Pointer -> s -> Result s Source #

movePath src dst s. Move the value from src to dest in s. If the types don't match an error is returned

replaceAtPointer :: Pointer -> s -> r -> (forall v. JsonPatch v => r -> Result v) -> Result s Source #

Replace the value at the pointer with the new value. To replace using a json Value, use fromJSON as the helper function, to insert a Dynamic use getDynamic.

replaceAtPointer :: FieldLens s => Pointer -> s -> r -> (forall v. JsonPatch v => r -> Result v) -> Result s Source #

Replace the value at the pointer with the new value. To replace using a json Value, use fromJSON as the helper function, to insert a Dynamic use getDynamic.

testAtPointer :: Pointer -> s -> r -> (forall v. JsonPatch v => r -> Result v) -> Result s Source #

Test if the value at the pointer matches the given value (after conversion). Return the data if they match, give an error otherwise.

testAtPointer :: FieldLens s => Pointer -> s -> r -> (forall v. JsonPatch v => r -> Result v) -> Result s Source #

Test if the value at the pointer matches the given value (after conversion). Return the data if they match, give an error otherwise.

getDynamic :: Typeable a => Dynamic -> Result a Source #

Read a Dynamic value into the Result Monad. Gives a type error if the results don't match.

getValueAtPointer :: JsonPatch s => Pointer -> s -> Result Value Source #

Get the json value at the pointer. Gives an error if the destination doesn't exist.

getDataAtPointer :: (JsonPatch s, Typeable a) => Pointer -> s -> Result a Source #

Get the value at the pointer. Gives and error If the types don't match or if the destination doesn't exist.

the fieldLens class

FieldLens provides a simpler way to create JsonPatch instances.

data GetSet s Source #

An existentially quantified getter and setter. The data inside is manipulated using json conversion functions (toJSON, fromJSON), or Data.Dynamic (getDynamic, toDyn, etc...).

Constructors

JsonPatch v => GetSet v (v -> Result s) 

class (Eq s, ToJSON s, FromJSON s, Typeable s) => FieldLens s where Source #

Methods

fieldLens :: Key -> s -> Result (GetSet s) Source #

Map a key to a getter and setter on the given data.

deleteAt :: Key -> s -> (forall v. JsonPatch v => v -> r) -> Result (r, s) Source #

Delete and return the data at the given key. The helper function determines which value to return: toJSON to return an aeson Value, toDyn to return a Dynamic.

insertAt :: Key -> s -> r -> (forall v. JsonPatch v => r -> Result v) -> Result s Source #

Insert a value at the given key. The helper function determines how to convert the value: fromJSON to convert from an aeson Value, getDynamic to convert from a Dynamic.