I have a problem. I had the following code to parse a JSON string to an object:
JavaScript
x
public AgentStrategy parseJsonToObject(String jsonString) {
Gson gson = new Gson();
AgentStrategy agent = gson.fromJson(jsonString, AgentStrategy.class);
return agent;
}
But now I have a JSON with values that are not equal to the attribute name. Here is the new JSON:
JavaScript
{
"Market": "USDT",
"Coin":"BTC",
"ModuleEnabled":{
"Patterns":{
"Buy":"true",
"Sell":"true"
},
"EMA":{
"Buy":"true",
"Sell":"false"
}
}
}
And this is what the class looks like:
JavaScript
public class AgentStrategy {
public String Market;
public String Coin;
public boolean ModuleEnabledBuyEMA;
public boolean ModuleEnabledSellEMA;
public boolean ModuleEnabledBuyPatterns;
public boolean ModuleEnabledSellPatterns;
public AgentStrategy parseJsonToObject(String jsonString) {
Gson gson = new Gson();
AgentStrategy agent = gson.fromJson(jsonString, AgentStrategy.class);
return agent;
}
}
Now how can I match the JSON and class as the following:
JavaScript
"ModuleEnabled" -> "EMA" -> "Buy" = ModuleEnabledBuyEMA
"ModuleEnabled" -> "EMA" -> "Sell" = ModuleEnabledSellEMA
"ModuleEnabled" -> "Patterns" -> "Buy" = ModuleEnabledBuyPatterns
"ModuleEnabled" -> "Patterns" -> "Sell" = ModuleEnabledSellPatterns
Advertisement
Answer
You can write a custom deserializer in which you use default Gson deserialization and additionally manually parse the “problematic” fields:
JavaScript
public class AgentStrategyDeserializer implements JsonDeserializer<AgentStrategy> {
@Override
public AgentStrategy deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
Gson gson = new Gson();
AgentStrategy agent = gson.fromJson(jsonElement, AgentStrategy.class);
// At this point only Market and Coin attributes are set. Since the booleans can not be parsed they are initialized to false
JsonObject moduleEnabledJsonObject = jsonElement.getAsJsonObject().get("ModuleEnabled").getAsJsonObject();
boolean moduleEnabledBuyPatterns = moduleEnabledJsonObject.get("Patterns").getAsJsonObject().get("Buy").getAsBoolean();
boolean moduleEnabledSellPatterns = moduleEnabledJsonObject.get("Patterns").getAsJsonObject().get("Sell").getAsBoolean();
boolean moduleEnabledBuyEMA = moduleEnabledJsonObject.get("EMA").getAsJsonObject().get("Buy").getAsBoolean();
boolean moduleEnabledSellEMA = moduleEnabledJsonObject.get("EMA").getAsJsonObject().get("Sell").getAsBoolean();
agent.setModuleEnabledBuyEMA(moduleEnabledBuyEMA);
agent.setModuleEnabledSellEMA(moduleEnabledSellEMA);
agent.setModuleEnabledBuyPatterns(moduleEnabledBuyPatterns);
agent.setModuleEnabledSellPatterns(moduleEnabledSellPatterns);
return agent;
}
}
This is how you use the deserializer:
JavaScript
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(AgentStrategy.class, new AgentStrategyDeserializer());
Gson gson = gsonBuilder.create();
AgentStrategy agentStrategy = gson.fromJson(jsonString, AgentStrategy.class);