public final class JsObjSpecParser extends Object
JsObjSpecParser
class is responsible for creating JSON object parsers based on provided JSON specifications (specs). It allows you to define a JSON schema using a specification and then use that schema to parse JSON data into structured Java objects. This class is part of the JSONValues library, which focuses on providing a type-safe and functional approach to JSON parsing and manipulation.
A parser, in the context of this class, refers to an instance of JsSpecParser
that encapsulates the rules and constraints defined in a JSON specification. The parser ensures that the JSON data being parsed conforms to the specified schema, enforcing rules such as data types, required fields, and user-defined conditions.
Usage of this class typically involves creating an instance with a JSON specification and then using that instance to parse JSON data. If the JSON data does not adhere to the specified schema, a JsParserException
will be raised, providing detailed information about the parsing error.
This class provides three main methods for parsing JSON data:
parse(byte[] bytes)
: Parses a byte array representing JSON data into a JSON object.parse(String str)
: Parses a JSON string into a JSON object.parse(InputStream inputStream)
: Parses JSON data from an input stream into a JSON object. This method also handles potential I/O exceptions when reading from the stream.It's important to note that the provided JSON specification should match the structure and constraints of the JSON data you expect to parse. The parser will enforce these constraints during parsing.
Constructor and Description |
---|
JsObjSpecParser(JsSpec spec)
Constructs a new instance of JsObjSpecParser with the specified JSON specification.
|
Modifier and Type | Method and Description |
---|---|
JsObj |
parse(byte[] bytes)
Parses an array of bytes into a JSON object that must conform to the spec of the parser.
|
JsObj |
parse(InputStream inputstream)
Parses an input stream of bytes into a JSON object that must conform to the spec of the parser.
|
JsObj |
parse(String str)
Parses a string into a JSON object that must conform to the spec of the parser.
|
public JsObjSpecParser(JsSpec spec)
spec
- The JSON specification that defines the schema the JSON object must conform to.NullPointerException
- if the provided spec is null.public JsObj parse(byte[] bytes)
bytes
- A JSON object serialized as an array of bytes.NullPointerException
- if the provided byte array is null.JsParserException
- If parsing fails due to JSON syntax errors or specification violations.public JsObj parse(String str)
str
- A JSON object serialized as a string.NullPointerException
- if the provided string is null.JsParserException
- If parsing fails due to JSON syntax errors or specification violations.public JsObj parse(InputStream inputstream)
inputstream
- The input stream of bytes.NullPointerException
- if the provided input stream is null.JsParserException
- If parsing fails due to JSON syntax errors or specification violations.Copyright © 2023. All rights reserved.