Last active
August 29, 2015 14:20
-
-
Save alexcrt/3bdae1589b320b6405e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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