Coder Social home page Coder Social logo

ember-schema-generator's Introduction

Ember Schema Generator

Ember Schema Generator can be used to generate an Ember schema from a Jackson model. This makes integration between Java and Ember much easier, because you only need to define your data model once, avoiding code duplication.

For our purpuses, a Jackson model is the set of classes that are mapped by Jackson Databind. The mapping can be configured in any way supported by Jackson Databind, usually through Jackson Annotations.

The generated Ember schema is a declarative JSON representation of the mapped classes and their properties. The classes have an optional super type. Each property has a name and a declared type.

To use the schema on the client side load it by using Ember Schema Loader.

License

Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0

Maven

<dependency>
	<groupId>com.github.marcus-nl</groupId>
	<artifactId>ember-schema-generator</artifactId>
	<version>1.0.2</version>
</dependency>

JavaDoc

http://marcus-nl.github.io/ember-schema-generator/

Usage

Create an instance of EmberSchemaGenerator by passing your Jackson ObjectMapper to its constructor. Then register the classes and hierarchies you want included in the model by calling addClass and addHierarchy respectively. Those classes will be processed by inspecting all properties that are known to the ObjectMapper. All classes that were encountered by inspecting those properties will also be processed.

The generator will respect the property filters of the object mapper, so that only those properties that are included by the filters will end up in the schema.

Note that addHierarchy will only add those classes that are known by the object mapper, either through ObjectMapper#registerSubtypes or by using the @JsonSubTypes annotation on the base type.

Example

Given the following data model:

Zoo UML

An EmberSchema can be generated as follows:

ObjectMapper objectMapper = ...;
EmberSchemaGenerator generator = new EmberSchemaGenerator(objectMapper);

generator.addClass(Zoo.class);
generator.addHierarchy(Animal.class);

EmberSchema schema = generator.getEmberSchema();

The EmberSchema can then be converted to JSON in the usual way (example), which will look like this:

{
  "classes" : [ {
    "name" : "Zoo",
    "superType" : null,
    "props" : [ {
      "name" : "name",
      "type" : { "kind" : "attr", "name" : "string" }
    }, {
      "name" : "city",
      "type" : { "kind" : "attr", "name" : "string" }
    }, {
      "name" : "star",
      "type" : { "kind" : "one", "name" : "Animal" }
    }, {
      "name" : "animals",
      "type" : { "kind" : "many", "name" : "Animal" }
    } ]
  }, {
    "name" : "Animal",
    "superType" : null,
    "props" : [ {
      "name" : "name",
      "type" : { "kind" : "attr", "name" : "string" }
    }, {
      "name" : "type",
      "type" : { "kind" : "attr", "name" : "string" }
    } ]
  }, {
    "name" : "Elephant",
    "superType" : "Animal",
    "props" : [ {
      "name" : "trunkLength",
      "type" : { "kind" : "attr", "name" : "number" }
    } ]
  }, {
    "name" : "Lion",
    "superType" : "Animal",
    "props" : [ {
      "name" : "hasManes",
      "type" : { "kind" : "attr", "name" : "boolean" }
    } ]
  } ]
}

ember-schema-generator's People

Contributors

marcus-nl avatar marsik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

marsik

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.