Package org.apache.avro

# Class JsonSchemaParser

java.lang.Object
org.apache.avro.JsonSchemaParser

All Implemented Interfaces:
  `FormattedSchemaParser`

public class JsonSchemaParser extends Object implements FormattedSchemaParser
Schema parser for JSON formatted schemata. This initial implementation simply delegates to the `Schema.Parser` class, though it should be refactored out of there.

Note: this class is intentionally not available via the Java `ServiceLoader`, as its use is hardcoded as fallback when no service exists. This enables users to reliably override the standard JSON parser as well.

## Constructor Summary

Constructors
Constructor
Description
`JsonSchemaParser()`

## Method Summary

Modifier and Type
Method
Description
`Schema`
`parse(ParseContext parseContext, URI baseUri, CharSequence formattedSchema)`
Parse schema definitions from a text based source.
`static Schema`
`parseInternal(String... fragments)`
Parse a schema written in the internal (JSON) format without any validations.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### JsonSchemaParser

public JsonSchemaParser()

## Method Details

### parseInternal

public static Schema parseInternal(String... fragments)

Parse a schema written in the internal (JSON) format without any validations.

Using this method is only safe if used to parse a write schema (i.e., a schema used to read Avro data). Other usages, for example by generated Avro code, can cause interoperability problems.

Use with care and sufficient testing!

Parameters:
  `fragments` - one or more strings making up the schema (some schemata exceed the compiler limits)
Returns:
  the parsed schema

### parse

public Schema parse(ParseContext parseContext, URI baseUri, CharSequence formattedSchema) throws IOException, SchemaParseException
Description copied from interface: `FormattedSchemaParser`

Parse schema definitions from a text based source.

## Notes for implementers:

- Schema definitions are expected not to be in the format the parser expects. So when the input clearly doesn't make sense (e.g., reading "/**" when expecting JSON), it is a good idea not to do anything (especially calling methods on the @code ParseContext}).
- The parameter `parseContext` is not thread-safe.
- All named schema definitions that are parsed should be added to the provided `ParseContext`.
- Optionally, you may return a "main" schema. Some schema definitions have one, for example the schema defined by the root of the JSON document in a standard schema definition. If unsure, return `null`.
- If parsing fails, throw a `SchemaParseException`. This will let the parsing process recover and continue.
- Throwing anything other than a `SchemaParseException` will abort the parsing process, so reserve that for rethrowing exceptions.

Specified by:
  `parse` in interface `FormattedSchemaParser`
Parameters:
  `parseContext` - the current parse context: all named schemata that are parsed should be added here, otherwise resolving schemata can fail; contains all previously known types
  `baseUri` - the base location of the schema, or `null` if not known
  `formattedSchema` - the text of the schema definition(s) to parse
Returns:
  the main schema, if any
Throws:
  `IOException` - when the schema cannot be read
  `SchemaParseException` - when the schema cannot be parsed