0

This may be a dumb question but I really need to find a solution for it.
This is my code:

public static <J> J[]fromJson(Class<?> classType, String fileLocation){
    try {
        Gson gson = new Gson();

        J[] data = (J[]) gson.fromJson(new FileReader(fileLocation), classType[].class);

        return (J[]) data;

    } catch (Exception oe) {
        System.err.println("Exception: " + oe.getMessage());
        return null;
    }

But Java does not allow the decalration as classType[].class (5th line). and I need to call my function as fromJson(People.class,"file.json"). I try to be generic with this method, so I can use the same method for different class types that I need from the Json file.

Can someone show me what I did wrong ?
Thank you very much.

Xitrum
  • 7,765
  • 26
  • 90
  • 126

1 Answers1

0

a walk around solution will be create an empty array

J[] arrayModel = (J[]) Array.newInstance(classType, 0);
J[] data = (J[]) gson.fromJson(new FileReader(fileLocation), arrayModel.getClass());
Xitrum
  • 7,765
  • 26
  • 90
  • 126