1

Using Java 8. I want to make a generic function that I can use for mapping the contents of a JSONArray object to a certain class. The inputs to this function will be a JSONArray and a class object that has a constructor that will turn the individual JSONObjects inside the JSONArray into the appropriate POJO.

I want something like this:

public static List<T> getObjectsFromJSONArray(Class T, JSONArray input) {
    return IntStream.range(0, input.length())
            .mapToObj(index -> (JSONObject)input.get(index))
            .collect(Collectors.toList())
            .forEach(jsonObj -> new T(jsonObj));
}

This doesn't compile. Perhaps because I am new to Java and am not accustomed to it, I want to make this a generic function.

One possible alternate solution is to make a class U<T>, that extends List and acts for all intents and purposes as a List<T>. The constructor for U would then take the whole JSONArray then my code works with the type specified. I've seen that implementation in action before, but it seems cumbersome to me. Let me know if a new class U is indeed the 'best practices' way to do this in Java.

8
  • 3
    if possible you could use a library to help you with that, gson for example, stackoverflow.com/questions/19133944/… here they solve a similar problem Commented Oct 26, 2018 at 20:23
  • 6
    Read about type erasure. "new T" is simply not possible. And yes: use a framework like Jackson or gson instead of reinventing the wheel. Commented Oct 26, 2018 at 20:26
  • 1
    @kingledion I use Jackson (because I use spring boot, which uses Jackson by default), and I’ve never had a problem with this kind of thing. What exactly did you try? BTW I highly recommend spring boot, as among many other awesome features, it serialises/deserialises from HTTP calls automatically, as in with zero json-related code: You just declare a method that returns/accepts (in your case) a list of things and hey presto you’re done. Commented Oct 27, 2018 at 0:34
  • 1
    @kingledion it’s trivial to deserialise from a String to a List<T> using jackson; would that work for you? Do you actually need to work with JSONObjects? Commented Oct 27, 2018 at 3:24
  • 1
    Besides the impossibility to do new T(...), what is .forEach(jsonObj -> new T(jsonObj)) supposed to do? Commented Oct 27, 2018 at 12:06

1 Answer 1

2

How about something like

   public static <T> List<T> getObjectsFromJSONArray(Function<JSONObject, T> factory, JSONArray input) {
    return IntStream.range(0, input.length())
      .mapToObj(index -> factory.apply((JSONObject)input.get(index)))
      .collect(Collectors.toList());
  }

and then you use it like

getObjectsFromJSONArray(YourObject::new, jsonarray)
Sign up to request clarification or add additional context in comments.

4 Comments

This is not fully what the op wants, in the example of the OP, he loops through the every entry of the json array, and you just try to cast it
This is excellent thank you. I'm not familiar with the <T> notation before the return value? What is that called? Where can I learn about that?
@kingledion it's a type parameter. Since you have tagged you question with [generics] yourself, you should have heard about it.
@kingledion you can read up on the basics of generics e.g. here: baeldung.com/java-generics

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.