Created
April 20, 2017 22:21
-
-
Save kennycason/ae25701e061dbedb8d67df6a156b3333 to your computer and use it in GitHub Desktop.
JsonApi Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.simplymeasured.cls.bll; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.PropertyNamingStrategy; | |
import com.github.jasminb.jsonapi.JSONAPIDocument; | |
import com.github.jasminb.jsonapi.ResourceConverter; | |
import com.github.jasminb.jsonapi.SerializationFeature; | |
import com.github.jasminb.jsonapi.annotations.Id; | |
import com.github.jasminb.jsonapi.annotations.Relationship; | |
import com.github.jasminb.jsonapi.annotations.Type; | |
import com.github.jasminb.jsonapi.exceptions.DocumentSerializationException; | |
import org.junit.Test; | |
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; | |
import java.io.IOException; | |
/** | |
* Created by kenny on 4/20/17. | |
* | |
* https://github.com/jasminb/jsonapi-converter#resource-serialization | |
*/ | |
public class JsonApiTest { | |
private static final ObjectMapper OBJECT_MAPPER = new Jackson2ObjectMapperBuilder() | |
.propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE) | |
.build(); | |
@Type("turd") | |
public static class NinjaTurdle { | |
@Id | |
private String id; | |
private String name; | |
private long awesomenessLevel; | |
@Relationship("mask") | |
private Mask mask; | |
public String getId() { | |
return id; | |
} | |
public void setId(final String id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(final String name) { | |
this.name = name; | |
} | |
public long getAwesomenessLevel() { | |
return awesomenessLevel; | |
} | |
public void setAwesomenessLevel(final long awesomenessLevel) { | |
this.awesomenessLevel = awesomenessLevel; | |
} | |
public Mask getMask() { | |
return mask; | |
} | |
public void setMask(final Mask mask) { | |
this.mask = mask; | |
} | |
} | |
@Type("mask") | |
public static class Mask { | |
@Id | |
private String id; | |
private String name; | |
public String getId() { | |
return id; | |
} | |
public void setId(final String id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(final String name) { | |
this.name = name; | |
} | |
} | |
@Test | |
public void testShitFuckYeah() throws IOException, IllegalAccessException, DocumentSerializationException { | |
final NinjaTurdle ninjaTurdle = new NinjaTurdle(); | |
ninjaTurdle.id = "1"; | |
ninjaTurdle.name = "Mr. Turdle"; | |
ninjaTurdle.awesomenessLevel = Long.MAX_VALUE; | |
final Mask mask = new Mask(); | |
mask.id = "1234"; | |
mask.name = "Worm Mask"; | |
ninjaTurdle.setMask(mask); | |
final ResourceConverter resourceConverter = new ResourceConverter(NinjaTurdle.class); | |
resourceConverter.enableSerializationOption(SerializationFeature.INCLUDE_RELATIONSHIP_ATTRIBUTES); | |
resourceConverter.enableSerializationOption(SerializationFeature.INCLUDE_LINKS); | |
final byte[] data = resourceConverter.writeDocument(new JSONAPIDocument<>(ninjaTurdle)); | |
System.out.println(OBJECT_MAPPER.readTree(data)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment