Package org.apache.avro

# Class SchemaParser

java.lang.Object
org.apache.avro.SchemaParser

public class SchemaParser extends Object
Avro schema parser for text-based formats like JSON, IDL, etc.

Parses formatted (i.e., text based) schemata from a given source using the available `FormattedSchemaParser` implementations, and returns the first result. This means it can transparently handle any schema format. The Avro project defines a JSON based format and an IDL format (the latter available as a separate dependency), but you can also provide your own.

The parser can handle various text based sources. If the source contains a UTF encoded latin text based format it can even detect which UTF encoding was used (UTF-8, UTF16BE, UTF16LE, UTF-32BE or UTF32LE).

See Also:
  - `FormattedSchemaParser`
  - `UtfTextUtils`

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static interface`
`SchemaParser.ParseResult`

## Constructor Summary

Constructors
Constructor
Description
`SchemaParser()`
Create a schema parser that validates names using `NameValidator.UTF_VALIDATOR`.
`SchemaParser(NameValidator nameValidator)`
Create a schema parser with the specified name validator.

## Method Summary

Modifier and Type
Method
Description
`List<Schema>`
`getParsedNamedSchemas()`
Get all parsed schemata.
`SchemaParser.ParseResult`
`parse(File file)`
Parse an Avro schema from a file.
`SchemaParser.ParseResult`
`parse(File file, Charset charset)`
Parse an Avro schema from a file written with a specific character set.
`SchemaParser.ParseResult`
`parse(InputStream in)`
Parse an Avro schema from an input stream.
`SchemaParser.ParseResult`
`parse(InputStream in, Charset charset)`
Parse an Avro schema from an input stream.
`SchemaParser.ParseResult`
`parse(Reader in)`
Parse an Avro schema from an input reader.
`SchemaParser.ParseResult`
`parse(CharSequence text)`
Parse an Avro schema from a string.
`SchemaParser.ParseResult`
`parse(URI location, Charset charset)`
Parse an Avro schema from a file written with a specific character set.
`SchemaParser.ParseResult`
`parse(Path file)`
Parse an Avro schema from a file.
`SchemaParser.ParseResult`
`parse(Path file, Charset charset)`
Parse an Avro schema from a file written with a specific character set.
`static Schema`
`parseSingle(String schema)`
Use a default SchemaParser to parse a single schema.
`static Schema`
`parseSingle(Path schemaFile)`
Use a default SchemaParser to parse a single schema from a file.
`Schema`
`resolve(SchemaParser.ParseResult result)`
Deprecated.

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

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

## Constructor Details

### SchemaParser

public SchemaParser()
Create a schema parser that validates names using `NameValidator.UTF_VALIDATOR`. Initially, the list of known (named) schemata is empty.

### SchemaParser

public SchemaParser(NameValidator nameValidator)
Create a schema parser with the specified name validator.

Initially, the list of known (named) schemata is empty.

Note: using `NameValidator.STRICT_VALIDATOR` to validate names is advised for maximum interoperability.

Parameters:
  `nameValidator` - the name validator to use

## Method Details

### parseSingle

public static Schema parseSingle(String schema) throws SchemaParseException

Use a default SchemaParser to parse a single schema. Equivalent to:

`new SchemaParser().parse(schema).mainSchema()`

Parameters:
  `schema` - the formatted schema to parse
Returns:
  the parsed schema
Throws:
  `SchemaParseException` - when the schema is invalid
See Also:
  - `parse(CharSequence)`

### parseSingle

public static Schema parseSingle(Path schemaFile) throws SchemaParseException, IOException

Use a default SchemaParser to parse a single schema from a file. Equivalent to:

`new SchemaParser().parse(schemaFile).mainSchema()`

Parameters:
  `schemaFile` - the formatted schema to parse
Returns:
  the parsed schema
Throws:
  `SchemaParseException` - when the schema is invalid
  `IOException`
See Also:
  - `parse(Path)`

### parse

public SchemaParser.ParseResult parse(File file) throws IOException, SchemaParseException
Parse an Avro schema from a file. The file content is assumed to be UTF text.

Parameters:
  `file` - the file to read
Returns:
  the schema
Throws:
  `IOException` - when the schema cannot be read
  `SchemaParseException` - if parsing the schema failed; contains suppressed underlying parse exceptions if available
See Also:
  - `UTF detection algorithm in UtfTextUtils`

### parse

public SchemaParser.ParseResult parse(File file, Charset charset) throws IOException, SchemaParseException
Parse an Avro schema from a file written with a specific character set.

Parameters:
  `file` - the file to read
  `charset` - the character set of the file contents
Returns:
  the schema
Throws:
  `IOException` - when the schema cannot be read
  `SchemaParseException` - if parsing the schema failed; contains suppressed underlying parse exceptions if available

### parse

public SchemaParser.ParseResult parse(Path file) throws IOException, SchemaParseException
Parse an Avro schema from a file. The file content is assumed to be UTF text.

Parameters:
  `file` - the file to read
Returns:
  the schema
Throws:
  `IOException` - when the schema cannot be read
  `SchemaParseException` - if parsing the schema failed; contains suppressed underlying parse exceptions if available
See Also:
  - `UTF detection algorithm in UtfTextUtils`

### parse

public SchemaParser.ParseResult parse(Path file, Charset charset) throws IOException, SchemaParseException
Parse an Avro schema from a file written with a specific character set.

Parameters:
  `file` - the file to read
  `charset` - the character set of the file contents
Returns:
  the schema
Throws:
  `IOException` - when the schema cannot be read
  `SchemaParseException` - if parsing the schema failed; contains suppressed underlying parse exceptions if available

### parse

public SchemaParser.ParseResult parse(URI location, Charset charset) throws IOException, SchemaParseException
Parse an Avro schema from a file written with a specific character set.

Parameters:
  `location` - the location of the schema resource
  `charset` - the character set of the schema resource
Returns:
  the schema
Throws:
  `IOException` - when the schema cannot be read
  `SchemaParseException` - if parsing the schema failed; contains suppressed underlying parse exceptions if available

### parse

public SchemaParser.ParseResult parse(InputStream in) throws IOException, SchemaParseException
Parse an Avro schema from an input stream. The stream content is assumed to be UTF text. Note that the stream stays open after reading.

Parameters:
  `in` - the stream to read
Returns:
  the schema
Throws:
  `IOException` - when the schema cannot be read
  `SchemaParseException` - if parsing the schema failed; contains suppressed underlying parse exceptions if available
See Also:
  - `UTF detection algorithm in UtfTextUtils`

### parse

public SchemaParser.ParseResult parse(InputStream in, Charset charset) throws IOException, SchemaParseException
Parse an Avro schema from an input stream. Note that the stream stays open after reading.

Parameters:
  `in` - the stream to read
  `charset` - the character set of the stream contents
Returns:
  the schema
Throws:
  `IOException` - when the schema cannot be read
  `SchemaParseException` - if parsing the schema failed; contains suppressed underlying parse exceptions if available

### parse

public SchemaParser.ParseResult parse(Reader in) throws IOException, SchemaParseException
Parse an Avro schema from an input reader.

Parameters:
  `in` - the stream to read
Returns:
  the schema
Throws:
  `IOException` - when the schema cannot be read
  `SchemaParseException` - if parsing the schema failed; contains suppressed underlying parse exceptions if available

### parse

public SchemaParser.ParseResult parse(CharSequence text) throws SchemaParseException
Parse an Avro schema from a string.

Parameters:
  `text` - the text to parse
Returns:
  the schema
Throws:
  `SchemaParseException` - if parsing the schema failed; contains suppressed underlying parse exceptions if available

### getParsedNamedSchemas

public List<Schema> getParsedNamedSchemas()
Get all parsed schemata.

Returns:
  all parsed schemas, in the order they were parsed

### resolve

@Deprecated public Schema resolve(SchemaParser.ParseResult result)
Deprecated.