-
-
Save sunluman/b49e12aa3e4f76390fbc08504ef38345 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
enum ResponseType{ | |
ERROR(ResponseError.class,"code") | |
,RESPONSE(Response.class,"feeTier","updateTime"); | |
private final String[] keys; | |
private final Class responseClass; | |
ResponseType(Class responseClass,String...keys){ | |
this.responseClass = responseClass; | |
this.keys = keys; | |
} | |
public static <T>T format(String responseJson){ | |
Map map = new Gson().fromJson(responseJson, Map.class); | |
for (ResponseType type : ResponseType.values()) { | |
boolean matchKeys = Arrays.stream(type.keys).allMatch(map::containsKey); | |
if(matchKeys){ | |
return (T) new Gson().fromJson(responseJson, type.responseClass); | |
} | |
} | |
throw new RuntimeException("not found match type"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment