Jackson-JsonLD nested Objects - java

I want to program a restful API and annotate my data with schema.org. For this I wanted to use Jackson-Jsonld. Annotating simple objects with jackson-jsonld were no problem, but complex ones with nested objects got me stucked. In my jsonld the simple attributes like id, name got anntotated but the nested location not.
I read about Serialization and that it should help in order to get the second object. However, after implementing my serialization part it seems that the serialization did not changed anything.
Here is my sample output, the type for location should be PostalAddress however the type is missing:
{"#context":
{"uri":"http://schema.org/url","name":"http://schema.org/name","location":"http://schema.org/location"},
"#type":"http://schema.org/Organization",
"uri":"http://localhost:8080/kangarooEvents/venue/12",
"name":"Joondalup Library - Ground Floor Meeting Room",
"location":{
"address":"102 Boas Avenue",
"city":"Joondalup",
"zip":"6027",
"country":"Australia",
"state":"WA"},
"#id":12}
I want to annotate an organization which has a single location:
#JsonldType("http://schema.org/Organization")
public class Venue {
#JsonldId
private Integer id;
#JsonldProperty("http://schema.org/url")
private String uri;
#JsonldProperty("http://schema.org/name")
private String name;
#JsonSerialize(using = CostumLocationSerializer.class)
#JsonldProperty("http://schema.org/location")
private Location location;
Location:
#JsonldType("http://schema.org/PostalAddress")
public class Location {
#JsonldProperty("http://schema.org/streetAddress")
private String address;
#JsonldProperty("http://schema.org/addressLocality")
private String city;
#JsonldProperty("http://schema.org/addressRegion")
private String state;
#JsonldProperty("http://schema.org/addressRegion")
private String country;
#JsonldProperty("http://schema.org/postalCode")
private String zipcode;
Serialization:
public class CostumLocationSerializer extends StdSerializer<Location> {
private ObjectMapper mapper = new ObjectMapper();
public CostumLocationSerializer(){
this( null);
}
protected CostumLocationSerializer(Class<Location> t) {
super(t);
}
#Override
public void serialize(Location location, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("address", location.getAddress());
jsonGenerator.writeStringField("city", location.getCity());
jsonGenerator.writeStringField("zip", location.getZipcode());
jsonGenerator.writeStringField("country", location.getCountry());
jsonGenerator.writeStringField("state", location.getState());
jsonGenerator.writeEndObject();
String serialized = mapper.writeValueAsString(location);
}
}
I think that my problem might be in the serialization but I can not figure it out. Maybe someone annotated nested obj. and can tell me what my problem is.

Just skip the jackson-jsonld part and do it manually
Create JSON - just introduce a field for type and id into your java classes.
Create a JSON-LD context - map your id and type fields in an additional #context object
Combine context and data - e.g. just add your #context object after your 'normal' json serialization using standard jackson API.
Example
#Test
public void createJsonFromPojo() throws Exception {
ObjectMapper mapper=new ObjectMapper();
// Create object structure
Venue venue = new Venue();
venue.location = new Location();
venue.id="12";
venue.uri="http://localhost:8080/kangarooEvents/venue/12";
venue.name="Joondalup Library - Ground Floor Meeting Room";
venue.location.address="102 Boas Avenue";
venue.location.city="Joondalup";
venue.location.state="WA";
venue.location.country="Australia";
venue.location.zipcode="6027";
//1. Create JSON
ObjectNode myData = mapper.valueToTree(venue);
//2. Create a JSON-LD context
ArrayNode context = mapper.createArrayNode();
context.add("http://schema.org/");
ObjectNode myContext=mapper.createObjectNode();
myContext.put("id", "#id");
myContext.put("type", "#type");
context.add(myContext);
//3. Combine context and data
myData.set("#context",context);
//4. Print
StringWriter w = new StringWriter();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true).writeValue(w, myData);
String result= w.toString();
System.out.println(result);
}
public class Venue {
public final String type = "http://schema.org/Organization";
public String id;
public String uri;
public String name;
public Location location;
}
public class Location {
public final String type = "http://schema.org/PostalAddress";
public String address;
public String city;
public String state;
public String country;
public String zipcode;
}
Gives you
{
"#context": [
"http://schema.org/",
{
"id": "#id",
"type":"#type"
}
],
"uri":"http://localhost:8080/kangarooEvents/venue/12",
"name":"Joondalup Library - Ground Floor Meeting Room",
"location":{
"address":"102 Boas Avenue",
"city":"Joondalup",
"zip":"6027",
"country":"Australia",
"state":"WA",
"type":"http://schema.org/PostalAddress"
},
"id":"12",
"type":"http://schema.org/Organization"
}
View Example in Playground

Related

Deserialize JSON object from MongoDB to Java object on GET request

I have some nested classes in Java, simplified here. Getters and setters exist.
Example
public class Planet {
#JsonProperty("name")
private String name;
#JsonProperty("moons")
private List<Moon> moons;
}
public class Moon {
#JsonProperty("moonname")
private String name;
#JsonProperty("craters")
private int craters;
}
I want to be able to deserialize the records on mongo (following this same structure) to java objects on the rest controller, specifically the HTTP GET request.
#RestController
#RequestMapping("/planets")
public class PlanetController {
#Autowired
private PlanetService planetService;
#RequestMapping("/")
public List<Planet> getAllPlanets() {
//Need to deserialize here
return planetService.getAll();
}
#RequestMapping("/{name}")
public Planet getItemsWithName(#PathVariable("name") String name) {
//deserialize here
return planetService.getEntryWithName(name.toLowerCase());
}
PlanetService.getAll() is expecting return type of List. getEntryWithName() is expecting return type of Planet.
How can I loop the results in the getAll() so I can deserialize them before they are returned?
Using Jackson's object mapper, I can do the serialization of a Java object to a JSON object.
ObjectMapper mapper = new ObjectMapper();
try {
mapper.writeValue(new File("target/mars.json"), mars);
} catch (IOException e) {
e.printStackTrace();
}
I can probably use readValue for the opposite process but I don't know how to loop the results.
I will appreciate the help. Let me know if something is not clear.
public List<Planet> getAllPlanets() {
List<Planet> planets = planetService.getAll();
String jsonString = new ObjectMapper().writeValueAsString(planets);
return planets;
}

Json Deserialization in Java /w Jackson of mixed types, contained in one array

Consider the following json, getting from an public API:
anyObject : {
attributes: [
{
"name":"anyName",
"value":"anyValue"
},
{
"name":"anyName",
"value":
{
"key":"anyKey",
"label":"anyLabel"
}
}
]
}
As you can see, sometimes the value is a simple string and sometimes its an object. Is it somehow possible to deserialize those kind of json-results, to something like:
class AnyObject {
List<Attribute> attributes;
}
class Attribute {
private String key;
private String label;
}
How would I design my model to cover both cases. Is that possible ?
Despite being hard to manage as others have pointed out, you can do what you want. Add a custom deserializer to handle this situation. I rewrote your beans because I felt your Attribute class was a bit misleading. The AttributeEntry class in the object that is an entry in that "attributes" list. The ValueObject is the class that represents that "key"/"label" object. Those beans are below, but here's the custom deserializer. The idea is to check the type in the JSON, and instantiate the appropriate AttributeEntry based on its "value" type.
public class AttributeDeserializer extends JsonDeserializer<AttributeEntry> {
#Override
public AttributeEntry deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode root = p.readValueAsTree();
String name = root.get("name").asText();
if (root.get("value").isObject()) {
// use your object mapper here, this is just an example
ValueObject attribute = new ObjectMapper().readValue(root.get("value").asText(), ValueObject.class);
return new AttributeEntry(name, attribute);
} else if (root.get("value").isTextual()) {
String stringValue = root.get("value").asText();
return new AttributeEntry(name, stringValue);
} else {
return null; // or whatever
}
}
}
Because of this ambiguous type inconvenience, you will have to do some type checking throughout your code base.
You can then add this custom deserializer to your object mapper like so:
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addDeserializer(AttributeEntry.class, new AttributeDeserializer());
objectMapper.registerModule(simpleModule);
Here's the AttributeEntry:
public class AttributeEntry {
private String name;
private Object value;
public AttributeEntry(String name, String value) {
this.name = name;
this.value = value;
}
public AttributeEntry(String name, ValueObject attributes) {
this.name = name;
this.value = attributes;
}
/* getters/setters */
}
Here's the ValueObject:
public class ValueObject {
private String key;
private String label;
/* getters/setters */
}

Java JSON deserialize String with property names as field names

I am receiving from API json like that:
{
"channel":"masta",
"startTime":1427673600000,
"endTime":1427760000000,
"totalUniques":1,
"totalViewtime":1927,
"totalViews":13,
"totalCountries":1,
"countries":{
"US":{
"uniques":1,
"views":13,
"viewtime":1927
}
}
}
Now I want to deserialize it to class, so this class(Stats) will have fields like channel, startTime and so on.
But how to handle countries property?
I thought about making class Countries but not sure about that cause it's have "US" as property name. Not "country": "US". And what's more it has own parameters. How to deserialize it?
Mostly I am using ObjectMapper object.readValue(jsonString) to do that but don't know how to handle 'countries'. In example is just one country 'US' but can be more.
Declare Country class:
public class Country {
private int uniques;
private int views;
private int viewtime;
public int getUniques() {
return uniques;
}
public void setUniques(int uniques) {
this.uniques = uniques;
}
public int getViews() {
return views;
}
public void setViews(int views) {
this.views = views;
}
public int getViewtime() {
return viewtime;
}
public void setViewtime(int viewtime) {
this.viewtime = viewtime;
}
}
In your Stats class you should declare countries as map of Country objects:
public class Stats {
private String channel;
private Long startTime;
private Long endTime;
private int totalUniques;
private int totalViewtime;
private int totalViews;
private int totalCountries;
...
private Map<String, Country> countries;
public Map<String, Country> getCountries() {
return countries;
}
public void setCountries(Map<String, Country> countries) {
this.countries = countries;
}
}
Now you can deserialize you object:
ObjectMapper mapper = new ObjectMapper();
Stats stats = mapper.readValue(jsonString, Stats.class);
After deserialization your Stack object will get map with one Country object with key "US".
Basically, you need to define a POJO like ViewInfo that has the attributes like channel, startTime, endTime and so on. countries is also another attribute, but it isn't a primitive, it's like a map with country code as the key and Country as another POJO (which has attributes like uniques, views and so on). This way, you should be able to deserialize the json into this pojo.

How to rename root key in JSON serialization with Jackson

I am using Jackson for JSON serialization of a list of objects.
Here is what I get:
{"ArrayList":[{"id":1,"name":"test name"}]}
But I want this :
{"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root name.
Below is my approach to this:
Interface:
public interface MyInterface {
public long getId();
public String getName();
}
Implementation class:
#JsonRootName(value = "rootname")
public class MyImpl implements MyInterface {
private final long id;
private String name;
public MyImpl(final long id,final name) {
this.id = id;
this.name = name;
}
// getters
}
JSon serialization:
public class MySerializer {
public static String serializeList(final List<MyInterface> lists) {
//check for null value.Throw Exception
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
return mapper.writeValueAsString(lists);
}
}
Test:
final List<MyInterface> list = new ArrayList<MyImpl>();
MyImpl item = new MyImpl(1L,"test name");
list.add(item);
final String json = MySerializer.serializeList(list);
System.out.println(json);
Here is what I get:
{"ArrayList":[{"id":1,"name":"test name"}]}
But I want this :
{"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root name.
I have tried all suggested solutions I could find but failed to achieve my goal. I have looked at:
Jackson : custom collection serialization to JSON
How do I rename the root key of a JSON with Java Jackson?
Jackson : custom collection serialization to JSON
Or am I missing something? I am using jackson 1.9.12 for this. Any help in this regard is welcome.
Well, by default Jackson uses one of two annotations when trying to determine the root name to be displayed for wrapped values - #XmlRootElement or #JsonRootName. It expects this annotation to be on the type being serialized, else it will use the simple name of the type as the root name.
In your case, you are serializing a list, which is why the root name is 'ArrayList' (simple name of the type being serialized). Each element in the list may be of a type annotated with #JsonRootName, but the list itself is not.
When the root value you are trying to wrap is a collection then you need some way of defining the wrap name:
Holder/Wrapper Class
You can create a wrapper class to hold the list, with an annotation to define the desired property name (you only need to use this method when you do not have direct control of the ObjectMapper/JSON transformation process):
class MyInterfaceList {
#JsonProperty("rootname")
private List<MyInterface> list;
public List<MyInterface> getList() {
return list;
}
public void setList(List<MyInterface> list) {
this.list = list;
}
}
final List<MyInterface> lists = new ArrayList<MyInterface>(4);
lists.add(new MyImpl(1L, "test name"));
MyInterfaceList listHolder = new MyInterfaceList();
listHolder.setList(lists);
final String json = mapper.writeValueAsString(listHolder);
Object Writer
This is the preferable option. Use a configured ObjectWriter instance to generate the JSON. In particular, we are interested in the withRootName method:
final List<MyInterface> lists = new ArrayList<MyInterface>(4);
lists.add(new MyImpl(1L, "test name"));
final ObjectWriter writer = mapper.writer().withRootName("rootName");
final String json = writer.writeValueAsString(lists);
I know, I am late , but I have better approach which don't require Holder/Wrapper Class. It picks root key from annotation.
package com.test;
import com.fasterxml.jackson.annotation.JsonRootName;
#JsonRootName("Products")
public class ProductDTO {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Here is test class:-
package com.test;
import java.io.IOException;
import java.util.ArrayList;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ProductDTOTestCase {
#Test
public void testPersistAndFindById() throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
ProductDTO productDTO = new ProductDTO();
productDTO.setDescription("Product 4 - Test");
ArrayList<ProductDTO> arrayList = new ArrayList<ProductDTO>();
arrayList.add(productDTO);
String rootName = ProductDTO.class.getAnnotation(JsonRootName.class).value();
System.out.println(mapper.writer().withRootName(rootName).writeValueAsString(arrayList));
}
}
It will give following output
{"Products":[{"name":null,"description":"Product 4 - Test"}]}
#JsonTypeName("usuarios")
#JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT,use= JsonTypeInfo.Id.NAME)
public class UsuarioDT extends ArrayList<Usuario> {
#JsonProperty("rowsAffected")
private Integer afectados;
public Integer getAfectados() {
return afectados;
}
public void setAfectados(Integer afectados) {
this.afectados = afectados;
}
}
You need to use this annotation at the top of the class
#JsonTypeName("rootname")

Jackson deserialization - aggregate several fields

suppose I've got a collection of people defined like this in JSON.
{
"NOM": "Doe",
"PRENOM": "John",
"EMAIL": "john.doe#email.me",
"VILLE": "Somewhere",
"LIKE1": "Lolcats",
"LIKE2": "Loldogs",
"LIKE3": "Lolwut",
"HATE1": "Bad stuff",
"HATE2": "Bad bad stuff"
}
Is it possible to write a JsonDeserializer that will aggregate and transform LIKE* and HATE* fields into a collection of Liking, set as a property of Person? (Note that there are only LIKE1, LIKE2, LIKE3, HATE1, HATE2.)
The final result properties would be something like:
public class Person {
private final String lastName;
private final String firstName;
private final String email;
private final String town;
private final Collection<Liking> likings;
// c-tor, getters
}
I've already the logic that can deserialize a given LIKE*/HATE* property into a Liking object but I fail to understand to aggregate and add them to a Person liking attribute.
Thx in advance!
It would have been nice if you had some code that showed you began the process of solving this problem yourself. But, here is a sample custom deserializer that does pretty much what you're looking for:
class PersonDeserializer extends JsonDeserializer<Person> {
#Override
public Person deserialize(final JsonParser parser,
final DeserializationContext content) throws IOException,
JsonProcessingException {
final ObjectCodec codec = parser.getCodec();
final JsonNode node = codec.readTree(parser);
final Person person = new Person();
final Iterator<String> fieldNameIter = node.getFieldNames();
while (fieldNameIter.hasNext()) {
final String fieldName = fieldNameIter.next();
if (fieldName.equalsIgnoreCase("EMAIL")) {
person.setEmail(node.get(fieldName).getTextValue());
} else if (fieldName.equalsIgnoreCase("NOM")) {
person.setFirstName(node.get(fieldName).getTextValue());
} else if (fieldName.equalsIgnoreCase("PRENOM")) {
person.setLastName(node.get(fieldName).getTextValue());
} else if (fieldName.equalsIgnoreCase("VILLE")) {
person.setTown(node.get(fieldName).getTextValue());
} else if (fieldName.startsWith("LIKE")) {
person.addLike(Liking.LikingType.LIKE, node.get(fieldName)
.getTextValue());
} else if (fieldName.startsWith("HATE")) {
person.addLike(Liking.LikingType.HATE, node.get(fieldName)
.getTextValue());
}
}
return person;
}
}
It presumes a Liking object similar to this:
public class Liking {
public static enum LikingType {
LIKE, HATE;
}
private LikingType type;
private String value;
// Constructors, getters/setters
}
And some changes to your Person object which I think you can figure out. If you intend to serialize the object to JSON in the same custom format then you will have to write a corresponding JsonSerializer.
Another option, not quite as robust, would be too simply use a map to store the likes and dislikes exactly as is. This solution would omit any explicit mappings for likes/dislikes and utilize the #JsonAny annotation to capture them. In this scheme the Person object would look like this:
public class Person {
private String lastName;
private String firstName;
private String email;
private String town;
#JsonAny
private Map<String, Object> otherProperties;
// Constructors, getters/setters
}
Deserializing your JSON into this modified version of Person will place all unrecognized properties into the hash map, as key-value pairs.
I'm pretty sure you can't do it the way you intend, how about doing it like this:
{
"NOM": "Doe",
"PRENOM": "John",
"EMAIL": "john.doe#email.me",
"VILLE": "Somewhere",
"likings": ["Lolcats", "Loldogs", "LIKE3": "Lolwut", "Bad stuff", "Bad bad stuff" ]
}

Categories

Resources