1

I'm working with JSON on the server for the first time. I'm trying to create an object that looks like this:

{
  columnData : {
    column1 : {"foo": "bar", "value" : 1 },
  },
  rowData : {
    row1 : {"type" : "integer",  "value" : 1},
  }
}

Essentially, it's a two-level JSON object in which I've got all objects on the top level. Everything on the second level will be a string, and integer, or a float. I've found several ways I could go about creating this JSON string:

  • Build the string directly

I can go through one line at a time and build up a giant string. This seems unwieldy.

  • Use gson

I can build a map (or class?) that has the structure of the JSON I want, and then use gson to convert to a JSON string.

  • Use JsonObject

This would work like this question. I just found that example, and I like how simple working with JSON looks in it, but I'm not sure if it's a better way to go than the other options.

I'm pretty new with Java, so I'm not really sure what the performance tradeoffs of these methods are. I do know that I'll potentially be calling this method lots of times very quickly to load data into a client as the user scrolls a table in a webapp, so I'd like everything to be as fast as possible. What's the quickest way to go about building the JSON string that I'll then be sending back to the client?

Community
  • 1
  • 1
ckersch
  • 7,507
  • 2
  • 37
  • 44
  • 1
    possible duplicate of [How to generate JSON string in Java?](http://stackoverflow.com/questions/13340138/how-to-generate-json-string-in-java) – ha9u63a7 Nov 14 '14 at 21:11
  • 1
    Just to give some perspective here: At this point in your design, just pick the one you find easiest to code and debug with. It's highly unlikely that your JSON conversion will be your performance bottleneck if you're building a GUI application. – Krease Nov 14 '14 at 21:13
  • @hagubear: That questions has answers that are suggesting both JSONObject and gson as ways of doing this. Which way is the fastest? – ckersch Nov 14 '14 at 21:14
  • Fastest to code or fastest to execute? fastest to code is to use an existing library (i.e. gson) - Now, fastest to execute means trying to do speed-comparisons between anything you can code and other (existing) libraries out there. – blurfus Nov 14 '14 at 21:18
  • I'm aiming for fastest to execute, though I don't really want to spend *too* much time on optimization, since, as Chris said, this probably won't be much of a bottleneck in the code. – ckersch Nov 14 '14 at 21:20
  • I guess I'm looking for "best performance I can get with minimal effort in actually profiling everything." :) – ckersch Nov 14 '14 at 21:21
  • I hope this example helps you (use Jackson API) http://www.journaldev.com/2324/jackson-json-processing-api-in-java-example-tutorial – JHDev Nov 14 '14 at 21:35

1 Answers1

1

Depends on what "fastest" means. Fast to develop or fast in terms of performance.

Fastest on dev is to use a library to serialize existing data structures directly to JSON. Use the following library.

http://flexjson.sourceforge.net

Performance wise comparisons it matches Jackson and beats in some cases and destroys gson. But that means you would be serializing your data structures directly rather than mapping them by hand with JSONObject or something like that.

You may be able to get a faster transaction / sec by using JSONObject by hand and mapping your data structures to it. You might get better performance, but then you might not because you have to new up JSONObject and convert the data to it. Then allow JSONObject to render. Can you write hand written code better than a serializing library? Maybe, maybe not.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138