0

I have a json file witch contains a list of a given type, for example:

   [{
      "createdBy":"SYSTEM_INIT",
      "status":401,
      "message":"Unauthorized",
      "description":"",
      "lang":"en"
   },{
      "createdBy":"SYSTEM_INIT",
      "status":413,
      "message":"Payload Too Large",
      "description":"The request is larger than the server is willing or able to process",
      "lang":"en"
   }{....

I have a java class(this case call it MyClass) witch represent this one entity. When i want to deserialize the json into collection i only have a Class object, with is MyClass.class . How can read in to a Collection<MyClass>

Actually it"s a spring applicaiton and this is my current code:

public boolean importFromJsonFile(String table, Class clazz) {
    Collection<Object> objectCollection = new ArrayList<>();

    ObjectMapper mapper = new ObjectMapper();
    try {
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource("init/" + table + ".import.json").getFile());
        objectCollection = mapper.readValue(file, Collection.class);
    }catch (Exception e){
        log.error("error: ", e);
        return false;
    }

    if(objectCollection.size() < 0){
        log.error("There is no data in the file");
        return false;
    }

    try {
        for(Object obj : objectCollection){
            save(obj, clazz);
        }
    }catch (Exception e){
        log.error("fatal error: ", e);
        return false;
    }

    return true;
}

So my problem after reading the file the result will be a Collection of LinkedHasMap instead of Collection of MyClass with is in this particular sample is represented by variable clazz. Is there any way to do this?

Syngularity
  • 795
  • 3
  • 17
  • 42

1 Answers1

4

There's way

List<MyClass> myObjects = Arrays.asList(mapper.readValue(file, MyClass[].class))

You can get different approaches here, however, this one is faster.

Based on the comments(As It is mentioned that class variable and file is present on the runtime):

public List getObjects(Class<T> aClass, File file) throws ClassNotFoundException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    //Fetches Arrayed Class
    Class namedClass = Class.forName("[L" + aClass.getName() + ";");
    List myObjects = Arrays.asList(mapper.readValue(file, namedClass));
    return myObjects;
}
Community
  • 1
  • 1
Pallav Jha
  • 3,409
  • 3
  • 29
  • 52
  • well this is obvious, but it's not help me, because MyClass is not constant, it can be MyCLass2 MyClass3 and ... can be a lot of class. I have a properti setting, where i expect classpaths, and get the Class object wit Class.forName(); that class should be anotated with Entity, and Table, and get table name, and the repository for it etc... So i only have the Class object of MyClass and want to create a List or what ever Collection or array – Syngularity Jul 14 '16 at 18:34
  • @Syngularity I've updated the answer, Now you can get the List on runtime – Pallav Jha Jul 14 '16 at 19:08
  • thx, it was a good starting point but it's need some fix. this line: `List myObjects = Arrays.asList(mapper.readValue(file, namedClass));` create a a list with One element win on `Object`, but the class of the Object actually namedClass[]. – Syngularity Jul 15 '16 at 08:58