I want to create a library which will do the authentication stuff (sending some authentication parameters along the request) and return the json response from an API. The JSON will usually represent an array of dictionaries, something like this:
[
{"a": 1, "b": 2},
{"a": 3, "b": 1}
]
I have done the same thing in Python and there it was no-brainer, I just return a dict which is builtin in Python.
I was wondering what would be the best way in Java? Here are some of the approaches I consider:
- Returning a stream which will be handled by user. Specifically, I use
java.net.HttpURLConnectionand by this approach I will returnconnection.getInputStream()which will be handled by the user. The drawback of this approach is that the user would need to do great part of the job (reading the stream and converting it to json-like object or whatever). The good part is the users will have flexibility to process it in any way they like, creating custom objects from classes, using third partyjsonlibraries or something else. By this approach I don't force them to use some library, because the whole code will use Java builtin functionalities. - Use third party library, like
org.jsonorGSONand return that kind of object. The drawback here is that I'll force the user to use third party library. - I could define classes for each kind of objects, there would be probably 5-6 type of classes. The drawbacks are: I will need more time to implement the solution and will force the user to use my objects.
What bothers me is that Python developers very often use dict, while in Java the developers most often prefer to work with objects of specific user defined classes. So, I can't conclude what would be the most appropriate way.