I'm deciding on whether to use Moshi by square or Gson to serialize and deserialize model data.
one thing i always did not like about Gson is i think it uses reflection which can be slow on android? Does Moshi use reflection also?
What are some of the pros and cons of moshi vs Gson?
I see them as similar. take for example this statement that creates a typeAdapter:
class CardAdapter { @ToJson String toJson(Card card) { return card.rank + card.suit.name().substring(0, 1); } @FromJson Card fromJson(String card) { if (card.length() != 2) throw new JsonDataException("Unknown card: " + card); char rank = card.charAt(0); switch (card.charAt(1)) { case 'C': return new Card(rank, Suit.CLUBS); case 'D': return new Card(rank, Suit.DIAMONDS); case 'H': return new Card(rank, Suit.HEARTS); case 'S': return new Card(rank, Suit.SPADES); default: throw new JsonDataException("unknown suit: " + card); } } }
and to use it register it just like in gson:
Moshi moshi = new Moshi.Builder() .add(new CardAdapter()) .build();
I guess the advantages would be the annotation being used in the typeAdapter. I'm looking to find out if there are any performance gains if I switch to Moshi.
Moshi is way faster than Gson(Link1, ) and uses less memory, This is due to its usage of Okio which can predict or expect ahead of the time the keys which helps on ignoring the unknown or unwanted fields while parsing a stream (A good article on this).
Moshi is a modern JSON library for Android and Java from Square. It can be considered as the successor to GSON, with a simpler and leaner API and an architecture enabling better performance through the use of the Okio library.
ConclusionBoth Gson and Jackson are good options for serializing/deserializing JSON data, simple to use and well documented. Advantages of Gson: Simplicity of toJson/fromJson in the simple cases. For deserialization, do not need access to the Java entities.
moshi - A modern JSON library for Android and Java.
Moshi uses Okio to optimize a few things that Gson doesn’t.
The benefits of these optimizations are particularly pronounced if you’re already using Okio streams. Users of Retrofit and OkHttp in particular benefit from Moshi.
Further discussion on the origins of Moshi are in my post, Moshi, another JSON Processor.
According to swankjesse's comment on reddit:
I’m proud of my work on Gson, but also disappointed by some of its limitations. I wanted to address these, but not as “Gson 3.0”, in part because I no longer work at Google. Jake, Scott, Eric, and I created Moshi to address the various limitations of Gson. Here’s ten small reasons to prefer Moshi over Gson:
Upcoming Kotlin support.
Qualifiers like @HexColor int permit multiple JSON representations for a single Java type.
The @ToJson and @FromJson make it easy to write and test custom JSON adapters.
JsonAdapter.failOnUnknown() lets you reject unexpected JSON data.
Predictable exceptions. Moshi throws IOException on IO problems and JsonDataException on type mismatches. Gson is all over the place.
JsonReader.selectName() avoids unnecessary UTF-8 decoding and string allocations in the common case.
You’ll ship a smaller APK. Gson is 227 KiB, Moshi+Okio together are 200 KiB.
Moshi won’t leak implementation details of platform types into your encoded JSON. This makes me afraid of Gson: gson.toJson(SimpleTimeZone.getTimeZone("GMT"))
Moshi doesn’t do weird HTML escaping by default. Look at Gson’s default encoding of "12 & 5 = 4" for an example.
No broken Date adapter installed by default.
If you’re writing new code, I highly recommend starting with Moshi. If you’ve got an existing project with Gson, you should upgrade if that’ll be simple and not risky. Otherwise stick with Gson! I’m doing my best to make sure it stays compatible and dependable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With