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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:40:57+00:00 2026-05-20T00:40:57+00:00

I try to connect an app to an external service which provides me either

  • 0

I try to connect an app to an external service which provides me either an array like

[{"descr":"my description","id":"123"},...] 

or in case of an error something like

{"error":{"code":123,"message":"my message"}}

Is there a way to define this as a single class or to recognize the stream by which object is it?


Update 1: I’ve tested several versions by runnig them 100 times in an loop and calculation average execution time. In all the different versions I’ve used jackson.


This one is the baseline for the performance. It doesn’t fulfill the business requirements because the code doesn’t distinguishes between arrays and obejcts.

fcPhotos = mJsonToObjectMapper.readValue(mFcHttpHelper.doGet(urlString), FcPhotos.class);

Photos: 100 executions with an avg. duration of: 923ms


Using a parser decreases the performance by about min. 50%

JsonParser jsonParser = mJsonFactory.createJsonParser(mFcHttpHelper.doGet(urlString));

if (jsonParser.nextToken() == JsonToken.START_ARRAY) {
   fcPhotos = mJsonToObjectMapper.readValue(jsonParser, FcPhotos.class);
}

Photos: 100 executions with an avg. duration of: 1585ms


Converting the InputStream into an String and keeping the objectMapper.readValue solution leads to:

jsonString = inputStreamToString(urlString);
fcPhotos = mJsonToObjectMapper.readValue(jsonString, FcPhotos.class);

private final String inputStreamToString(final String urlString) throws UnsupportedEncodingException, IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(mFcHttpHelper.doGet(urlString), "UTF8"), 8192);
    StringBuilder stringBuilder = new StringBuilder();
    String line = null;

    while ((line = bufferedReader.readLine()) != null) {
        stringBuilder.append(line + "\n");
    }

    bufferedReader.close();
    return stringBuilder.toString();
}

Photos: 100 executions with an avg. duration of: 1191ms


end of update 1


  • 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-20T00:40:58+00:00Added an answer on May 20, 2026 at 12:40 am

    One possibility would be to use JsonParser to see what is the first token (JsonToken.START_ARRAY or JsonToken.START_OBJECT), and then do data binding. ObjectMapper can take JsonParser that either points to the first event, or has not yet been moved to the first event. Combining of JsonParser/ObjectMapper is also good when processing a stream of root-level JSON values (like would be the case for map/reduce style streams, or log entries when using JSON).

    Another way would be to first bind to tree model (JsonNode; ObjectMapper.readTree()); see if node is an ObjectNode or ArrayNode, and then use ObjectMapper.convertValue(rootNode, desiredClass). This has the benefit that you could also check out properties of the tree, in case there is no array vs object distinction.

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

Sidebar

Related Questions

I getting the following error when I try to connect to my server app
try to set host in dsn of pdo like this: <?php /* Connect to
In my Android app, I try to connect to a port on a local
I just downloaded the microsoft jdbc mssql driver to try connect my test app...
I'm getting this strange error when I try connect to my WCF with webHttpBinding
i try to connect (HttpClient) to an internal server in my app via wi-fi.
I try to connect to my BD This is my code public class JavaSQLTest
When I try to connect to local ports, Computer -> Connect local , using
When I try to connect Weblogic t3s protocol in Solaris Server, it shows this
What are the differences if I try to connect to an https using HttpURLConnection

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.