to-jsonapi

format entities to jsonapi format.

License

License

Categories

Categories

JSON Data
GroupId

GroupId

com.go2wheel
ArtifactId

ArtifactId

to-jsonapi
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

to-jsonapi
format entities to jsonapi format.
Project URL

Project URL

https://github.com/jianglibo/to-jsonapi
Source Code Management

Source Code Management

https://github.com/jianglibo/to-jsonapi

Download to-jsonapi

How to add to project

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

Dependencies

test (4)

Group / Artifact Type Version
junit : junit jar 4.12
org.skyscreamer : jsonassert jar 1.5.0
org.apache.johnzon : johnzon-mapper jar 1.1.6
org.apache.geronimo.specs : geronimo-json_1.1_spec jar 1.0

Project Modules

There are no modules declared in this project.

to-jsonapi

Do only one thing, formatting entities to jsonapi format. No extra dependencies, except java sdk. It results a hashmap which you can use any json library to convert to json format.

examples

maven dependency.

<!-- https://mvnrepository.com/artifact/com.go2wheel/to-jsonapi -->
<dependency>
    <groupId>com.go2wheel</groupId>
    <artifactId>to-jsonapi</artifactId>
    <version>0.1.0</version>
</dependency>
@JsonapiResource(type="hello")
public class MyUserWithAnnotation extends BaseModel {
	
	@JsonapiField(name = "un")
	private String username;
	
	private Date birthDay;
	
	@JsonapiFieldIgnore
	private String ignored;
	
	private long uniqueNumber;
	
	@JsonapiRelation(targetResourceClass=MyRole.class, relationType=JsonapiRelationType.ITERABLE)
	private List<MyRole> roles;
Pager pager = new OffsetlimitPager("offset", "limit");
JsonapiDocumentBuilder jdb = new JsonapiDocumentBuilder(pager);
JsonApiDocument jad = jdb.buildListResource(rss, 100, "http://www.abc.com/resources/?page[offset]=40&page[limit]=10");
Map<String, Object> map = jad.asMap();
{
  "data":[
    {
      "id":"1",
      "type":"hello",
      "attributes":{
        "un":"username",
        "birthDay":"20180305155312+0800",
        "uniqueNumber":1
      },
      "relationships":{
        "roles":{
          "links":{
            "self":"http://www.abc.com/hello/1/relationships/roles",
            "related":"http://www.abc.com/hello/1/roles"
          }
        }
      },
      "links":{
        "self":"http://www.abc.com/hello/1"
      }
    },
    {
      "id":"2",
      "type":"hello",
      "attributes":{
        "un":"username",
        "birthDay":"20180305155312+0800",
        "uniqueNumber":1
      },
      "relationships":{
        "roles":{
          "links":{
            "self":"http://www.abc.com/hello/2/relationships/roles",
            "related":"http://www.abc.com/hello/2/roles"
          }
        }
      },
      "links":{
        "self":"http://www.abc.com/hello/2"
      }
    },
    {
      "id":"3",
      "type":"hello",
      "attributes":{
        "un":"username",
        "birthDay":"20180305155312+0800",
        "uniqueNumber":1
      },
      "relationships":{
        "roles":{
          "links":{
            "self":"http://www.abc.com/hello/3/relationships/roles",
            "related":"http://www.abc.com/hello/3/roles"
          }
        }
      },
      "links":{
        "self":"http://www.abc.com/hello/3"
      }
    }
  ],
  "meta":{
    "totalResourceCount":100
  },
  "links":{
    "first":"http://www.abc.com/resources/?page[limit]=10",
    "last":"http://www.abc.com/resources/?page[offset]=90&page[limit]=10",
    "prev":"http://www.abc.com/resources/?page[offset]=30&page[limit]=10",
    "next":"http://www.abc.com/resources/?page[offset]=50&page[limit]=10"
  }
}

questions

  1. Why use annotation in stead of name in requesting URL to describe resource name? You can only get top level resource name from URL, what about relationships? So use annotations. If the resource name in URL conflicts with annotation the result was shown in above code. use the URL passed in for pagination links and use annotation name for internal links.

Versions

Version
0.1.0