Skip to content

Instantly share code, notes, and snippets.

@alexcrt
Last active August 29, 2015 14:20
Show Gist options
  • Save alexcrt/3bdae1589b320b6405e1 to your computer and use it in GitHub Desktop.
Save alexcrt/3bdae1589b320b6405e1 to your computer and use it in GitHub Desktop.
public class RS {
public static void main(String[] args) throws FileNotFoundException {
Type t = new TypeToken<List<RateInfo>>(){}.getType();
Gson gson = new GsonBuilder().registerTypeAdapter(t, new RSDeserializer()).create();
TopLevel topLevel = gson.fromJson(new FileReader(new File("test.json")), TopLevel.class);
System.out.println(topLevel);
}
}
class TopLevel {
private AVL[] AVL;
public String toString() {
return Arrays.toString(AVL);
}
}
class AVL {
private Rates RATES;
public String toString() {
return RATES.toString();
}
}
class Rates {
private List<RateInfo> RS;
public String toString() {
return RS.toString();
}
}
class RateInfo{
private String BEG;
private String END;
private int RATE;
private String RQ;
@Override
public String toString() {
return "RateInfo{" +
"BEG='" + BEG + '\'' +
", END='" + END + '\'' +
", RATE=" + RATE +
", RQ='" + RQ + '\'' +
'}';
}
}
class RSDeserializer implements JsonDeserializer<List<RateInfo>> {
@Override
public List<RateInfo> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json instanceof JsonArray) {
return Arrays.asList(context.deserialize(json, RateInfo[].class));
}
return Collections.singletonList(context.deserialize(json, RateInfo.class));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment