Interface JsonValidationService

  • All Superinterfaces:
    JsonSchemaReaderFactory

    public interface JsonValidationService
    extends JsonSchemaReaderFactory
    The facade interface for creating JSON validation objects.

    The following example shows how to read a JSON schema from a string:

     
     JsonValidationService service = JsonValidationService.newInstance();
     StringReader reader = new StringReader("{\"type\": \"integer\"}");
     JsonSchema schema = service.readSchema(reader);
     
     

    Alternatively, a JSON schema can be built programmatically with JsonSchemaBuilder.

     
     JsonSchemaBuilderFactory factory = service.createSchemaBuilderFactory();
     JsonSchemaBuilder builder = factory.createBuilder();
     JsonSchema schema = builder.withType(InstanceType.INTEGER).build();
     
     

    All the methods in this class are safe for use by multiple concurrent threads. For most use-cases, only one instance of JsonValidationService is required within the application.

    Author:
    leadpony
    See Also:
    Java API for JSON Processing (JSON-P), Java API for JSON Binding (JSON-B)
    • Method Detail

      • newInstance

        static JsonValidationService newInstance()
        Creates a new instance of this type.
        Returns:
        newly created instance of this type, never be null.
        Throws:
        JsonException - if an error is encountered while creating the instance.
      • createSchemaReaderFactory

        JsonSchemaReaderFactory createSchemaReaderFactory()
        Creates a factory for creating JSON schema readers with default configuration.
        Returns:
        newly created instance of JSON schema reader factory.
      • createSchemaReaderFactoryBuilder

        JsonSchemaReaderFactoryBuilder createSchemaReaderFactoryBuilder()
        Creates a builder for building a JSON schema reader factory.
        Returns:
        newly created instance of JSON schema reader factory builder.
      • readSchema

        default JsonSchema readSchema​(InputStream in)
        Reads a JSON schema from a byte stream. The character encoding of the stream is determined as described in RFC 7159.
        Parameters:
        in - the byte stream from which a JSON schema is to be read. The specified stream will be closed automatically in this method.
        Returns:
        the read JSON schema.
        Throws:
        NullPointerException - if the specified in is null.
        JsonException - if an I/O error occurs while reading.
        JsonValidatingException - if the reader found problems during validation of the schema.
      • readSchema

        default JsonSchema readSchema​(InputStream in,
                                      Charset charset)
        Reads a JSON schema from a byte stream encoded by the specified charset. The bytes of the stream will be decoded to characters using the specified charset.
        Parameters:
        in - the byte stream from which a JSON schema is to be read. The specified stream will be closed automatically in this method.
        charset - the character set.
        Returns:
        the read JSON schema.
        Throws:
        NullPointerException - if the specified in or charset is null.
        JsonException - if an I/O error occurs while reading.
        JsonValidatingException - if the reader found problems during validation of the schema.
      • readSchema

        default JsonSchema readSchema​(Reader reader)
        Reads a JSON schema from a character stream.
        Parameters:
        reader - the character stream from which a JSON schema is to be read. The specified reader will be closed automatically in this method.
        Returns:
        the read JSON schema.
        Throws:
        NullPointerException - if the specified reader is null.
        JsonException - if an I/O error occurs while reading.
        JsonValidatingException - if the reader found problems during validation of the schema.
      • readSchema

        default JsonSchema readSchema​(Path path)
        Reads a JSON schema from a path.
        Parameters:
        path - the path from which a JSON schema is to be read.
        Returns:
        the read JSON schema.
        Throws:
        NullPointerException - if the specified path is null.
        JsonException - if an I/O error occurs while reading.
        JsonValidatingException - if the reader found problems during validation of the schema.
      • createValidationConfig

        ValidationConfig createValidationConfig()
        Creates a configuration for JsonParser or JsonReader with validation functionality. The map generated from the configuration can be passed to the methods createParserFactory(Map) and createReaderFactory(Map).
        Returns:
        newly created configuration, never be null.
      • createParserFactory

        JsonParserFactory createParserFactory​(Map<String,​?> config)
        Creates a parser factory for creating JsonParser instances. Parsers created by the factory can validate JSON documents while parsing.

        The factory is configured with the specified map of configuration properties.

        Recommended way to create the configuration properties is to generate it via ValidationConfig.getAsMap(). An instance of ValidationConfig can be created by calling createValidationConfig().

        Parameters:
        config - the map of provider-specific properties to configure the JSON parsers. The map may be empty or null.
        Returns:
        newly created instance of JsonParserFactory, which is defined in the JSON Processing API.
      • createParserFactory

        JsonParserFactory createParserFactory​(Map<String,​?> config,
                                              JsonSchema schema,
                                              ProblemHandlerFactory handlerFactory)
        Creates a parser factory for creating JsonParser instances. Parsers created by the factory validate JSON documents while parsing.

        The factory is configured with the specified map of configuration properties.

        Parameters:
        config - the map of provider-specific properties to configure the JSON parsers. The map may be empty or null.
        schema - the JSON schema to apply when validating JSON document.
        handlerFactory - the factory to supply problem handlers, cannot be null.
        Returns:
        newly created instance of JsonParserFactory, which is defined in the JSON Processing API.
        Throws:
        NullPointerException - if any of specified parameters is null.
      • createParser

        JsonParser createParser​(InputStream in,
                                JsonSchema schema,
                                ProblemHandler handler)
        Creates a JSON parser from the specified byte stream, which validates the JSON document while parsing. The character encoding of the stream is determined as specified in RFC 7159.
        Parameters:
        in - the byte stream from which JSON is to be read.
        schema - the JSON schema to apply when validating JSON document.
        handler - the object which handles problems found during the validation, cannot be null.
        Returns:
        newly created instance of JsonParser, which is defined in the JSON Processing API. It must be closed by the method caller after use.
        Throws:
        NullPointerException - if any of specified parameters is null.
        JsonException - if encoding cannot be determined or I/O error occurred.
      • createParser

        JsonParser createParser​(InputStream in,
                                Charset charset,
                                JsonSchema schema,
                                ProblemHandler handler)
        Creates a JSON parser from the specified byte stream, which validates the JSON document while parsing. The bytes of the stream are decoded to characters using the specified charset.
        Parameters:
        in - the byte stream from which JSON is to be read.
        charset - the character set.
        schema - the JSON schema to apply when validating JSON document.
        handler - the object which handles problems found during the validation, cannot be null.
        Returns:
        newly created instance of JsonParser, which is defined in the JSON Processing API. It must be closed by the method caller after use.
        Throws:
        NullPointerException - if any of specified parameters is null.
        JsonException - if encoding cannot be determined or I/O error occurred.
      • createParser

        JsonParser createParser​(Reader reader,
                                JsonSchema schema,
                                ProblemHandler handler)
        Creates a JSON parser from the specified character stream, which validates the JSON document while parsing.
        Parameters:
        reader - the character stream from which JSON is to be read.
        schema - the JSON schema to apply when validating JSON document.
        handler - the object which handles problems found during the validation, cannot be null.
        Returns:
        newly created instance of JsonParser, which is defined in the JSON Processing API. It must be closed by the method caller after use.
        Throws:
        NullPointerException - if any of specified parameters is null.
      • createParser

        JsonParser createParser​(Path path,
                                JsonSchema schema,
                                ProblemHandler handler)
        Creates a JSON parser from the specified path, which validates the JSON document while parsing.
        Parameters:
        path - the path from which JSON is to be read.
        schema - the JSON schema to apply when validating JSON document.
        handler - the object which handles problems found during the validation, cannot be null.
        Returns:
        newly created instance of JsonParser, which is defined in the JSON Processing API. It must be closed by the method caller after use.
        Throws:
        JsonException - if an I/O error occurs while creating parser.
        NullPointerException - if any of specified parameters is null.
      • createReaderFactory

        JsonReaderFactory createReaderFactory​(Map<String,​?> config)
        Creates a reader factory for creating JsonReader instances. Readers created by the factory can validate JSON documents while reading.

        The factory is configured with the specified map of configuration properties.

        Recommended way to create the configuration properties is to generate it via ValidationConfig.getAsMap(). An instance of ValidationConfig can be created by calling createValidationConfig().

        Parameters:
        config - the map of provider specific properties to configure the JSON readers. The map may be empty or null.
        Returns:
        newly created instance of JsonReaderFactory, which is defined in the JSON Processing API.
      • createReaderFactory

        JsonReaderFactory createReaderFactory​(Map<String,​?> config,
                                              JsonSchema schema,
                                              ProblemHandlerFactory handlerFactory)
        Creates a reader factory for creating JsonReader instances. Readers created by the factory validate JSON documents while reading.

        The factory is configured with the specified map of configuration properties.

        Parameters:
        config - the map of provider specific properties to configure the JSON readers. The map may be empty or null.
        schema - the JSON schema to apply when validating JSON document.
        handlerFactory - the factory to supply problem handlers, cannot be null.
        Returns:
        newly created instance of JsonReaderFactory, which is defined in the JSON Processing API.
        Throws:
        NullPointerException - if any of specified parameters is null.
      • createReader

        JsonReader createReader​(InputStream in,
                                JsonSchema schema,
                                ProblemHandler handler)
        Creates a JSON reader from a byte stream, which validates the JSON document while reading. The character encoding of the stream is determined as described in RFC 7159.
        Parameters:
        in - a byte stream from which JSON is to be read.
        schema - the JSON schema to apply when validating JSON document.
        handler - the object which handles problems found during the validation, cannot be null.
        Returns:
        newly created instance of JsonReader, which is defined in the JSON Processing API. It must be closed by the method caller after use.
        Throws:
        NullPointerException - if any of specified parameters is null.
      • createReader

        JsonReader createReader​(InputStream in,
                                Charset charset,
                                JsonSchema schema,
                                ProblemHandler handler)
        Creates a JSON reader from a byte stream, which validates the JSON document while reading. The bytes of the stream are decoded to characters using the specified charset.
        Parameters:
        in - a byte stream from which JSON is to be read.
        charset - the character set.
        schema - the JSON schema to apply when validating JSON document.
        handler - the object which handles problems found during the validation, cannot be null.
        Returns:
        newly created instance of JsonReader, which is defined in the JSON Processing API. It must be closed by the method caller after use.
        Throws:
        NullPointerException - if any of specified parameters is null.
      • createReader

        JsonReader createReader​(Reader reader,
                                JsonSchema schema,
                                ProblemHandler handler)
        Creates a JSON reader from a character stream, which validates the JSON document while reading.
        Parameters:
        reader - the character stream from which JSON is to be read.
        schema - the JSON schema to apply when validating JSON document.
        handler - the object which handles problems found during the validation, cannot be null.
        Returns:
        newly created instance of JsonReader, which is defined in the JSON Processing API. It must be closed by the method caller after use.
        Throws:
        NullPointerException - if any of specified parameters is null.
      • createReader

        JsonReader createReader​(Path path,
                                JsonSchema schema,
                                ProblemHandler handler)
        Creates a JSON reader from a path, which validates the JSON document while reading.
        Parameters:
        path - the path from which JSON is to be read.
        schema - the JSON schema to apply when validating JSON document.
        handler - the object which handles problems found during the validation, cannot be null.
        Returns:
        newly created instance of JsonReader, which is defined in the JSON Processing API. It must be closed by the method caller after use.
        Throws:
        JsonException - if an I/O error occurs while creating reader.
        NullPointerException - if any of specified parameters is null.
      • createJsonProvider

        JsonProvider createJsonProvider​(JsonSchema schema,
                                        ProblemHandlerFactory handlerFactory)
        Creates a JSON provider for validating JSON documents while parsing and reading. This method is intended to be used with Java API for JSON Binding (JSON-B).
        Parameters:
        schema - the JSON schema to apply when validating JSON document.
        handlerFactory - the factory to supply problem handlers, cannot be null.
        Returns:
        newly created instance of JsonProvider, which is defined in the JSON Processing API.
        Throws:
        NullPointerException - if any of specified parameters is null.
      • createProblemPrinter

        ProblemHandler createProblemPrinter​(Consumer<String> lineConsumer)
        Creates a problem handler which will print problems with the aid of the specified line consumer.

        If a customized printer is needed, createProblemPrinterBuilder(Consumer) should be used instead of this method.

        Parameters:
        lineConsumer - the object which will output the message lines to somewhere.
        Returns:
        newly created instance of problem handler.
        Throws:
        NullPointerException - if the specified lineConsumer is null.
      • createProblemPrinter

        ProblemHandler createProblemPrinter​(Consumer<String> lineConsumer,
                                            Locale locale)
        Creates a problem handler which will print problems with the aid of the specified line consumer. The messages will be localized for the specified locale.

        If a customized printer is needed, createProblemPrinterBuilder(Consumer) should be used instead of this method.

        Parameters:
        lineConsumer - the object which will output the message lines to somewhere.
        locale - the locale for which the problem messages will be localized.
        Returns:
        newly created instance of problem handler.
        Throws:
        NullPointerException - if the one of the specified parameters is null.
      • createProblemPrinterBuilder

        ProblemPrinterBuilder createProblemPrinterBuilder​(Consumer<String> lineConsumer)
        Creates a builder instance which can be used to build a customized problem printer.
        Parameters:
        lineConsumer - the object which will output the message lines to somewhere.
        Returns:
        newly created instance of problem printer builder, which can be used to build a problem printer.
        Throws:
        NullPointerException - if the specified lineConsumer is null.
      • getJsonProvider

        JsonProvider getJsonProvider()
        Returns the underlying JSON provider used by this service.
        Returns:
        the underlying JSON provider, never be null.