cardano-wallet-core-2022.7.1: The Wallet Backend for a Cardano node.
Copyright © 2022 IOHK
License Apache-2.0
Safe Haskell None
Language Haskell2010

Cardano.Wallet.Api.Aeson.Variant

Description

This module provides a utility function variants for parsing JSON values using one out of many parsers.

Synopsis

Documentation

variants Source #

Arguments

:: 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.

data Variant a Source #

Specification of a JSON parser suitable for variants .

variant Source #

Arguments

:: String

Error message suffix in case of parse failure.

-> ( Object -> Bool )

Check whether this variant applies.

-> ( Value -> Parser a)

Parser for this variant.

-> Variant a

Define a Variant for parsing a JSON value.

A predicate checks whether a given Value belongs to this variant; the Value is parsed only if this this check succeeds.