aeson-quick: Quick JSON extractions with Aeson

[ bsd3, json, library ] [ Propose Tags ] [ Report a vulnerability ]

Small DSL on top of Aeson for casual and concise JSON construction and deconstruction.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.1, 0.1.1.1, 0.1.1.2, 0.1.2.0, 0.1.2.1, 0.1.3, 0.2.0
Change log ChangeLog.md
Dependencies aeson, attoparsec, base (>=4.7 && <5), deepseq, template-haskell, text, unordered-containers, vector [details]
License BSD-3-Clause
Copyright 2022 Scott Sadler
Author Scott Sadler
Maintainer scott@scottsadler.de
Category JSON
Home page https://github.com/ssadler/aeson-quick#readme
Bug tracker https://github.com/ssadler/aeson-quick/issues
Source repo head: git clone https://github.com/ssadler/aeson-quick
Uploaded by ssadler at 2022-05-02T21:57:24Z
Distributions NixOS:0.2.0
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 4098 total (26 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for aeson-quick-0.2.0

[back to package description]

aeson-quick

hackage ci

aeson-quick is a small DSL on top of aeson for casual and concise JSON construction and deconstruction.

In essence, it allows you to remove the Objects in a json Value using a simple key lookup syntax, so that the FromJSON typeclass can do the rest of the work, and it can go the other way, too.

aeson-quick structures can be defined using the fromString instance, or using the QuasiQuoter quick for compile time validation, ie: [quick|{foo}|].

Syntax

Transformations

Operation Json Value Quick expression     Haskell type
Parse any value * . a
Key lookup {"foo": *} {foo} a
Optional key {} {foo?} Maybe a
Multiple keys {"foo": *, "bar": *} {foo,bar} (a, b)
Nested object {"foo": {"bar": *}} {foo:{bar}} b
Complex object [{"foo": *, "bar": [*, *]}]     [{a,b:[.]}] [(a, [c])]
Array [*, *] [.] [a]
Array lookup (deconstruct only) [*, *] [.]1 a
Array range (deconstruct only) [*, *] [.]1- [a]

Examples:

Deconstruction:

> let value = [jsonlit|{"foo": true, "bar": [0, 1], "baz": [{"foo": true}, {}, {"foo": false}]}|]

> value .! "{foo}" :: Bool
True

> value .! "{foo, bar}" :: (Bool, [Int])
(True, [0, 1])

> value .! "{baz:[{foo?}]}" :: [Maybe Bool]
[Just True, Nothing, Just False]

-- value is [1,2,3]
> value .! "[.]1" :: Int
2
> value .! "[.]1-" :: [Int]
[2,3]
> value .! "[.]0-2" :: [Int]
[1,2]
> value .! "[.]4" :: Maybe Int
Nothing

Construction:


> "." .% True
true

> "{foo}" .% True
{"foo": true}

> "[{foo}]" .% [1,2,3]
[{"foo":1},{"foo":2},{"foo":3}]

> "{foo:[{bar?}]}" .% [Just 1, Nothing]
{"foo":[{"bar":1},{}]}

Performance

Performance is extremely similar to using Aeson functions directly. See Writeup.md for more details.