The most common question: which parser to use for JSON parsing on Android device.
Here are several options:

  1. That comes with platform (org.json) – optimized since 2.2
  2. Jackson
  3. GSON
  4. Something else

Let’s take a look.

  1. org.json – Not very convenient to use. Doesn’t support stream parsing out of the box.
  2. jackson – very convenient to use, excellent mapping features, custom stream deserializers… but don’t use it on Android. Like Facebook and Twitter did… This will put you into the grave:) Everything is excellent before you will take a look into the ddms log.
  3. This is the the log when you are trying to open Facebook news feed (Google Nexus One, 2.2. Facebook v.1.5):

    02-25 02:49:52.862: DEBUG/dalvikvm(22579): GC_FOR_MALLOC freed 3339 objects / 504352 bytes in 68ms
    02-25 02:49:53.762: DEBUG/dalvikvm(170): GC_EXPLICIT freed 1246 objects / 63000 bytes in 94ms
    02-25 02:49:54.002: DEBUG/WifiService(85): releaseWifiLockLocked: WifiLock{NetworkLocationProvider type=2 <a href="mailto:binder=android.os.Binder@44b3cb58">binder=android.os.Binder@44b3cb58</a>}
    02-25 02:49:58.942: INFO/ActivityManager(85): Starting activity: Intent { cmp=com.facebook.katana/.activity.stream.StreamActivity }
    02-25 02:49:59.152: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 2159 objects / 607368 bytes in 37ms
    02-25 02:49:59.532: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 2156 objects / 152352 bytes in 40ms
    02-25 02:49:59.752: INFO/ActivityManager(85): Displayed activity com.facebook.katana/.activity.stream.StreamActivity: 778 ms (total 778 ms)
    02-25 02:50:00.262: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 1128 objects / 271976 bytes in 46ms
    02-25 02:50:00.632: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 4492 objects / 928160 bytes in 55ms
    02-25 02:50:03.922: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1845 objects / 556056 bytes in 66ms
    02-25 02:50:04.012: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 697 objects / 49840 bytes in 39ms
    02-25 02:50:05.702: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1686 objects / 560232 bytes in 45ms
    02-25 02:50:05.812: DEBUG/dalvikvm(14535): GC_EXPLICIT freed 1270 objects / 45920 bytes in 142ms
    02-25 02:50:05.862: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 535 objects / 26608 bytes in 70ms
    02-25 02:50:06.422: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1279 objects / 390888 bytes in 42ms
    02-25 02:50:06.782: INFO/dalvikvm(14758): Jit: resizing JitTable from 8192 to 16384
    02-25 02:50:06.952: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1490 objects / 558784 bytes in 42ms
    02-25 02:50:08.702: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1222 objects / 498232 bytes in 38ms
    02-25 02:50:09.272: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1711 objects / 349648 bytes in 72ms
    02-25 02:50:11.322: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 3758 objects / 1647488 bytes in 45ms
    02-25 02:50:12.012: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 2347 objects / 624600 bytes in 53ms
    02-25 02:50:12.142: DEBUG/dalvikvm(22579): GC_EXPLICIT freed 2020 objects / 316328 bytes in 86ms
    02-25 02:50:15.712: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 2275 objects / 823520 bytes in 65ms
    02-25 02:50:17.552: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1776 objects / 591760 bytes in 54ms
    02-25 02:50:17.822: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 623 objects / 101408 bytes in 73ms
    02-25 02:50:19.452: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1172 objects / 473632 bytes in 57ms
    02-25 02:50:20.212: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 700 objects / 74080 bytes in 46ms
    02-25 02:50:20.772: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 804 objects / 119440 bytes in 37ms
    02-25 02:50:21.672: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1529 objects / 344096 bytes in 42ms
    02-25 02:50:23.602: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 914 objects / 326848 bytes in 44ms
    02-25 02:50:23.662: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 174 objects / 7232 bytes in 40ms
    02-25 02:50:23.752: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 116 objects / 10616 bytes in 50ms
    02-25 02:50:25.202: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1286 objects / 368296 bytes in 70ms
    

    scaring, right?:)

  4. gson – GSON stream is very nice and small. It doesn’t produce so much garbage as Jackson. But it is not so convenient to use. Good news that GSON stream parser is included in API 11.

I would prefer GSON and org.json for my code.

Happy parsing!

//DL