Gson.fromJson() always returns null - java

I'm trying to deserialize some JSON to a generic class. The structure is roughly as follows:
public abstract class AbstractRequest implements Constants
{
public abstract Class<?> getClazz();
}
public class GetTransaction extends AbstractTransactionRequest
{
#Override
public Class<Transaction> getClazz()
{
return Transaction.class;
}
}
And the Transaction class is as follows:
public class Transaction implements Serializable
{
#SerializedName("_id")
private String id;
private int amount;
#SerializedName("details")
private Map<String, String> transactionDetails;
private class Details {
private String issuer;
#SerializedName("redirect_url")
private String redirectUrl;
#SerializedName("approval_url")
private String approvalUrl;
}
}
All classes are slightly more complicated but I removed irrelevant variables.
Here's a JSON sample:
{
"_id": "2740096e-58a0-4677-8947-84fcc54cfaad",
"amount": 456,
"details": {
"issuer": "MYBANK",
"redirect_url": "https://example.com/redirect/MYBANK",
"approval_url": "https://example.com/v1/transaction/2740096e-58a0-4677-8947-84fcc54cfaad/MYBANK/authorize"
}
}
Now, I deserialize this code by doing
response.setData(Gson.fromJson(this.getResponse(), this.request.getClazz()));
Where setData accepts a Object, and getResponse returns the JSON as a String. I then do (Transaction) response.getData() which casts data to a Transaction. However, this is always null. Can anyone tell my why?
Sorry for the potentially confusing code!

Related

Jackson: Serializing Map<Object, BigDecimal> yields undesired object reference

Suppose I am trying to serialize the following with Jackson:
public class Investment implements Serializable{
private String shortName;
private MutualFund mutualFund;
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public MutualFund getMutualFund() {
return mutualFund;
}
public void setMutualFund(MutualFund mutualFund) {
this.mutualFund = mutualFund;
}
}
That in turn refers to:
public class MutualFund implements Serializable{
private BigDecimal expenseRatio;
private Map<Investment, BigDecimal> underlyingInvestments;
public BigDecimal getExpenseRatio() {
return BigDecimalHelper.guard(expenseRatio);
}
public void setExpenseRatio(BigDecimal expenseRatio) {
this.expenseRatio = expenseRatio;
}
public Map<Investment, BigDecimal> getUnderlyingInvestments() {
return underlyingInvestments;
}
public void setUnderlyingInvestments(Map<Investment, BigDecimal>
underlyingFunds) {
this.underlyingInvestments = underlyingFunds;
}
}
When I try to serialize this with Jackson, everything else is fine except that I end up with an Investment object reference instead of the string with attributes like I was expecting:
"underlyingInvestments":{"com.financial.detail.Investment#5d465e4b":1}}
I've tried to fashion some custom serializers, but without success as I always get an object reference for the nested Investment(s). So, I have a two part question:
Can this problem be addressed simply with Jackson annotations?
If I have to build a custom serializer, could someone kindly point me in the right direction on how to best
approach this issue given the nested nature of this (e.g. an Investment could contain a Mutual Fund, which in turn has an Investment with a Mutual Fund...)
The problem is that you are using the object Investment as keys of a Map, so the question here is, what keys do you expect the json to have? Json keys cand only be strings so the mapper is executing the toString() method of the Investment class. If you know what that key should be, then you can implement the method, like this:
public class Investment implements Serializable {
private String shortName;
private MutualFund mutualFund;
// ...
#Override
public String toString() {
return shortName;
}
}
This will create a json object like this:
{
"expenseRatio": 1,
"underlyingInvestments": {
"shortName": 10
}
}
Also, as #chrylis-cautiouslyoptimistic suggested, another option is to use #JsonValue to indicate which method to use when serializing, like this:
public class Investment implements Serializable{
private String shortName;
private MutualFund mutualFund;
#JsonValue
public String getShortName() {
return shortName;
}
// ...
}

cannot serialize interface object to json using jakson

Im trying to convert a java object to a json according to an API's declared pattern, using jakson.
but I couldnt figure it out.
how should I do that?
Here are my classes
ProcessValueBaseDto
package com.ir.tsn;
public class ProcessValueBaseDto implements Serializable {
private String id;
private ProcessValue value;
}
//methods Ommited
ProcessValue
package com.ir.tsn;
public interface ProcessValue<T> extends Serializable {
void setValues(T values);
}
//methods Ommited
ProcessValueDto
package com.ir.tsn;
public class ProcessValueDto implements
ProcessValue<List<String>> {
private List<String> values;
}
//methods Ommited
the excpected json should be like this
{
"id": "id1",
"value": {
"com.alz.ProcessValueDto": {
"values": [
"500000000"
]
}
}
}
com.alz.ProcessValueDto is the name of one of the ProcessValue.class implementations in API
thank you in advance
I can't really understand the question, but the one problem I see from the code you posted is that you have two classes with the same name. Also if the code is all in the same file you should split it up as you can't have more than one public class or interface in the same file.
You can use the given below class for given JSON.
public class MyValue
{
public List<string> values { get; set; }
}
public class Value
{
public MyValue MyValue { get; set; }
}
public class RootObject
{
public string id { get; set; }
public Value value { get; set; }
}

Polymorphic deserialization JSON by using POJOS

I want to consume a json with jax-rs my method stamp look like that.
#PostMapping("/test")
public ResponseEntity<String> consumeJson(#RequestBody TestPojo testPojo)
My json look like that
{
"code": "<code>",
"display": "<display>",
"activities": [
{
"categoryCode": "drug",
"drugDisplay" : "Ceforanide"
},{
"categoryCode": "observation",
"measurementWeight" : "80kg",
}
]
}
And i have the following pojos
public class TestPojo implements Serializable{
private String code;
private String display;
private List<ActivityPojo> activities;
// Getters & Setters
}
Now i have a super class and couple of classes inherit from it
public class ActivityPojo implements Serializable{
private String categoryCode;
}
The child classes
public class DrugPojo extends ActivityPojo implements Serializable{
private String drugDisplay;
// Getters & Setters
}
public class ObservationPojo extends ActivityPojo implements Serializable{
private String measurementWeight;
// Getters & Setters
}
Inside my webservice method i want to do something like that
List<ActivityPojo> activities = testPojo.getActivities();
for(int i = 0; i < activities.size(); i++){
if( activities.get(i) instanceof DrugPojo){
// do stuff
}
else if( activities.get(i) instanceof ObservationPojo){
// do stuff
}
}
So can polymorphically serialize my json in order to do that. Any help would be appreciated.
This question is very interresting so I did a few tests.
If I understood correctly the problem, I think this class (and the inner one) can solve it :
#Component
#SuppressWarnings("serial")
public class ActivityPojoJsonModule extends SimpleModule {
public ActivityPojoJsonModule() {
this.addDeserializer(ActivityPojo.class, new ActivityPojoDeserializer());
}
public static class ActivityPojoDeserializer extends JsonDeserializer<ActivityPojo> {
#Override
public ActivityPojo deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
ObjectCodec codec = parser.getCodec();
JsonNode node = codec.readTree(parser);
if(this.isDrug(node)) {
return codec.treeToValue(node, DrugPojo.class);
}
return codec.treeToValue(node, ObservationPojo.class);
}
private boolean isDrug(JsonNode node) {
return node.get("categoryCode").asText().equals("drug");
}
}
}
It adds a component to the Spring context that will deserialize ActivityPojo with a logic based on the value of the field categoryCode. You just have to add this class in the a scanned package and it will override the default behaviour of Jackson.

Jackson and deserialisation when you don't know the JSON tag name ahead of time?

I want to use Jackson to deserialise my JSON, from Jira, into a set of POJOs. I have most of what I want working beautifully, now I just have to decode the custom field values.
My input JSON looks like:
{
"expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
"id": "104144",
"self": "https://jira.internal.net/rest/api/2/issue/104144",
"key": "PRJ-524",
"fields": {
"summary": "Redo unit tests to load from existing project",
"components": [],
"customfield_10240": {
"self": "https://jira.internal.net/rest/api/2/customFieldOption/10158",
"value": "Normal",
"id": "10158"
}
}
I can trivially load the summary and components, since I know ahead of time what the name of those JSON elements are, and can define them in my POJO:
#JsonIgnoreProperties({ "expand", "self", "id", })
public class JiraJson
{
private JiraFields fields;
private String key;
public JiraFields getFields()
{
return fields;
}
public String getKey()
{
return key;
}
public void setFields(JiraFields newFields)
{
fields = newFields;
}
public void setKey(String newKey)
{
key = newKey;
}
}
And similarly for JiraFields:
#JsonIgnoreProperties({ "issuetype", "priority", "status" })
public class JiraFields
{
private List<JiraComponent> components;
private String summary;
public List<JiraComponent> getComponents()
{
return components;
}
public String getSummary()
{
return summary;
}
public void setComponents(List<JiraComponent> newComponents)
{
components = newComponents;
}
public void setSummary(String newSummary)
{
summary = newSummary;
}
}
However, the field custom_10240 actually differs depending on which Jira system this is run against, on one it is custom_10240, on another it is custom_10345, so I cannot hard-code this into the POJO. Using another call, it is possible to know at runtime, before the deserialisation starts, what the name of the field is, but this is not possible at compile time.
Assuming that I want to map the value field into a String on JiraFields called Importance, how do I go about doing that? Or perhaps simpler, how to map this Importance onto a JiraCustomField class?
You can use a method annotated with #JsonAnySetter that accepts all properties that are undefined (and not ignored). in case of a Json Object (like the custom field in the question) Jackson passes a Map that contains all the Object properties (it may even contain Map values in case of nested objects). You can now at run time extract whatever properties you want:
#JsonIgnoreProperties({ "issuetype", "priority", "status" })
public class JiraFields
{
private List<JiraComponent> components;
private String summary;
private String importance;
// getter/setter omitted for brevity
#JsonAnySetter
public void setCustomField(String name, Object value) {
System.out.println(name); // will print "customfield_10240"
if (value instanceof Map) { // just to make sure we got a Json Object
Map<String, Object> customfieldMap = (Map<String, Object>)value;
if (customfieldMap.containsKey("value")) { // check if object contains "value" property
setImportance(customfieldMap.get("value").toString());
}
}
}
}
After searching further, I finally found the JsonAlias annotation. This is still defined at compile time, but I had something that I could search further on!
Further searching, and I found PropertyNamingStrategy, which allows you to rename what JSON field name is expected for a setter/field. This has the advantage in that this is done via a method, and the class can be constructed at runtime.
Here is the class that I used to perform this mapping:
import java.util.Map;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
public final class CustomFieldNamingStrategy
extends PropertyNamingStrategy
{
private static final long serialVersionUID = 8263960285216239177L;
private final Map<String, String> fieldRemapping;
private final Map<String, String> reverseRemapping;
public CustomFieldNamingStrategy(Map<String, String> newFieldRemappings)
{
fieldRemapping = newFieldRemappings;
reverseRemapping = fieldRemapping.entrySet()//
.stream()//
.collect(Collectors.toMap(Map.Entry::getValue,
Map.Entry::getKey));
}
#Override
public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName)
{
if (field.getDeclaringClass().getName().equals(JiraFields.class.getName()))
{
return reverseRemapping.getOrDefault(defaultName, defaultName);
}
return defaultName;
}
#Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method,
String defaultName)
{
if (method.getDeclaringClass().getName().equals(JiraFields.class.getName()))
{
return reverseRemapping.getOrDefault(defaultName, defaultName);
}
return defaultName;
}
#Override
public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method,
String defaultName)
{
if (method.getDeclaringClass().getName().equals(JiraFields.class.getName()))
{
return reverseRemapping.getOrDefault(defaultName, defaultName);
}
return defaultName;
}
}

Gson.fromJson() throws StackOverflowError

We have this Json:
{
"id": 500,
"field1": "TESTE",
"banco": {
"id": 300,
"descricao": "BANCO_TESTE"
},
"categorias": [
{
"id": 300,
"descricao": "PT",
"publica": true
}
]
}
And my beans:
public class Asking implements Serializable {
private long id;
private String field1;
private Bank bank;
private List<Categoria> categorias;
//[getters and setters]
}
The beans Bank and Categoria:
public class Bank implements Serializable {
private Long code;
private Long id;
private String descricao;
//getters and setters
}
public class Categoria implements Serializable {
private Long code;
private Long id;
private String descricao;
private boolean marcada;
private boolean publica;
//getters and setters
}
When I call:
gson.fromJson(strJson, tokenType);
The error appears:
Method threw 'java.lang.StackOverflowError' exception.
What is wrong?
I can't reproduce this problem. One of two things are wrong here:
Your beans are not defined as you say they are. Check to see if they have other fields hidden within the getter and setter method section. This can happen if you have a circular reference.
You've stated in the comments that this is likely to be your problem. I recommend:
Remove the extra fields from your bean
Create a new class that contains the extra fields, and a field for the Asking instance
Deserialize the Asking instance using Gson, and then pass it into the new class's constructor.
You are doing something unexpected with your setup of the gson.fromJson method. Here's what I'm using that works great:
public static void parseJSON(String jsonString) {
Gson gsonParser = new Gson();
Type collectionType = new TypeToken<Asking>(){}.getType();
Asking gsonResponse = gsonParser.fromJson(jsonString, collectionType);
System.out.println(gsonResponse);
}
Either check your bean class definitions for extra fields, or, failing that, try to make your deserialization match mine.

Categories

Resources