Skip to content

Instantly share code, notes, and snippets.

@sunluman
Created August 25, 2022 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunluman/b49e12aa3e4f76390fbc08504ef38345 to your computer and use it in GitHub Desktop.
Save sunluman/b49e12aa3e4f76390fbc08504ef38345 to your computer and use it in GitHub Desktop.
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