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 7605007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:03:29+00:00 2026-05-31T00:03:29+00:00

I am trying to make the three following methods into one generic solution, I

  • 0

I am trying to make the three following methods into one generic solution, I tried some ideas which compile but don’t do well at runtime.

public static List<User> parseToUsers(HttpResponse response) {
  ObjectMapper mapper = new ObjectMapper();
  String results = parseToString(response);
  return mapper.readValue(results, new TypeReference<List<User>>() {});
}

public static List<Record> parseToRecords(HttpResponse response) {
  ObjectMapper mapper = new ObjectMapper();
  String results = parseToString(response);
  return mapper.readValue(results, new TypeReference<List<Record>>() {});
}

public static Record parseToRecord(HttpResponse response) {
  ObjectMapper mapper = new ObjectMapper();
  String results = parseToString(response);
  return mapper.readValue(results, new TypeReference<Record>() {});;
}

I have also tried to understand this blog post about Super Type Tokens.

EDIT:

This is what I came up with so far:

public static <T> T parseJsonResponse(TypeReference<T> type, HttpResponse response) throws DroidException {
    ObjectMapper mapper = new ObjectMapper();
    String results = parseResponseToString(response);
    return readValue = mapper.readValue(results, type);
}

Then I call it like this.

parseJsonResponseToList(new TypeReference<List<Record>>() {}, response)

Not really satisfieng.Is there a better solution?

  • 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-31T00:03:30+00:00Added an answer on May 31, 2026 at 12:03 am

    So what exactly is the problem? In what way do you not like it?

    Jackson has other ways for constructing generic types; so perhaps what are looking for is along lines of:

    public List<T> listOf(String json, Class<T> elementType) {
       ObjectMapper mapper = new ObjectMapper(); // should (re)use static instance for perf!
       JavaType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, elementType);
       return mapper.readValue(json, listType);
    }
    

    TypeFactory can be used to programmatically construct types that use generics — return type is JavaType, because basic Java Class is type-erased.
    TypeFactory is actually used to convert TypeReference to JavaType internally as well.

    EDIT

    As to regular, non-Collection/Map types, it’s really quite simple:

    public T parseSingle(Class<T> cls, InputStream src) throws IOException {
      return mapper.readValue(src, cls);
    }
    

    (you also do NOT want to read contents as String — not only is it slow, but it’s easy to mess up character encodings, so if possible, feed InputStream or byte[] instead)

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

Sidebar

Related Questions

I currently have three methods which I want to collapse into one: def send_email(contact,email)
Trying to make a MySQL-based application support MS SQL, I ran into the following
I am trying to make a program in Java that checks for three specific
I have been trying to get yui-css grid system to make a three column
I'm trying to make sure some data is auto-deleted when there's no more references
Trying to make a make generic select control that I can dynamically add elements
Trying to make a generic PL/SQL procedure to export data in specific XML format,
I have a service level methods, which make few changes to database and I
I'm trying to make long codes short. The original codes does something following: using
I'm trying to use DbLinq with a SQLite database, but I'm running into a

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.