I'm trying to figure out response of a GET method API, it is returning extra attribute with name "attributesMap"
Reponse entity return code is return ResponseEntity.ok(document);
document model class
private String id;
private String format;
private List<Attribute> attributes;
private String type;
private String accessright;
public Document() {
}
/**
* Copy constructor.
*/
public Document(Document source) {
this.id = source.id;
this.format = source.format;
this.type = source.type;
this.accessright = source.accessright;
this.attributes = source.attributes;
}
public static Document newDocument(String type, String id, String format, String accessright, List<Attribute> attributes) {
Document document = new Document();
document.type = type;
document.id = id;
document.format = format;
document.accessright = accessright;
document.attributes = attributes;
return document;
}
public static Document newCompleteDocument(String id, String type, String format, String accessright, List<Attribute> attributes) {
Document document = new Document();
document.id = id;
document.type = type;
document.format = format;
document.accessright = accessright;
document.attributes = attributes;
return document;
}
//
// Getters and setters ahead
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Attribute> getAttributes() {
return attributes;
}
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}
public String getAccessright() {
return accessright;
}
public void setAccessright(String accessright) {
this.accessright = accessright;
}
public Map<String, List<String>> getAttributesMap() {
Map<String, List<String>> attributesMap = getAttributes().stream().collect(Collectors.toMap(Attribute::getName, Attribute::getValues));
return attributesMap;
}
#Override
public String toString() {
final StringBuffer sb = new StringBuffer("Document{");
sb.append("id='").append(id).append('\'');
sb.append(", type='").append(type).append('\'');
sb.append(", format='").append(format).append('\'');
sb.append(", accessright='").append(accessright).append('\'');
sb.append(", attributes=[").append(attributes);
if (attributes != null) {
for (Attribute attribute:attributes) {
sb.append(attribute.getName()).append(":").append(attribute.getValues().toArray()).append(",");
}
}
sb.append("]}");
return sb.toString();
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Document document = (Document) o;
if (id != null ? !id.equals(document.id) : document.id != null) {
return false;
}
if (type != null ? !type.equals(document.type) : document.type != null) {
return false;
}
if (format != null ? !format.equals(document.format) : document.format != null) {
return false;
}
if (accessright != null ? !accessright.equals(document.accessright) : document.accessright != null) {
return false;
}
return attributes != null ? attributes.equals(document.attributes) : document.attributes == null;
}
#Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (type != null ? type.hashCode() : 0);
result = 31 * result + (format != null ? format.hashCode() : 0);
result = 31 * result + (accessright != null ? accessright.hashCode() : 0);
result = 31 * result + (attributes != null ? attributes.hashCode() : 0);
return result;
}
}
but the API reponse JSON as an extra attribute "attributesMap". Sample JSON as below:
{
"id": "xxx",
"format": "xx",
"attributes": [
{
"name": "attr1",
"values": [
"val1"
]
}
],
"type": "test type",
"accessright": "DELETE",
"attributesMap": {
"name": "attr1",
"values": [
"val1"
]
}
}
Can anyone help me figure out how to check where this attribute coming from , when I debugged till the return
return ResponseEntity.ok(document);
there is no attributesMap in the document model object.
The problem in your case is that you have the getter getAttributesMap. What will jackson serialize by default?
All public fields
All getter methods
What can you do?
Rename the method
Exclude the getter with #JsonIgnore
#JsonIgnore
public Map<String, List<String>> getAttributesMap() {
Map<String, List<String>> attributesMap = getAttributes().stream().collect(Collectors.toMap(Attribute::getName, Attribute::getValues));
return attributesMap;
}
You could also provide your own ObjectMapper bean with the following settings:
var mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
or
var mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
This would only serialize fields, not getters.
Related
I've created CRUD methods but i have some problems with that.
This is my add method code:
public Product addProduct(Product content) {
Product item = new Product();
item.setName(content.getName());
item.setCalories(content.getCalories());
item.setFat(content.getFat());
item.setCarbo(content.getCarbo());
item.setProtein(content.getProtein());
item.setProductKinds(content.getProductKinds());
item.setAuthor(content.getAuthor());
item.setMedia(content.getMedia());
item.setApproved(content.getApproved());
databaseController.saveTest(item);
logger.log("Item created");
return item;
}
This is my editProduct method:
public Product editProduct(Product product) {
Product databaseProduct = databaseController.get(Product.class, product.getId());
databaseProduct.setAllProducts(product);
databaseController.save(databaseProduct);
return databaseProduct;
}
In model class I think that I have made everything right :
package pl.javamill.model.kitchen;
import com.amazonaws.services.dynamodbv2.datamodeling.*;
import pl.javamill.model.Request;
import pl.javamill.model.common.Author;
import pl.javamill.model.common.AuthorConverter;
import pl.javamill.model.common.Media;
import pl.javamill.model.common.MediaConverter;
import java.util.List;
#DynamoDBTable(tableName = "product")
public class Product extends Request {
/**
* Id of kitchen content
*/
private String id;
/**
* Name of product
*/
private String name;
/**
* Calories in 100g
*/
private Integer calories;
/**
* Fat in 100g
*/
private Double fat;
/**
* Total carbo in 100g
*/
private Double carbo;
/**
* Total Protein in 100g
*/
private Double protein;
/**
* Labels of product for example gluten fee product
*/
private List<ProductKind> productKinds;
/**
* Author of content.
*/
private Author author;
/**
* Address of content image.
*/
private Media media;
private Boolean approved;
#DynamoDBHashKey(attributeName = "id")
#DynamoDBAutoGeneratedKey
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#DynamoDBAttribute(attributeName = "Name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#DynamoDBAttribute(attributeName = "Calories")
public Integer getCalories() {
return calories;
}
public void setCalories(Integer calories) {
this.calories = calories;
}
#DynamoDBAttribute(attributeName = "Fat")
public Double getFat() {
return fat;
}
public void setFat(Double fat) {
this.fat = fat;
}
#DynamoDBAttribute(attributeName = "Carbo")
public Double getCarbo() {
return carbo;
}
public void setCarbo(Double carbo) {
this.carbo = carbo;
}
#DynamoDBAttribute(attributeName = "Protein")
public Double getProtein() {
return protein;
}
public void setProtein(Double protein) {
this.protein = protein;
}
#DynamoDBTypeConvertedEnum
#DynamoDBTypeConverted(converter = ProductKindConverter.class)
#DynamoDBAttribute(attributeName = "ProductKinds")
public List<ProductKind> getProductKinds() {
return productKinds;
}
public void setProductKinds(List<ProductKind> productKinds) {
this.productKinds = productKinds;
}
#DynamoDBTypeConverted(converter = AuthorConverter.class)
#DynamoDBAttribute(attributeName = "Author")
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
#DynamoDBTypeConverted(converter = MediaConverter.class)
#DynamoDBAttribute(attributeName = "Media")
public Media getMedia() {
return media;
}
public void setMedia(Media media) {
this.media = media;
}
#DynamoDBAttribute(attributeName = "Approved")
public Boolean getApproved() {
return approved;
}
public void setApproved(Boolean approved) {
this.approved = approved;
}
public void setAllProducts(Product product) {
if (!getName().equals(product.getName())) {
setName(product.getName());
}
if (!getCalories().equals(product.getCalories())) {
setCalories(product.getCalories());
}
if (!getFat().equals(product.getFat())) {
setFat(product.getFat());
}
if (!getCarbo().equals(product.getCarbo())) {
setCarbo(product.getCarbo());
}
if (!getProtein().equals(product.getProtein())) {
setProtein(product.getProtein());
}
if (!getProductKinds().equals(product.getProductKinds())) {
setProductKinds(product.getProductKinds());
}
if (!getAuthor().equals(product.getAuthor())) {
setAuthor(product.getAuthor());
}
if (!getMedia().equals(product.getMedia())) {
setMedia(product.getMedia());
}
if (!getApproved().equals(product.getApproved())) {
setApproved(product.getApproved());
}
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product product = (Product) o;
if (name != null ? !name.equals(product.name) : product.name != null) return false;
if (calories != null ? !calories.equals(product.calories) : product.calories != null) return false;
if (fat != null ? !fat.equals(product.fat) : product.fat != null) return false;
if (carbo != null ? !carbo.equals(product.carbo) : product.carbo != null) return false;
if (protein != null ? !protein.equals(product.protein) : product.protein != null) return false;
if (productKinds != null ? !productKinds.equals(product.productKinds) : product.productKinds != null)
return false;
if (author != null ? !author.equals(product.author) : product.author != null) return false;
if (media != null ? !media.equals(product.media) : product.media != null) return false;
return approved != null ? approved.equals(product.approved) : product.approved == null;
}
#Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (calories != null ? calories.hashCode() : 0);
result = 31 * result + (fat != null ? fat.hashCode() : 0);
result = 31 * result + (carbo != null ? carbo.hashCode() : 0);
result = 31 * result + (protein != null ? protein.hashCode() : 0);
result = 31 * result + (productKinds != null ? productKinds.hashCode() : 0);
result = 31 * result + (author != null ? author.hashCode() : 0);
result = 31 * result + (media != null ? media.hashCode() : 0);
result = 31 * result + (approved != null ? approved.hashCode() : 0);
return result;
}
}
ProductKindConventor:
public class ProductKindConverter implements DynamoDBTypeConverter<String, List<ProductKind>> {
#Override
public String convert(List<ProductKind> objects) {
//Jackson object mapper
ObjectMapper objectMapper = new ObjectMapper();
try {
String objectsString = objectMapper.writeValueAsString(objects);
return objectsString;
} catch (JsonProcessingException e) {
//do something
}
return null;
}
#Override
public List<ProductKind> unconvert(String objectsString) {
ObjectMapper objectMapper = new ObjectMapper();
try {
List<ProductKind> objects = objectMapper.readValue(objectsString, new TypeReference<List<ProductKind>>(){});
return objects;
} catch (JsonParseException e) {
//do something
} catch (JsonMappingException e) {
//do something
} catch (IOException e) {
//do something
}
return null;
}
}
Save method in dbcontroller:
public void saveTest(Product product){
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(regions).build();
mapper = new DynamoDBMapper(client);
mapper.save(product);
}
Get product from DB method:
public Product getTest(String id) {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(regions).build();
mapper = new DynamoDBMapper(client);
Product retrivedProduct = mapper.load(Product.class, id);
return retrivedProduct;
}
Unit test passed, everything seems to be ok, but when I was using POSTMAN to test, i got some errors. I send product as json and it seems like that:
{"id":null,"name":"yoloornotyolo","calories":1000,"fat":400.0,"carbo":20.0,"protein":40.0,"productKinds":["MEAT"],"author":{"name":"Plejer Annołn","id":"testID2"},"media":{"name":"heheszki","url":"http://blabla.pl","mediaType":"IMAGE"},"approved":false}
Then I got ""message": "Internal server error"", so I check log file and that what I can see:
not supported; requires #DynamoDBTyped or #DynamoDBTypeConverted:
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException
I dont know what is wrong. Can someone explain me what I suppose to do?
It would be useful to see your saveProduct method. You are using DynamoDBMapper annotations but it looks like you are trying to save an item object, not a Product object. Rather than
Item item = new Item()
Use
Product item = new Product();
item.setName...
One thing I would mention is that you can offload the handling of your generated id to DyanmoDBMapper using the DynamoDBAutoGeneratedKey annotation
In your model class do this:
#DynamoDBHashKey(attributeName = "Id")
#DynamoDBAutoGeneratedKey
public String getId() { return id; }
public void setId(String id) { this.id = id; }
And then you dont need to handle id in your addProduct method
EDIT: Your saveProduct method should be along the lines of
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
mapper.save(item)
I doubt you need to be passing a tablename as that is an annotation on your Product object. You shouldn't be using putItem.
EDIT:
Your saveProduct method should take a Product object, not an item object. Dont use putItem because this doesn't know about your model class. Use DynamoDBMapper save function. You dont need to specify the table name as that's an annotation on your Product class (the table is called 'product'). You should use AmazonDynamoDB for interfacing to DynamoDB. The method below return is void, so you will need to update the addProduct method.
public void saveProduct(Product item) {
logger.log("Starting to save an item");
//You need a new method to return an AmazonDynamoDB interface
AmazonDynamoDB dynamoDB = getAmazonDynamoDB();
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB);
mapper.save(item)
logger.log("Item stored");
return saved;
}
EDIT:
You build an AmazonDynamoDB interface like this
// Set to your chosen region
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.US_WEST_2)
.build();
EDIT:
DynamoDBMapper only supports certain data types.
You have three complex data types there; productKind, author and media. If you want to store these in your database, you will need to write a Convertor Class for each one. The convertor class typically define how you convert your object to a string and back again.
EDIT: Your set, ProductKind, will need a convertor that looks like this. Just change 'MyObject' to ProductKinds.
It would be useful to have a stack trace for the exception. In the intermediate message it contains the data type and field information which can not be converted.
Usually you do not need to make any convertors, just be sure to have #DynamoDBDocument on all custom classes, and #DynamoDBTypeConvertedEnum on all custom enums used.
I also had this error and it is a bit misleading, all we need to do is use #DynamoDBDocument on the class file of the referenced classes.
For your case, just put this annotation on ProductKind, like this:
#DynamoDBDocument
class ProductKind {
...
}
It should persist into dynamoDB as a map in the product table.
you will run into this error if the mapper class is
missing a No-Arg constructor
or has
invalid Setters/Getters
that do not comply with the camel case convention.
(Solved) Error: could not unconvert attribute dynamodb
Hello folks this may be dumb question but as a beginner am struggling with this how to group values based on id in list, Now let me clarify you briefly am having set of objects like this :
ID:1,UserID:330
ID:2,UserID:303
ID:3,UserID:090
ID:1,UserID:302
ID:2,UserID:306
How my list should look like is(Json Format):
[{"ID":1,"UserID":[330,302]},{"ID":2,"UserID":[303,306]},{"ID":3,"UserID":[090]}]
Now let me post what i have tried so far:
final List<Integer>list=new ArrayList<>();
final List<SpareReturnModel>lisobj=new ArrayList<>();
int duplicate=0;
for(int i=0;i<tView.getSelected().size();i++){
Object o= tView.getSelected().get(i).getValue();
SpareReturnModel asset=(SpareReturnModel) o;
int flag=asset.getFlag();
if(flag==2) {
int warehouseid = asset.getWareHouseID();
asset.setWareHouseID(warehouseid);
int partid = asset.getSerialNoID();
list.add(partid);
}
else {
Log.d("s","no value for header");
}
if(duplicate!=asset.getWareHouseID()){
asset.setParlist(list);
asset.setWareHouseID(asset.getWareHouseID());
lisobj.add(asset);
list.clear();
}
duplicate=asset.getWareHouseID();
}
Gson gson=new Gson();
//this will convert list to json
String value=gson.toJson(listobj);
SpareReturn Model Class:
public class SpareReturnModel {
private Integer SerialNoID;
private String SerialNumber;
private List<Integer>parlist;
public List<Integer> getParlist() {
return parlist;
}
public void setParlist(List<Integer> parlist) {
this.parlist = parlist;
}
public Integer getFlag() {
return flag;
}
public void setFlag(Integer flag) {
this.flag = flag;
}
private Integer flag;
public Integer getWareHouseID() {
return WareHouseID;
}
public void setWareHouseID(Integer wareHouseID) {
WareHouseID = wareHouseID;
}
private Integer WareHouseID;
public Integer getSerialNoID() {
return SerialNoID;
}
public void setSerialNoID(Integer serialNoID) {
SerialNoID = serialNoID;
}
public String getSerialNumber() {
return SerialNumber;
}
public void setSerialNumber(String serialNumber) {
SerialNumber = serialNumber;
}
}
Can someone let me know how to achieve this am struggling with this.
I simplify your class to make solution clearer:
public class SpareReturnModel implements Comparable<SpareReturnModel> {
private Integer id;
private String userId;
public SpareReturnModel(Integer id, String userId) {
this.id = id;
this.userId = userId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
#Override
public int compareTo(SpareReturnModel other) {
return this.getId().compareTo(other.getId());
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SpareReturnModel model = (SpareReturnModel) o;
if (id != null ? !id.equals(model.id) : model.id != null) return false;
return userId != null ? userId.equals(model.userId) : model.userId == null;
}
#Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (userId != null ? userId.hashCode() : 0);
return result;
}
}
and add JsonSpareReturnModel
public class JsonSpareRuturnModel implements Comparable<JsonSpareRuturnModel> {
private final List<SpareReturnModel> modelList;
private final Integer id;
public JsonSpareRuturnModel(List<SpareReturnModel> modelList) {
this.modelList = modelList;
this.id = modelList.get(0).getId();
}
private final String toJson() {
return String.format("{\"ID\":%s,\"UserID\":%s}", id, formatUserIdList());
}
private String formatUserIdList() {
StringBuilder builder = new StringBuilder("[");
Iterator<SpareReturnModel> modelIterator = modelList.iterator();
while (modelIterator.hasNext()) {
builder.append(modelIterator.next().getUserId());
if (modelIterator.hasNext()) {
builder.append(",");
}
}
builder.append("]");
return builder.toString();
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JsonSpareRuturnModel that = (JsonSpareRuturnModel) o;
return id != null ? id.equals(that.id) : that.id == null;
}
#Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
#Override
public int compareTo(JsonSpareRuturnModel other) {
return this.id.compareTo(other.id);
}
#Override
public String toString() {
return toJson();
}
if you need to group by user id you need to sort your models according to id's
and place them to json format model:
public class Main {
public static void main(String[] args) {
List<SpareReturnModel> models = new ArrayList<>(Arrays.asList(
new SpareReturnModel(1, "330"),
new SpareReturnModel(2, "303"),
new SpareReturnModel(3, "090"),
new SpareReturnModel(1, "302"),
new SpareReturnModel(2, "306")
));
Map<Integer, List<SpareReturnModel>> groupById = new HashMap<>();
for (SpareReturnModel model : models) {
List<SpareReturnModel> listById = groupById.get(model.getId());
if (listById == null) {
groupById.put(model.getId(), new ArrayList<>(Arrays.asList(model)));
} else {
listById.add(model);
}
}
List<JsonSpareRuturnModel> jsonList = new ArrayList<>();
for (Map.Entry<Integer, List<SpareReturnModel>> pair : groupById.entrySet()) {
jsonList.add(new JsonSpareRuturnModel(pair.getValue()));
}
System.out.println(jsonList);
final String expected = "[{\"ID\":1,\"UserID\":[330,302]}, {\"ID\":2,\"UserID\":[303,306]}, {\"ID\":3,\"UserID\":[090]}]";
System.out.println(jsonList.toString().equals(expected));
}
}
I have two POJOs defined as follows,
public class VertexDefinition {
private final String name;
private final Vertex vertex;
public VertexDefinition(String name, Vertex vertex) {
this.name = name;
this.vertex = vertex;
}
#JsonProperty("name")
public String getName() {
return name;
}
#JsonProperty("properties")
public Iterable<PropertyDefinition> getProperties() {
if(vertex == null) {
return Collections.emptySet();
}
return Iterables.transform(vertex.getPropertyKeys(), new Function<String, PropertyDefinition>() {
#Nullable #Override public PropertyDefinition apply(#Nullable String s) {
return new PropertyDefinition(vertex, s);
}
});
}
#JsonProperty("propertyKeys")
public Iterable<String> getPropertyKeys() {
if (vertex == null) {
return Collections.emptySet();
}
return vertex.getPropertyKeys();
}
}
public class PropertyDefinition {
private final Vertex vertex;
private final String propertyName;
public PropertyDefinition(Vertex vertex, String propertyName) {
this.vertex = vertex;
this.propertyName = propertyName;
}
#JsonProperty("name")
public String getName() {
return propertyName;
}
#JsonProperty("type")
public String getType() {
final Object property = vertex.getProperty(propertyName);
if (property != null) {
return property.getClass().getTypeName();
}
return "(unknown)";
}
}
My Rest method looks as follows,
public Iterable<VertexDefinition> getSchema() {
.....
}
When I make a request I get a json response as follows,
[
{
"name" : "Foo",
"properties" : [],
"propertyKeys" : [
"a",
"b",
"c"
]
},
{
"name" : "Bar",
"properties" : [],
"propertyKeys" : [
"a",
"b",
"c"
]
}
]
In short I get an empty array returned for properties while the propertyKeys is filled in.
What am I doing wrong?
I don't think deserialization into an iterable works as you've tried. Could you try something like this instead in your getProperties method?
List<PropertyDefinition> propertyDefinitions = Arrays.asList(mapper.readValue(json, PropertyDefinition[].class))
I have json objects in the following schema:
{
name: "foo",
timestamp: 1475840608763,
payload:
{
foo: "bar"
}
}
Here, the payload field contains an embedded json object, and the schema of this object is dynamic, and different each time.
The payload object is the raw output obtained from different API services, and different methods of different API services. It isn't possible to map it to all possible values.
Is it possible to have a java class such as the following:
public class Event
{
public String name;
public long timestamp;
public JsonObject payload;
}
Or something along those lines, so I can receive the basic schema and process it, then send it to the relevant class which will convert payload to its appropriate expected class?
Using JsonNode
You could use JsonNode from the com.fasterxml.jackson.databind package:
public class Event {
public String name;
public long timestamp;
public JsonNode payload;
// Getters and setters
}
Then parse it using:
String json = "{\"name\":\"foo\",\"timestamp\":1475840608763,"
+ "\"payload\":{\"foo\":\"bar\"}}";
ObjectMapper mapper = new ObjectMapper();
Event event = mapper.readValue(json, Event.class);
Mapping JsonNode to a POJO
Consider, for example, you want to map the JsonNode instance to the following class:
public class Payload {
private String foo;
// Getters and setters
}
It can be achieved with the following piece of code:
Payload payload = mapper.treeToValue(event.getPayload(), Payload.class);
Considering a Map<String, Object>
Depending on your requirements, you could use a Map<String, Object> instead of JsonNode:
public class Event {
public String name;
public long timestamp;
public Map<String, Object> payload;
// Getters and setters
}
If you need to convert a Map<String, Object> to a POJO, use:
Payload payload = mapper.convertValue(event.getPayload(), Payload.class);
According to the Jackson documentation, the convertValue() method is functionally similar to first serializing given value into JSON, and then binding JSON data into value of given type, but should be more efficient since full serialization does not (need to) occur. However, same converters (serializers and deserializers) will be used as for data binding, meaning same object mapper configuration works.
Does this help?
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
public class Payload {
private final Map<String, Object> other = new HashMap<>();
#JsonAnyGetter
public Map<String, Object> any() {
return other;
}
#JsonAnySetter
public void set(final String name, final Object value) {
other.put(name, value);
}
public Map<String, Object> getOther() {
return other;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = (prime * result) + ((other == null) ? 0 : other.hashCode());
return result;
}
#Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Payload)) {
return false;
}
Payload other = (Payload) obj;
if (this.other == null) {
if (other.other != null) {
return false;
}
} else if (!this.other.equals(other.other)) {
return false;
}
return true;
}
#Override
public String toString() {
return "Payload [other=" + other + "]";
}
}
Then this owning class
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Outer {
private final String name;
private final long timestamp;
private final Payload payload;
#JsonCreator
public Outer(#JsonProperty("name") final String name, #JsonProperty("timestamp") final long timestamp, #JsonProperty("payload") final Payload payload) {
super();
this.name = name;
this.timestamp = timestamp;
this.payload = payload;
}
public String getName() {
return name;
}
public long getTimestamp() {
return timestamp;
}
public Payload getPayload() {
return payload;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = (prime * result) + ((name == null) ? 0 : name.hashCode());
result = (prime * result) + ((payload == null) ? 0 : payload.hashCode());
result = (prime * result) + (int) (timestamp ^ (timestamp >>> 32));
return result;
}
#Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Outer)) {
return false;
}
Outer other = (Outer) obj;
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (payload == null) {
if (other.payload != null) {
return false;
}
} else if (!payload.equals(other.payload)) {
return false;
}
if (timestamp != other.timestamp) {
return false;
}
return true;
}
#Override
public String toString() {
return "Outer [name=" + name + ", timestamp=" + timestamp + ", payload=" + payload + "]";
}
}
Then to test
public class Main {
private static final ObjectMapper mapper = new ObjectMapper();
public static void main(final String... args) throws JsonParseException, JsonMappingException, IOException {
final Outer outer = mapper.readValue(new File("test.json"), Outer.class);
System.out.println(outer);
}
}
Gives console output of
Outer [name=foo, timestamp=1475840608763, payload=Payload [other={foo=bar}]]
I am a beginner in android.I am calling a webservice from my android project which returns a json string as response which contain a xml formatted string as one entry.
String jsoncontent=restTemplate.getForObject(constr+"getAssetdata/{Id}", String.class, curAcct.getiD());
final GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Assets.class, new AssetDeserialiser());
final Gson gson = gsonBuilder.create();
Assets assetAcc = gson.fromJson(jsoncontent, Assets.class);
Toast.makeText(getApplicationContext(), assetAcc.getKeyValueData(), 68000).show();
Below is the json string that i got as webservice response
jsoncontent={"id":39,"name":"ICICI Bank","purchaseValue":6000.0,"purchaseDate":1402403751000,"keyValueData":"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><root><Description><Name>Tax and other payments</Name><Value>433</Value></Description><Description><Name>Add more details...</Name><Value></Value></Description></root>"}
But i am getting a null value for assetAcc.getKeyValueData() after deserialization,there is no isue with other fields in assets.How to solve this issue? Please help me.
AssetDeserialiser.java:
public class AssetDeserialiser implements JsonDeserializer<Assets> {
#Override
public Assets deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
JsonObject jobject =arg0.getAsJsonObject();
final Assets asset = new Assets();
try{
asset.setId(jobject.get("id").getAsInt());
asset.setName(jobject.get("name").getAsString());
asset.setPurchaseValue(jobject.get("purchaseValue").getAsFloat());
asset.setPurchaseDate(new Timestamp(jobject.get("purchaseDate").getAsLong()));
asset.setKeyValueData(jobject.get("keyValueData").isJsonNull() ? "" : jobject.get("keyValueData").getAsString());
}catch(Exception es){
System.out.println("es "+es);
}
return asset;
}
}
Assets.java:
public class Assets implements Serializable{
private Integer id;
private String name;
private Float purchaseValue;
private Timestamp purchaseDate;
private String keyValueData;
public Assets() {
super();
// TODO Auto-generated constructor stub
}
public Assets(Integer id, String name, Float purchaseValue, Timestamp purchaseDate, String keyValueData) {
super();
this.id = id;
this.name = name;
this.purchaseValue = purchaseValue;
this.purchaseDate = purchaseDate;
this.keyValueData = keyValueData;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getPurchaseValue() {
return purchaseValue;
}
public void setPurchaseValue(Float purchaseValue) {
this.purchaseValue = purchaseValue;
}
public Timestamp getPurchaseDate() {
return purchaseDate;
}
public void setPurchaseDate(Timestamp purchaseDate) {
this.purchaseDate = purchaseDate;
}
public String getKeyValueData() {
return keyValueData;
}
public void setKeyValueData(String keyValueData) {
this.keyValueData = keyValueData;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result
+ ((keyValueData == null) ? 0 : keyValueData.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result
+ ((purchaseDate == null) ? 0 : purchaseDate.hashCode());
result = prime * result
+ ((purchaseValue == null) ? 0 : purchaseValue.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Assets other = (Assets) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (keyValueData == null) {
if (other.keyValueData != null)
return false;
} else if (!keyValueData.equals(other.keyValueData))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (purchaseDate == null) {
if (other.purchaseDate != null)
return false;
} else if (!purchaseDate.equals(other.purchaseDate))
return false;
if (purchaseValue == null) {
if (other.purchaseValue != null)
return false;
} else if (!purchaseValue.equals(other.purchaseValue))
return false;
return true;
}
#Override
public String toString() {
return name;
}
}
You can set this keyValueData after deserialisation from your json string that contain the xml string as below
String jsoncontent=restTemplate.getForObject(constr+"getAssetdata/{Id}", String.class, curAcct.getiD());
final GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Assets.class, new AssetDeserialiser());
final Gson gson = gsonBuilder.create();
Assets assetAcc = gson.fromJson(jsoncontent, Assets.class);
JSONObject jsonObj=new JSONObject(jsoncontent);
assetAcc.setKeyValueData(jsonObj.getString("keyValueData"));
1.Use Parcelable -its much faster.
2.Timestamp change to long. (Then can parce this value like this):
private String parceDate(data long){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
try {
retrun df.format(your long from Assets.class);
}catch (Exception e){
return "";
}
}
UPDATE:
Y can change your getter and setter for use Timestamp object from Assets class like this:
public void setPurchaseDate(long purchaseDate){
this.purchaseDate=purchaseDate
}
public Timestamp getPurchaseDate(){
return new Timestamp(purchaseDate); //from java.sql.Timestamp;
}
You can use jackson for deserialization.
public class AssetDeserialiser extends JsonDeserializer<Asset> {
#Override
public Asset deserialize(JsonParser arg0, DeserializationContext arg1)
throws IOException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(arg0);
final Asset asset = new Asset();
try{
asset.setId(mapper.readValue(node.get("id"),Integer.class));
asset.setName(mapper.readValue(node.get("name"),String.class));
asset.setPurchaseDate(mapper.readValue(node.get("purchaseDate"),Timestamp.class));
asset.setPurchaseValue(mapper.readValue(node.get("purchaseValue"),Float.class));
asset.setKeyValueData(mapper.readValue(node.get("keyValueData"), String.class));
}catch(Exception es){
System.out.println("es "+es);
}
return asset;
}
}
This may help you.
Also you will have to add "#JsonDeserialize(using=AssetDeserialiser.class)" at the beginning of your asset class. It is done like this:
#JsonDeserialize(using=AssetDeserialiser.class)
public class Asset implements Serializable{