I have added an enum to a Class and the class is annotated with Jackson annotations. I don't want this enum to be considered during serialization or deserialization. Do I need to add any special tags to ignore the enum, like we do #JsonIgnore for methods or variables ?
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({ "name", "license", "description" })
public class Car {
#JsonProperty("name")
private String name;
#JsonProperty("license")
private String license;
#JsonProperty("description")
private String description;
#JsonProperty("name")
public String getName() {
return name;
}
#JsonProperty("name")
public void setName(String name) {
this.name = name;
}
#JsonProperty("license")
public String getLicense() {
return license;
}
#JsonProperty("license")
public void setLicense(String license) {
this.license = license;
}
#JsonProperty("description")
public String getDescription() {
return description;
}
#JsonProperty("description")
public void setDescription(String description) {
this.description = description;
}
#JsonIgnore
public Type getType() {
if(this.description.contains("electric")) {
return Type.ELECTRIC;
}else if(this.description.contains("diesel")) {
return Type.DIESEL;
}else {
return Type.UNKNOWN;
}
}
public enum Type {
ELECTRIC, DIESEL, GASOLINE, HYDROGEN, BIOFUEL, UNKNOWN
}
}
Here is some code to use this class. It works fine.
public class EnumJsonTester {
public static void main(String [] args) throws Exception {
String json = "{\r\n" +
" \"name\": \"Tesla Model S\",\r\n" +
" \"license\": \"1234\",\r\n" +
" \"description\": \"electric powered vehicle.\"\r\n" +
"}";
Car tesla = Utils.jsonToObject(json, Car.class);
System.out.println("My Car: " + tesla.getType());
}
}
Related
I have a base class
public class Box<T> {
private T entity;
public T getEntity() {
return entity;
}
void setEntity(T entity) {
this.entity = entity;
}
}
It has 2 implementations.
// Class Person
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// Class Machine
public class Machine {
private String macAddress;
private String type;
public Machine(String macAddress, String type) {
this.macAddress = macAddress;
this.type = type;
}
}
If I want to serialise either of classA or class B objects, I will do it like this
Type typeTokenPerson = new TypeToken< Box <Person>>() {}.getType();
String userJson = gson.toJson(boxWithPersonObject, typeTokenPerson);
But the problem here is I need to know the type at compile time. I have a use case where I don't know this at compile-time, in other words, I have a json which I want to deserialize into either Person or Animal and I want to do this at runtime based on some condition.
Is there a way to do this usig Gson ?
Example:
Lets say we have a json like this
{
"entity": {
"name": "ABC",
"age": 10
}
}
This is of type Person. I want to deserialise this into an object of type Box<Person>
Gson can do it like this.
package com.example.demo;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Type;
import java.time.Duration;
import java.time.LocalDateTime;
public class GsonDemo {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private <T> Box<T> parseResponse(String responseData) {
Gson gson = new Gson();
Type jsonType = new TypeToken<Box<T>>() {
}.getType();
Box<T> result = gson.fromJson(responseData, jsonType);
return result;
}
#Test
public void test() {
LocalDateTime start = LocalDateTime.now();
try {
String json = "{ \"entity\": { \"name\": \"ABC\", \"age\": 10 }}";
Box<Person> objectBox = parseResponse(json);
System.out.println(objectBox);
String json2 = "{\n \"entity\": { \"macAddress\": \"DEF\", \"type\": \"def\" }}";
Box<Machine> objectBox2 = parseResponse(json2);
System.out.println(objectBox2);
} catch (Exception e) {
logger.error("Error", e);
}
LocalDateTime end = LocalDateTime.now();
logger.info("Cost time {}", Duration.between(start, end).toMillis() + "ms");
}
public class Box<T> {
private T entity;
public T getEntity() {
return entity;
}
void setEntity(T entity) {
this.entity = entity;
}
#Override
public String toString() {
return "Box{" + "entity=" + entity + '}';
}
}
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
#Override
public String toString() {
return "Person{" + "name='" + name + '\'' + ", age=" + age + '}';
}
}
public class Machine {
private String macAddress;
private String type;
public Machine(String macAddress, String type) {
this.macAddress = macAddress;
this.type = type;
}
public String getMacAddress() {
return macAddress;
}
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
#Override
public String toString() {
return "Machine{" + "macAddress='" + macAddress + '\'' + ", type='" + type + '\'' + '}';
}
}
}
This is my xml file and it is returning null values for type and currency when unmarshalling and rest all values are getting printed. I have used unmarshalling here and all Parent and Child POOJ are specified and finally my Main method calls unmarshall function
1) Vehicle.xml
<?xml version="1.0" encoding="UTF-8"?>
<Vehicle>
<car>
<manufacturer>Maruti</manufacturer>
<cost currency="INR">675000</cost>
<name type="sedan">Ciaz</name>
<fuelType>Petrol</fuelType>
<driverType>Manual</driverType>
</car>
<car>
<manufacturer>Maruti</manufacturer>
<cost currency="INR">575000</cost>
<name type="sedan">Dezire</name>
<fuelType>Petrol</fuelType>
<driverType>Manual</driverType>
</car>
</Vehicle>
Respective file are as
2) Vehicle.java
package jaxb;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "Vehicle")
public class Vehicle {
#XmlElement
private List<Car> car;
public List<Car> getCar() {
return car;
}
#Override
public String toString() {
return "Vehicle[ Car=" + car + "]";
}
}
3) Child for POJO Car.java
package jaxb;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name="Car")
public class Car {
private String manufacturer;
private String name;
private String driverType;
private String fuelType;
private String currency;
#XmlAttribute
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
#XmlAttribute
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
private String type;
private int cost;
#XmlElement
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
#XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#XmlElement
public String getDriverType() {
return driverType;
}
public void setDriverType(String driverType) {
this.driverType = driverType;
}
#XmlElement
public String getFuelType() {
return fuelType;
}
public void setFuelType(String fuelType) {
this.fuelType = fuelType;
}
#XmlElement
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
#Override
public String toString() {
return "Car [name=" + name + ", fuelType=" + fuelType + ", cost=" + cost+",driverType="+driverType +",currency="+currency+ " , type="+type +"]";
}
}
4) Fie for unmarshalling
package jaxb;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class VehicleJxb {
public void unmarhalling() {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Vehicle.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Vehicle vehicle = (Vehicle) jaxbUnmarshaller.unmarshal(new File("src\\main\\java\\Data\\Vehicle.xml"));
System.out.println(vehicle);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
5) Final Output
Vehicle[ Car=[Car [name=Ciaz, fuelType=Petrol, cost=675000,driverType=Manual,currency=null , type=null], Car [name=Dezire, fuelType=Petrol, cost=575000,driverType=Manual,currency=null , type=null]]]
With JAXB you can map attributes only on the same level. To map attributes on embedded elements you should use separate classes for these elements.
Here is how you can map attributes (publci attributes used for simplicity):
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
#XmlRootElement(name = "Car")
public class Car {
public static class Cost {
#XmlValue
public String value;
#XmlAttribute
public String currency;
#Override
public String toString() {
return "Cost[value=" + value + ", currency=" + currency + "]";
}
}
public static class Name {
#XmlValue
public String value;
#XmlAttribute
public String type;
#Override
public String toString() {
return "Name[value=" + value + ", type=" + type + "]";
}
}
private String manufacturer;
private Name name;
private String driverType;
private String fuelType;
#XmlAttribute
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
private String type;
private Cost cost;
#XmlElement
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
#XmlElement
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
#XmlElement
public String getDriverType() {
return driverType;
}
public void setDriverType(String driverType) {
this.driverType = driverType;
}
#XmlElement
public String getFuelType() {
return fuelType;
}
public void setFuelType(String fuelType) {
this.fuelType = fuelType;
}
#XmlElement
public Cost getCost() {
return cost;
}
public void setCost(Cost cost) {
this.cost = cost;
}
#Override
public String toString() {
return "Car [name=" + name + ", fuelType=" + fuelType + ", cost=" + cost + ",driverType=" + driverType + "]";
}
}
One common approach would be to add an extra class to handle the attribute + value. If you don't want the extra indirection when using Car. You can add a shortcut getter to it.
public class Car {
// .....
private Money cost;
public Money getCost() {
return cost;
}
public void setCost(Money cost) {
this.cost = cost;
}
/* Optional shortcut getter */
public String getCurrency(){
if(getCost()==null){
return null;
}
return getCost().getCurrency();
}
}
public static class Money {
private String currency;
private int amount;
#XmlAttribute
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
#XmlValue
public int getAmount() {
return amount;
}
public void setAmount(int cost) {
this.amount = cost;
}
}
try this:
#XmlAttribute(name = "cost currency")
public String getCurrency() {
return currency;
}
#XmlAttribute(name = "name type")
public String getType() {
return type;
}
I have the following endpoint in retrofit:
#GET("user/detail")
Observable<JacksonResponse<User>> getUserDetail();
This endpoint returns the following result:
{
"code":1012,
"status":"sucess",
"message":"Datos Del Usuario",
"time":"28-10-2015 10:42:04",
"data":{
"id_hash":977417640,
"user_name":"Daniel",
"user_surname":"Hdz Iglesias",
"birthdate":"1990-02-07",
"height":190,
"weight":80,
"sex":2,
"photo_path":" https:\/\/graph.facebook.com
\/422\/picture?width=100&height=100"
}
}
Here is the definition of the class:
public class JacksonResponse<T> {
private Integer code;
private String status;
private String message;
private String time;
#JsonInclude(JsonInclude.Include.NON_NULL)
private T data;
public JacksonResponse(){}
#JsonCreator
public JacksonResponse(
#JsonProperty("code") Integer code,
#JsonProperty("status") String status,
#JsonProperty("message") String message,
#JsonProperty("time") String time,
#JsonProperty("data") T data) {
this.code = code;
this.status = status;
this.message = message;
this.time = time;
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
I want the content "data" is mapped to the user class, whose extract show here:
#JsonIgnoreProperties(ignoreUnknown = true)
#ModelContainer
#Table(database = AppDatabase.class)
public class User extends BaseModel {
#PrimaryKey(autoincrement = true)
private Long id;
#Column
private Long idFacebook;
#Column
#JsonProperty("user_name")
private String name;
#Column
#JsonProperty("user_surname")
private String surname;
#Column
private Date birthday;
#Column
#JsonProperty("height")
private Double height;
#Column
#JsonProperty("weight")
private Double weight;
#Column
private String tokenFacebook;
#Column
#JsonProperty("sex")
private Integer sex;
#Column
private String email;
#Column
private String token;
#Column
private Date lastActivity;
#Column
#JsonProperty("id_hash")
private Long idHash;
#Column
#JsonProperty("photo_path")
private String photoPath;
To birthdate, I have defined a custom deserializer, whose code show here:
public class BirthdayDeserializer extends JsonDeserializer<Date> {
#Override
public Date deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext) throws IOException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = jsonparser.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
I am using this as follows (at User class):
#JsonProperty("birthday")
#JsonDeserialize(using = BirthdayDeserializer.class)
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
but this is never called.
Any idea what 's going on?
You Pojo and JSON does not map. You need to have a Data.java which should have properties as given in the JSON. you classes should be as below based on the json given above.
User.java
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
#JsonIgnoreProperties(ignoreUnknown = true)
public class User {
#JsonProperty("code")
public Integer code;
#JsonProperty("status")
public String status;
#JsonProperty("message")
public String message;
#JsonProperty("time")
public String time;
#JsonProperty("data")
public Data data;
}
Data.java
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
#JsonIgnoreProperties(ignoreUnknown = true)
public class Data {
#JsonProperty("id_hash")
public Integer idHash;
#JsonProperty("user_name")
public String userName;
#JsonProperty("user_surname")
public String userSurname;
#JsonProperty("birthdate")
#JsonDeserialize(using = BirthdayDeserializer.class)
public Date birthdate;
#JsonProperty("height")
public Integer height;
#JsonProperty("weight")
public Integer weight;
#JsonProperty("sex")
public Integer sex;
#JsonProperty("photo_path")
public String photoPath;
}
Main.java to test it.
public class Main {
public static void main(String[] args) throws IOException {
String json = "{\n" +
" \"code\": 1012,\n" +
" \"status\": \"sucess\",\n" +
" \"message\": \"Datos Del Usuario\",\n" +
" \"time\": \"28-10-2015 10:42:04\",\n" +
" \"data\": {\n" +
" \"id_hash\": 977417640,\n" +
" \"user_name\": \"Daniel\",\n" +
" \"user_surname\": \"Hdz Iglesias\",\n" +
" \"birthdate\": \"1990-02-07\",\n" +
" \"height\": 190,\n" +
" \"weight\": 80,\n" +
" \"sex\": 2\n" +
" }\n" +
"}";
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Date.class, new BirthdayDeserializer());
mapper.registerModule(module);
User readValue = mapper.readValue(json, User.class);
}
}
JSON String
{
"order":{
"address":{
"city":"seattle"
},
"orderItem":[
{
"itemId":"lkasj",
"count":2
},
{
"itemId":"ldka",
"count":3
}
]
}
}
Order Class
public class Order {
private OrderItem[] orderItems;
private CustomerAddress address;
Order(OrderItem[] orderItems, CustomerAddress address ) {
this.orderItems = orderItems;
this.address = address;
}
public OrderItem[] getOrderItems() {
return orderItems;
}
public void setOrderItems(OrderItem[] orderItems) {
this.orderItems = orderItems;
}
public CustomerAddress getAddress() {
return address;
}
public void setAddress(CustomerAddress address) {
this.address = address;
}
}
My OrderItem class
package com.cbd.backend.model;
import org.springframework.data.annotation.Id;
public class OrderItem {
#Id
private String id;
private String itemId;
private String count;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
unit Test that blows up
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
}
Unit test to demonstrate issue
package com.cbd.backend.model;
import com.google.gson.Gson;
import org.junit.Test;
import static org.junit.Assert.*;
public class OrderTest {
Gson gson = new Gson();
#Test
public void gsonToOrder() {
Order order = gson.fromJson( a, Order.class );
assertNotNull(order);
assertNotNull(order.getOrderItems()[0]);
}
private final String a = "{ \"order\": { \"address\": { \"city\": \"seattle\" },\"orderItem\":[{ \"itemId\":\"lkasj\", \"count\":2 }, { \"itemId\":\"ldka\", \"count\":3 } ] } }";
}
Should I be using something other than gson or am i constructing this incorrectly
There are two problems in your code:
The root element of your JSON is "order", but the class does not have a property with this name. Try changing you model or just removing the element from the JSON.
There is a mismatch in the name of the "orderItem" property. It is plural in the class, but singular in the JSON.
To sum it up, the following JSON will work without any changes to the code.
{
"address":{
"city":"seattle"
},
"orderItems":[
{
"itemId":"lkasj",
"count":2
},
{
"itemId":"ldka",
"count":3
}
]
}
Also, "count" as it appears in the JSON seems to be numeric, so you might want to change the type of OrderItem.count to int or java.lang.Integer.
data_user = "{"id":1,"lastName":"lastName","name":"name","school":{"id":1}}"
public class School {
private int id;
private String name;
}
public class User {
private int id;
private String lastName;
private String name;
private School school;
}
How to deserialize Json data_user to java object User?
I tried with Gson :
Gson gson = new Gson();
User user = gson.fromJson(data_user, User.class)
But I have an error with this code because the Json contains a school which hasn't the school's name.
How Can I serialize the Json to java Object?
School.java
public class School {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "School [id=" + id + ", name=" + name + "]";
}
}
User.java
public class User {
private int id;
private String lastName;
private String name;
private School school;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
#Override
public String toString() {
return "User [id=" + id + ", lastName=" + lastName + ", name=" + name
+ ", school=" + school + "]";
}
}
Main.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.testgson.beans.User;
public class Main {
private static Gson gson;
static {
gson = new GsonBuilder().create();
}
public static void main(String[] args) {
String j = "{\"id\":1,\"lastName\":\"lastName\",\"name\":\"ignacio\",\"school\":{\"id\":1}}";
User u = gson.fromJson(j, User.class);
System.out.println(u);
}
}
Result
User [id=1, lastName=lastName, name=ignacio, school=School [id=1, name=null]]
Try with the Jackson Library. With Gson with should have not any problem, I tried with the code of #Saurabh and it work well