0

I got a problem when I use gson to parse json file. I want to deserialize some similar json files to my objects. I typed a method to do this job, but I don't know how to apply this method to different json files. These json files have some similar structures, so I want to deserialize them into subtypes of the same supertypes.

    private Map<String, PatternDetectionRequestBody> readRequestFromJson(File jsonFile) {
        Map<String, PatternDetectionRequestBody> requestBodyMap = null;
        try {
            FileReader fileReader = new FileReader(jsonFile);
            JsonReader jsonReader = new JsonReader(fileReader);
            Gson gson = new Gson();
            Type type = new TypeToken<Map<String, PatternDetectionRequestBody>>(){}.getType();
            requestBodyMap = gson.fromJson(jsonReader, type);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return requestBodyMap;
}

As code above, I want to use this code to parse different json files by changing PatternDetectionRequestBody to some sibling classes. Could anyone tell me how to do this?

wdfeng94
  • 25
  • 2
  • 8

1 Answers1

0

Can't you just do something like this? Class<? extends ParentOfYourObject>
EDIT
Did something like this for a trial, and it worked.

private static <T> Map<String, T> readRequestFromJson(File jsonFile, TypeToken<Map<String, T>> typeToken) {
        Map<String, T> requestBodyMap = null;
        try {

            FileReader fileReader = new FileReader(jsonFile);
            JsonReader jsonReader = new JsonReader(fileReader);
            Gson gson = new Gson();

            requestBodyMap = gson.fromJson(jsonReader,  typeToken.getType());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return requestBodyMap;
}  
public static void main(String[] args) throws Exception {

        Map<String, Person> myMap = (Map<String, Person>) readRequestFromJson(new File("C:/Users/User.Admin/Desktop/jsonFile"),
                new TypeToken<Map<String, Person>>() {
                });   

        for (Map.Entry<String, Person> entry : myMap.entrySet()) {
            System.out.println(entry.getValue().getFirstName());
        }
    }  
Eldon Hipolito
  • 704
  • 1
  • 8
  • 24
  • No, I can't. What I want is to pass type into this method when I use this method. – wdfeng94 Sep 07 '16 at 03:06
  • Yea that's what I meant. Basically what would happen is you would just replace your return type to something like this `Map>` and you will pass the type as an argument, `Class extends ParentOfYourObject>` Therefore making your type something like this `Type type = new TypeToken(){}.getType();` – Eldon Hipolito Sep 07 '16 at 03:12
  • I tried. But compiler said unknown class for parameterClazz. And I found [similar question](http://stackoverflow.com/questions/20773850/gson-typetoken-with-dynamic-arraylist-item-type). It seems what I want is impossible. Anyway, thanks for your help! – wdfeng94 Sep 07 '16 at 03:28
  • Then how about just simply the parent class of your classes. Just downcast them to suit your needs. – Eldon Hipolito Sep 07 '16 at 04:02
  • Could u illustrate with a example? – wdfeng94 Sep 07 '16 at 04:10
  • Added an example. – Eldon Hipolito Sep 07 '16 at 05:29