wnameless / japison

A JSON API (application/vnd.api+json) partial implementation which focuses on serialization and deserialization between API objects and Java objects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maven Central

Japison

A lightweight implementation of JSON API specification(http://jsonapi.org/).

Maven Repo

<dependency>
    <groupId>com.github.wnameless.jsonapi</groupId>
    <artifactId>japison</artifactId>
    <version>0.2.0</version>
</dependency>

Quick Start

Static import first

import static com.github.wnameless.jsonapi.JsonApi.*;

Example resource

public class Res {
    private Long id;
    private String data;
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getData() { return data; }
    public void setData(String data) { this.data = data; }
    public Res(Long id, String data) { this.id = id; this.data = data; }
  }
Res res =  new Res();
res.setId(123);
res.setData("abc");

Create a resource document

ResourceDocument resourceDoc = resourceDocument(res, "Res", res.getId().toString());
System.out.println(resourceDoc.toJson());
# {"data":{"type":"Res","id":"123","attributes":{"id":123,"data":"abc"}}}

Create a resources document

ResourcesDocument resourcesDoc = resourcesDocument(Arrays.asList(res, new Res(456L, "def")), "Res",  r -> r.getId().toString());
System.out.println(resourcesDoc.toJson());
# {"data":[{"type":"Res","id":"123","attributes":{"id":123,"data":"abc"}},{"type":"Res","id":"456","attributes":{"id":456,"data":"def"}}]}

Create an errors document

ErrorsDocument errors = errorsDocument();
errors.setErrors(Arrays.asList(error().withStatus("500").withTitle("Oops!").withDetail("Unknown?")));
System.out.println(errors.toJson());
# {"errors":[{"status":"500","title":"Oops!","detail":"Unknown?"}]}

Since v0.2.0, users can use ObjectMapperFactory to set their own Jackson ObjectMapper
It will take affect globally within the entire Japison library

ObjectMapperFactory.setObjectMapper(customObjectMapper);

About

A JSON API (application/vnd.api+json) partial implementation which focuses on serialization and deserialization between API objects and Java objects

License:Apache License 2.0


Languages

Language:Java 100.0%