Skip to content

Latest commit

 

History

History
156 lines (117 loc) · 6.27 KB

json.md

File metadata and controls

156 lines (117 loc) · 6.27 KB

JSON

JavaScript Object Notation, useful for a variety of cases. What are the options for reading and writing it?

Gson

Probably one of the most popular JSON libraries. All the configuration is done by calling methods on a GsonBuilder. Though for most cases the defaults should work well.

Example (More at gson::user-guide)

GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
// Parse
String json = "{\"brand\":\"Jeep\", \"doors\": 3}";
Car car = gson.fromJson(json, Car.class);
// Write
json = gson.toJson(car);

Pros

Cons

Moshi

Moshi is a small, annotation-driven JSON library that offers Kotlin code-generation. It can use custom adapters to transform objects into new representations as well.

Example

Moshi moshi = new Builder().build();
JsonAdapter<Car> adapter = moshi.adapter(Car.class);
// Parse
Car car = adapter.fromJson(json);
// Write
json = adapter.toJson(car);
// Custom adapter allows changing JSON representation to a simple string (brand:doors)
// Usage: new Builder().add(new CarAdapter()).build();
class CarAdapter {
    @ToJson 
    String toJson(Car car) {
        return card.brand + ":" + car.doors;
    }
    
    @FromJson 
    Car fromJson(String car) {
        String[] split = car.split(":");
        if (split.length != 2) throw new JsonDataException("Unknown car format: " + car);
        return new Car(split[0], Integer.parseInt(split[1]));
    }
}

Pros

Cons

  • Transitive dependency on Kotlin runtime

Jackson

Jackson used to be just a JSON library but now supports a variety of data formats. It tends to be near the top of performance benchmarks when configured properly. Configuring usually entails properly annotating types with Jackson Annotations. Personally I find the documentation on Jackson to be confusing to navigate due to the wide scope of the Jackson project and its many sub-modules.

Example

ObjectMapper mapper = new ObjectMapper();
// Parse
String json = "{\"brand\":\"Jeep\", \"doors\": 3}";
Car car = mapper.readValue(json, Car.class);    
// Read generic collection
json = "[{\"brand\":\"Jeep\", \"doors\": 3}]";
List<Car> listCar = mapper.readValue(jsonCarArray, new TypeReference<List<Car>>(){});
// Transform to Json object
JsonNode node = mapper.readTree(json);
String brand = node.get("brand").asText();
// Write
mapper.writeValue(new File("car.json"), car);

Pros

Cons

Minimal JSON

As the name implies, this is a very minimal JSON parser/writer at only 33.4 KB. It does not support any type of type serialization, so you will have to implement type handling on your own.

Example

// Parsing
JsonValue value = Json.parse(string);
if (value.isString()) {
    String string = value.asString();
} else if (value.isArray()) {
    // Can also: get(index), set(index, value), remove(index)
    JsonArray array = value.asArray();
    for (JsonValue arrayValue : array) {
        // ...
    }
} else if (value.isObject()) {
    JsonObject object = value.asObject();
    // Can also: set(name, value)
    String brand = object.get("brand").asString();
    int doors = object.get("doors").asInt();
}
// Creating new json objects
JsonValue name = Json.value("Jeep");
JsonValue doors = Json.value(3);
// Writing
value.toString();

Pros

Cons

  • (This is intentional, but for some is a con) Much more of a manual effort than automated type (de)serialization other libraries offer. No fancy type casting and conversion of POJOs.