jddf-gson

JSON Data Definition Format validation with Gson

License

License

Categories

Categories

Gson Data JSON
GroupId

GroupId

io.jddf.gson
ArtifactId

ArtifactId

jddf-gson
Last Version

Last Version

0.1.2
Release Date

Release Date

Type

Type

jar
Description

Description

jddf-gson
JSON Data Definition Format validation with Gson
Project URL

Project URL

https://github.com/jddf/jddf-java
Source Code Management

Source Code Management

https://github.com/jddf/jddf-java

Download jddf-gson

How to add to project

<!-- https://jarcasting.com/artifacts/io.jddf.gson/jddf-gson/ -->
<dependency>
    <groupId>io.jddf.gson</groupId>
    <artifactId>jddf-gson</artifactId>
    <version>0.1.2</version>
</dependency>
// https://jarcasting.com/artifacts/io.jddf.gson/jddf-gson/
implementation 'io.jddf.gson:jddf-gson:0.1.2'
// https://jarcasting.com/artifacts/io.jddf.gson/jddf-gson/
implementation ("io.jddf.gson:jddf-gson:0.1.2")
'io.jddf.gson:jddf-gson:jar:0.1.2'
<dependency org="io.jddf.gson" name="jddf-gson" rev="0.1.2">
  <artifact name="jddf-gson" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.jddf.gson', module='jddf-gson', version='0.1.2')
)
libraryDependencies += "io.jddf.gson" % "jddf-gson" % "0.1.2"
[io.jddf.gson/jddf-gson "0.1.2"]

Dependencies

runtime (1)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.8.5

test (1)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.3.1

Project Modules

There are no modules declared in this project.

jddf-java

Documentation on JavaDoc.io: https://javadoc.io/doc/io.jddf.gson/jddf-gson

This package is a Java implementation of JSON Data Definition Format. In particular, it lets you:

  1. Validate input data is valid against a JDDF schema,
  2. Get a list of validation errors from that input data, and
  3. Build your own tooling on top of JSON Data Definition Format

This package integrates with Google's gson, a popular JSON implementation for Java. If you would like support for another JSON implementation for Java, please open a GitHub ticket! Your request will be warmly welcomed.

Installation

If you're using Gradle:

dependencies {
  implementation 'io.jddf.gson:jddf-gson:0.1.2'
}

Or Maven:

<dependency>
  <groupId>io.jddf.gson</groupId>
  <artifactId>jddf-gson</artifactId>
  <version>0.1.2</version>
</dependency>

Usage

The three most important classes offered by this package are:

Here's an example of all of this in action:

// You can use Gson to convert JSON input into a JDDF schema.
//
// You can also just construct an instance of Schema directly, and use the
// getter/setter methods yourself.
String schemaJson = "{\"properties\":" +
  "{\"name\": {\"type\": \"string\"}," +
  "\"age\": {\"type\": \"uint32\"}," +
  "\"phones\": {\"elements\": {\"type\": \"string\"}}}}";

Gson gson = new Gson();
Schema schema = gson.fromJson(schemaJson, Schema.class);

// JsonElement is a class from Gson. It represents generic JSON data.
//
// This input data is completely valid against the schema we just constructed.
String inputOkJson = "{\"name\": \"John Doe\", \"age\": 43, \"phones\": [\"+44 1234567\", \"+44 2345678\"]}";
JsonElement inputOk = gson.fromJson(inputOkJson, JsonElement.class);

// This input data has problems. "name" is missing, "age" has the wrong type,
// and "phones[1]" has the wrong type.
String inputBadJson = "{\"age\": \"43\", \"phones\": [\"+44 1234567\", 442345678]}";
JsonElement inputBad = gson.fromJson(inputBadJson, JsonElement.class);

Validator validator = new Validator();
List<ValidationError> errorsOk = validator.validate(schema, inputOk);
List<ValidationError> errorsBad = validator.validate(schema, inputBad);

// inputOk satsfies the schema we're using, so we don't get any validation
// errors from it.
System.out.println(errorsOk.size()) // 0

// inputBad had three validation problems, and so we get back three errors.
System.out.println(errorsBad.size()) // 3

// The first error indicates that "name" is missing.
//
// []
// [properties, name]
System.out.println(errorsBad.get(0).getInstancePath());
System.out.println(errorsBad.get(0).getSchemaPath());

// The second error indicates that "age" has the wrong type.
//
// [age]
// [properties, age, type]
System.out.println(errorsBad.get(1).getInstancePath());
System.out.println(errorsBad.get(1).getSchemaPath());


// The second error indicates that "phones[1]" has the wrong type.
//
// [phones, 1]
// [properties, phones, elements, type]
System.out.println(errorsBad.get(2).getInstancePath());
System.out.println(errorsBad.get(2).getSchemaPath());
io.jddf.gson

JSON Data Definition Format

A simple schema language for JSON

Versions

Version
0.1.2
0.1.1
0.1.0