How to convert a JSON to a generic class

Asked

Viewed 137 times

0

In a project with multiple client-server applications, I have a number of configuration files in JSON, which contain the connection port, host, etc. For each application, the information in JSON is different:

configCache.json

{
    "port": "1234",
    "managerServerHost": "127.0.0.1",
    "managerServerPort": "1235",
    "cacheTimeout": "10000"
}

configSGBD.json

{
    "port": "1235",
    "studentServerHost": "127.0.0.1",
    "studentServerPort": "1236",
    "classServerHost": "127.0.0.1",
    "classServerPort": "1237"
}

configAlunos.json

{
   "port":"1236",
   "datafile":"student.data"
}

When running these applications, I need to take this JSON configuration information from each one to set Socket port, etc. I was suggested to use a single generic class to convert each JSON and get the information I need in each application. But I don’t really understand how generic classes work and how to use it in my problem. Anyone willing to help??

  • I don’t see how a generic class would help in this case. I suggest creating a JSONObject from the string with the library org.json and read the data of this object or do something similar with the Jackson library, which is more current.

  • Regarding generic classes, these are parameterized classes with an additional type, for example the generic class ArrayList<Integer> stores integers while the class ArrayList<String> stores strings. Search for genetic classes in Java to get better information, but note that there is not much point in using them here.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.