jsonpatch-0.1.0.0: JSON Patch parsing and application
Copyright(c) 2025 Patrick Brisbin
LicenseAGPL-3
Maintainerpbrisbin@gmail.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellSafe-Inferred
LanguageGHC2021

Data.Aeson.Optics.Ext

Description

 
Synopsis

Documentation

atKey :: Key -> AffineTraversal' Value (Maybe Value) Source #

Like key, but uses at instead of ix. This is handy when adding and removing object keys:

>>> "{\"a\": 100, \"b\": 200}" & atKey "a" .~ Nothing
"{\"b\":200}"
>>> "{\"a\": 100, \"b\": 200}" & atKey "c" ?~ String "300"
"{\"a\":100,\"b\":200,\"c\":\"300\"}"

atNth :: Int -> AffineTraversal' Value (Maybe Value) Source #

Like atKey, but for Arrays

Adding shifts all later elements right:

>>> ['a', 'b'] & atNth 1 ?~ 'x'
['a', 'x', 'b']

Removing shifts all later elements left:

>>> ['a', 'b', 'c'] & atNth 1 .~ Nothing
['a', 'c']

NOTE: this function will also index objects, in which case this behaves exactly like atKey. This is necessary for our use-case and probably means we could never upstream this.

>>> {"0": 'a', "1": 'b'} & atNth 1 ?~ 'x'
{"0": 'a', "1": 'x', "2": 'b'}