Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android gson streaming parser or android.util.jsonreader?

I'm looking to use a a streaming json parser in an Android app, and I am wondering whether to use gson or the android.util.jsonreader library. Supposing that licensing and version compatibility are not a problem, which should I prefer given that:

  • I process several MBs of data.
  • JSON mostly consists of compressed strings, so I don't really need an Object to JSON mapping.
like image 886
apoorv020 Avatar asked Apr 16 '12 07:04

apoorv020


People also ask

How does jsonreader work in streaming mode?

In streaming mode, every JSON data is considered an individual token. When we use JsonReader to process it, each token will be processed sequentially. For example, While parsing with JsonReader, above JSON will generate 4 tokens:

How to create Gson jsonreader?

How to create GSON JsonReader We can create a JsonReader instance using it’s simple constructor which accepts java.io.Reader type input stream. We can use one of following readers based on source of JSON stream:

What is the use of jsonreader in Java?

JsonReader The JsonReader is the streaming JSON parser and an example of pull parser. It helps in reading a JSON (RFC 7159) encoded value as a stream of tokens. It reads both literal values (strings, numbers, booleans, and nulls) as well as the begin and end delimiters of objects and arrays.

How does it read data from JSON?

It reads both literal values (strings, numbers, booleans, and nulls) as well as the begin and end delimiters of objects and arrays. The tokens are traversed in depth-first order, the same order that they appear in the JSON document. 2. Tokens In streaming mode, every JSON data is considered an individual token.


1 Answers

It doesn't much matter: android.util.JsonReader and com.google.gson.stream.JsonReader are the same code. The two classes have the same API and the same behavior.

That said, there are some tiny differences:

  • Android's JsonReader is only available in Android 3.0 and later. The Gson one is available in a standalone jar file that you can use on any version of Android.
  • Gson's may be a tiny bit faster simply because it always has the latest optimizations. The version on shipping Android devices is always slightly older than the latest and greatest. For most applications you won't notice this difference.
like image 134
Jesse Wilson Avatar answered Oct 29 '22 19:10

Jesse Wilson