Skip to content

hrytsenko/json-data

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quality Gate Status

JSON Data

This library handles JSON entities without transformation into POJOs. It uses of DSLs to manipulate, transform and validate JSON objects.

The JSON entity inherits the class JsonEntity and defines methods to manipulate the underlying object:

class Entity extends JsonEntity<Entity> {

  String getName() {
    return getString("entity.name");
  }

}

Besides, the class JsonBean represents the generic JSON entity for temporary objects with the limited scope.

Serialization

This library uses Jackson to serialize JSON entities. The class JsonParser provides the serialization API.

Manipulation

This library uses JsonPath DSL to manipulate JSON entities. The class JsonEntity provides the manipulation API.

The JsonPath DSL uses expressions to navigate JSON. The following example illustrates the DSL expression that refers an entity name:

Input:
{
  "entity": {
    "name":"ENTITY"
  }
}
 
Expression:
$.entity.name
 
Output:
"ENTITY"

Transformation

This library uses Jolt DSL to transform JSON entities. The class JsonMapper provides the transformation API.

The Jolt DSL uses specifications to express a list of chained transformations. The following example illustrates the specification that creates a collection from a single entity:

Input:
{
  "entity": {
    "name": "ENTITY"
  }
}
 
Specification:
[
  {
    "operation": "shift",
    "spec": {
      "entity": "entities[].entity"
    }
  },
  {
    "operation": "default",
    "spec": {
      "entities": []
    }
  }
]
 
Output:
{
  "entities": [
    {
      "entity": {
        "name": "ENTITY"
      }
    }
  ]
}

Each transformation consumes an output document from a previous transformation and produces an input document for a next transformation. The major transformations are shift and default. The shift transformation maps content from an input document to an output document. It supports iterators, conditions and wildcards that allow to implement a complex logic. The default transformation adds content to an output document. It ensures completeness of an output document.

Validation

This library uses Justify DSL (namely JSON Schema) to validate JSON entities. The class JsonValidator provides the validation API.

The Justify DSL uses schemas to express a structure and a content of a document. The following example illustrates the schema that validates a single entity:

Input:
{
  "entity": {
    "name": "ENTITY"
  }
}
 
Schema:
{
  "type": "object",
  "properties": {
    "entity": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^.{5,30}$"
        }
      },
      "additionalProperties": false,
      "required": [
        "name"
      ]
    }
  },
  "additionalProperties": false,
  "required": [
    "entity"
  ]
}