Copyright | © 2022 IOHK |
---|---|
License | Apache-2.0 |
Safe Haskell | None |
Language | Haskell2010 |
This module provides a utility function
variants
for parsing
JSON values using one out of many parsers.
Documentation
:: String |
Error message suffix in case of parse failure. |
-> [ Variant a] |
Possible variants. |
-> Value |
Value to parse. |
-> Parser a |
Construct a parser for
a
from parsers for its variants.
The parser succeeds iff exactly one of the predicates of the variants succeeds and the parser of that variant also succeeds. Using the predicate in this way improves error messages in case of parse failure.
For example,
variants
can be used to parse a JSON value
into a disjoint sum (
Either
) without needing a tag
representing the
Left
/
Right
cases.
Instead, the predicates of the variants can be used to disambiguate a
Value
by checking the presence of absence of certain JSON object keys.
Specification of a JSON parser suitable for
variants
.