Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3604706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:01:55+00:00 2026-05-18T21:01:55+00:00

I basically am making webrequests and recieving a JSON response. Depending on the request,

  • 0

I basically am making webrequests and recieving a JSON response. Depending on the request, I am parsing JSON request into objects I have created. The parsing is pretty much the same no matter what the object Im parsing into looks like. So I have a bunch of methods doing the same work only with different objects, I was wondering how I could accomplish this with generics? Here is an example

   public static ArrayList<Contact> parseStuff(String responseData) {
ArrayList<Person> People = new ArrayList<Person>();
try {

    JSONArray jsonPeople = new JSONArray(responseData);
    if (!jsonPeople.isNull(0)) {
        for (int i = 0; i < jsonPeople.length(); i++) {
            People.add(new Person(jsonPeople.getJSONObject(i)));
        }

    }

} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {

}

return People;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:01:56+00:00Added an answer on May 18, 2026 at 9:01 pm

    You should look at Effective Java 2, Item 29: Consider typesafe heterogeneous type containers

    The basic idea is that you could imagine you have some Deserializer interface. Then you can have a Map<Class<?>, Deserializer<String, Object>>. The map is basically a registry from type to the proper Deserializer for that type (which deserializes from a String (perhaps you want a json type or something instead of String, but String works) to the type of interest.

    The real trick is that you must enforce that the class key type matches the deserializer type. i.e. – For some Class that is a key in the map, you have a Deserializer as a value. You can do this with a generic method. For example:

    <T> void put(Class<T> clazz, Deserializer<String, T> deserializer) {
      map.put(clazz, deserializer);
    }
    

    Then to use the heterogeneous map, you can use a generic method again:

    <T> T deserialize(Class<T> typeToDeserializeFromJson, String toDeserialize) {
      return typeToDeserializeFromJson.cast(
          deserializers.get(tTDFJ).deserialize(toDeserialize));
    }
    

    You use the type that is specified by the method caller to

    • lookup the right `Deserializer`
    • deserialize the json text to an `Object`
    • safely cast the `Object` to the right type which we know is safe because of how we constructed the map in the first place.

    I hope what I said was at least somewhat clear. Reading the corresponding entry in EJ2 should help!

    UPDATE:

    As Abhinav points out, the GSON library will help you accomplish your final goal of deserializing objects in a way that uses generics appropriately. However, I think your question is more about generics than the end goal so I feel my answer is more appropriate. GSON must be implemented using a heterogeneous type container :-).

    Thanks, Abhinav for pointing out GSON!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a Stored Procedure that basically concatenates a long string with data from
I've spent most of the day making what are basically some housekeeping changes to
Basically I have some code to check a specific directory to see if an
Basically, I'm trying to tap into the Soap pipeline in .NET 2.0 - I
So I'm basically making an app that adds a count to a number and
I am basically making a video library where you download videos and I then
When I create a view I am basically making a new table that will
Okay, so I am basically making a script to pass post data using cURL.
So basically im making a custom button. Desired Behaviour: When a user mouses over,
Im making a DSL in lisp (basically what i think is a nicer syntax),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.