Top 4 Android Libraries to Handle JSON

JSON is always a hot topic in both web and mobile development. There are lots of good libraries to handle JSON for Android. And it is not hard to choose one. You can use the one that is the most familiar to you.

Table of Contents

Gson

Gson can convert Java Objects into their JSON representation. It is often used to convert a JSON string to a respective Java object. Gson blends well with arbitrary Java objects including objects that you do not have source code of. That’s because Gson considers the use of Java Generics and Java annotations.

class BagOfPrimitives {
private int value1 = 1;
private String value2 = "abc";
private transient int value3 = 3;
BagOfPrimitives() {
// no-args constructor
}
}

// Serialization
BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);

moshi

Moshi is a modern JSON library for Android and Java, which can easily parse JSON into Java objects and serialize Java objects as JSON. It supports reading and writing Java’s core data types, including Primitives (int, float, char…) and their boxed counterparts (Integer, Float, Character…), Arrays, Collections, Lists, Sets, Maps, Strings, and Enums.

//parse JSON into Java objects
String json = …;

Moshi moshi = new Moshi.Builder().build();
JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class);

BlackjackHand blackjackHand = jsonAdapter.fromJson(json);

//serialize Java objects as JSON
BlackjackHand blackjackHand = new BlackjackHand(
new Card(‘6’, SPADES),
Arrays.asList(new Card(‘4’, CLUBS), new Card(‘A’, HEARTS)));

Moshi moshi = new Moshi.Builder().build();
JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class);

String json = jsonAdapter.toJson(blackjackHand);

jackson

Jackson is a library that processes data for Java. It supports JSON parser and generator, matching data-binding (POJOs to and from JSON) and additional data format modules to process data encoded in Avro, BSON, CBOR, CSV, Smile, (Java) Properties, Protobuf, XML or YAML; and even the large set of data format modules to support data types of widely used data types such as Guava, Joda, PCollections and many more.

If you only want to use a JSON parser, this library is not suitable as it is a jack-of-all-trade tool.

LoganSquare

LoganSquare is a fast JSON parsing and serializing library for Android. This library can utilize the power of Jackson’s streaming API without having to code using low-level code involving JsonParsers or JsonGenerator.

// Parse from an InputStream
InputStream is = …;
Image imageFromInputStream = LoganSquare.parse(is, Image.class);

// Parse from a String
String jsonString = …;
Image imageFromString = LoganSquare.parse(jsonString, Image.class);

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close