Interface JsonParser<T>
- Type Parameters:
T- the type of domain object convertible with JSON
- All Known Subinterfaces:
JsonObjectParser<T>
- All Known Implementing Classes:
PathJsonParser,ProductParsers.EmptyProductParser,ProductParsers.VariadicProductParser
This interface is designed for the composition of complex JSON parsers by building them up from simpler ones. For instance, a parser for a primitive data type like an integer might be used as an element of a parser for an object with one or more integer fields.
This interface helps avoid the situation in which a parser is modified without making analogous changes to the corresponding unparser (printer), since both capabilities are implemented by the same object. Moreover, since the parser is defined separately from the domain type it represents, parsers can be defined for types not owned by the developer of the parser, or for types that may be serialized to multiple other formats depending on the context.
We encourage a style of composition emphasizing the overall structure of the supported JSON documents, which is consolidated into a single co-located grammar. A developer should not build a parser by implementing this interface, but rather by building a parser up from pre-existing parsers and combinators. New parsers for leaf-level data types may be defined, of course, but they should be the exception, not the rule.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault javax.json.JsonObjectProduces a JSON Schema document describing the format of JSON documents produced byunparse(T).javax.json.JsonObjectgetSchema(SchemaCache anchors) Produces a JSON Schema document fragment describing the format of JSON documents accepted byparse(JsonValue)and produced byunparse(T).default <S> JsonParser<S> Adapts this parser losslessly from one domain type to another with a two-way conversion.default <S> JsonParser<S> Adapts this parser losslessly from one domain type to another.parse(javax.json.JsonValue json) Attempts to parse a JSON document into the domain typeT.javax.json.JsonValueProduces a JSON document representing the given value.
-
Method Details
-
getSchema
Produces a JSON Schema document fragment describing the format of JSON documents accepted byparse(JsonValue)and produced byunparse(T).The
anchorsparameter is an accumulator for visited sub-parsers, tracking a JSON Schema identifier for each. An implementations may add to the set of anchors, but may not alter or remove an existing anchor, and must not add itself. This allows an arbitrary caller to make the choice of whether to inline a definition at all use-sites or reference a single copy.This method is primarily meant for use from other parsers. To generate a top-level schema for a root-level parser, use
getSchema().- Parameters:
anchors- a mutable collection of JSON Schema identifiers for previously-visited parsers- Returns:
- a JSON Schema document describing the format of JSON documents supported by this parser.
-
parse
Attempts to parse a JSON document into the domain typeT.This method must successfully parse any document generated by
unparse(T), and the resulting domain value must be equal to the one that produced that document. No guarantees are made for documents not generated by theunparsemethod. SeegetSchema()for more on the relationships among these methods.- Parameters:
json- the JSON document to parse into a domain value- Returns:
- a wrapped domain value if parsing succeeds, or failure information otherwise
-
unparse
Produces a JSON document representing the given value.The document produced by this method must validate against the schema returned by
getSchema(), and must be parsed successfully byparse(JsonValue)into a value equal to the one given here.- Parameters:
value- a domain value to encode in JSON- Returns:
- a JSON document representing the given value
-
getSchema
default javax.json.JsonObject getSchema()Produces a JSON Schema document describing the format of JSON documents produced byunparse(T).All documents returned from
unparse(T)must validate against this schema. However,parse(JsonValue)is only required to faithfully parse documents generated byunparse(T). Theparsemethod may reject other documents that validate relative to the schema (for instance, if the document does not maintain invariants required of the domain type), and may accept other documents that do not validate.Individual parsers may make stronger guarantees about the behavior of the
parsemethod. Absent such guarantees, clients of theparsemethod should treat this schema as advisory only: if a document does not validate, then it definitely isn't something thatunparsewould produce.- Returns:
- a JSON Schema document describing the format of JSON documents supported by this parser.
-
map
Adapts this parser losslessly from one domain type to another with a two-way conversion.The data types most convenient for building parsers are not always the ones we actually want to end up with in the business domain. This method turns a parser that works with one (parsing-relevant) type into a parser that works with another (domain-relevant) type (typically the domain-relevant one).
To do this, we need to know the precise relationship between the two types. Specifically, we need a way to turn any value of one into a value of the other, and vice versa. This is captured by the
Convertparameter, which provides a pair of forward and backward transformations.- Type Parameters:
S- the new domain type to convert to- Parameters:
transform- the reversible transformation faithfully mapping betweenTobjects andSobjects- Returns:
- a parser supporting the same JSON documents as this parser, but for domain type
S.
-
map
Adapts this parser losslessly from one domain type to another.The data types most convenient for building parsers are not always the ones we actually want to end up with in the business domain. This method turns a parser that works with one (parsing-relevant) type into a parser that works with another (domain-relevant) type (typically the domain-relevant one).
To do this, we need to know the precise relationship between the two types. Specifically, we need a way to turn any value of one into a value of the other, and vice versa.
- Type Parameters:
S- the new domain type to convert to- Parameters:
from- an infallible transformation from the source type to the target typeto- an infallible transformation to the source type from the target type- Returns:
- a parser supporting the same JSON documents as this parser, but for domain type
S.
-