How do I install the GSON module in Java - Stack Overflow
java - Jackson Vs. Gson - Stack Overflow
[Java] Read GSON into list
java - Creating GSON Object - Stack Overflow
Videos
I did this research the last week and I ended up with the same 2 libraries. As I'm using Spring 3 (that adopts Jackson in its default Json view 'JacksonJsonView') it was more natural for me to do the same. The 2 lib are pretty much the same... at the end they simply map to a json file! :)
Anyway as you said Jackson has a + in performance and that's very important for me. The project is also quite active as you can see from their web page and that's a very good sign as well.
Jackson and Gson are the most complete Java JSON packages regarding actual data binding support; many other packages only provide primitive Map/List (or equivalent tree model) binding. Both have complete support for generic types, as well, as enough configurability for many common use cases.
Since I am more familiar with Jackson, here are some aspects where I think Jackson has more complete support than Gson (apologies if I miss a Gson feature):
- Extensive annotation support; including full inheritance, and advanced "mix-in" annotations (associate annotations with a class for cases where you can not directly add them)
- Streaming (incremental) reading, writing, for ultra-high performance (or memory-limited) use cases; can mix with data binding (bind sub-trees) -- EDIT: latest versions of Gson also include streaming reader
- Tree model (DOM-like access); can convert between various models (tree <-> java object <-> stream)
- Can use any constructors (or static factory methods), not just default constructor
- Field and getter/setter access (earlier gson versions only used fields, this may have changed)
- Out-of-box JAX-RS support
- Interoperability: can also use JAXB annotations, has support/work-arounds for common packages (joda, ibatis, cglib), JVM languages (groovy, clojure, scala)
- Ability to force static (declared) type handling for output
- Support for deserializing polymorphic types (Jackson 1.5) -- can serialize AND deserialize things like List correctly (with additional type information)
- Integrated support for binary content (base64 to/from JSON Strings)
I have the following JSON response from an API:
{
"offices": [
{
"id": "1",
"city": "louisville",
},
{
"id": "2",
"city": "phoenix",
}
]
}I saved the response in a file locally for easy testing.
I am able to read in the file, make a JsonObject in order to get the offices element, and then store the list of Office objects.
Working code:
File file = new File("src/test/resources/offices.json");
InputStream inputStream = new FileInputStream(file);
String json = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
List<Office> offices = gson.fromJson(
gson.fromJson(json, JsonObject.class).get("offices"),
new TypeToken<List<Office>>(){}.getType());I know Java 8 has introduced a lot of new features. Is there a cleaner way of doing this with lambdas and streams? Just looking for a second pair of eyes to clean up the code if needed.
JsonObject innerObject = new JsonObject();
innerObject.addProperty("name", "john");
JsonObject jsonObject = new JsonObject();
jsonObject.add("publisher", innerObject);
http://www.javadoc.io/doc/com.google.code.gson/gson
Just an FYI: Gson is really made for converting Java objects to/from JSON. If this is the main way you're using Gson, I think you're missing the point.
Figured it out how to do it correctly using Java Objects.
Creator creator = new Creator("John");
new Gson().toJson(creator);
Implementation of Creator java class.
public class Creator {
protected HashMap<String, String> publisher = new HashMap<String, String>();
public Creator(String name){
publisher.put("name", name);
}
}