Automatically derivable JSON marshallers in the spirit of Haskell's aeson.
Please note, that while tests suggest that the encoders and decoders perform reasonably well, this library has not been optimized in terms of performance.
Getting started with encoding and decoding is very easy:
module README
import JSON.Derive
%language ElabReflection
data MonsterClass = Imp | Goblin | Orc | Dragon
%runElab derive "MonsterClass" [Show,Eq,ToJSON,FromJSON]
record Villain where
constructor MkVillain
name : String
hp : Nat
class : MonsterClass
cronies : List Villain
%runElab derive "Villain" [Show,Eq,ToJSON,FromJSON]
gorgar : Villain
gorgar = MkVillain "Gorgar" 2000 Dragon [MkVillain "Igor" 10 Imp []]
You can give this a try at the REPL:
pack repl docs/src/README.md
README> :exec putStrLn $ encode gorgar
README> :exec printLn $ (decode {a = Villain} (encode gorgar))
More examples can be found in the tutorial.
Besides base
, the following dependencies are needed to
support the automatic deriving of interface implementations:
In addition, the test suit requires the following:
It is nowadays recommended to use a package manager such as pack for installing and managing your Idris2 dependencies.
Personally, I don't think the versatility of using different JSON
representations and thus having to go via Encoder
, Value
, or Object
interfaces is worth the hassle. I therefore added a second library,
which uses just the JSON representation from the
parser-json package.