<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dhiraj Gupta on Medium]]></title>
        <description><![CDATA[Stories by Dhiraj Gupta on Medium]]></description>
        <link>https://medium.com/@dhirajgupta?source=rss-395675d33c3f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*05xO_4vgVmfdavId.jpeg</url>
            <title>Stories by Dhiraj Gupta on Medium</title>
            <link>https://medium.com/@dhirajgupta?source=rss-395675d33c3f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 06:33:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhirajgupta/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Adding JSON Pointer support to Gson]]></title>
            <link>https://medium.com/@dhirajgupta/adding-json-pointer-support-to-gson-c70c96b649dc?source=rss-395675d33c3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c70c96b649dc</guid>
            <category><![CDATA[json-pointer]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[songs]]></category>
            <category><![CDATA[rfc-6901]]></category>
            <dc:creator><![CDATA[Dhiraj Gupta]]></dc:creator>
            <pubDate>Thu, 09 Aug 2018 12:37:52 GMT</pubDate>
            <atom:updated>2018-08-09T12:37:52.944Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FxT9KVn8tApfgnNLpL2%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Ffilmeditor-horror-friday-the-13th-xT9KVn8tApfgnNLpL2&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FxT9KVn8tApfgnNLpL2%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/286b652ac96a09338ba603490d95c45e/href">https://medium.com/media/286b652ac96a09338ba603490d95c45e/href</a></iframe><p>Often, I find myself looking at Google’s GSON library for parsing JSON when I’m working on Android and always end up skipping it and going for something else. Starting working with Gson is easy enough, until you realize that you need to make a dedicated Pojo mapping class for each type of response that you’re getting. I guess this is not a problem for most developers because, well, APIs are a contract and things aren’t supposed to change once a contract is defined, right?</p><p>Well, it turns out that things, often, <em>do</em> require changing. In my case, I’m often working with 3rd party APIs over which I have no control and having to repeatedly change your Pojo classes because someone changed the API response format can be <em>super</em> frustrating. On other platforms, I’ve happily used the <a href="https://tools.ietf.org/html/rfc6901">JSON pointer specification</a> to access any path anywhere, directly navigating and getting values from anywhere in the JSON node tree without making a single data class object. On iOS, I’ve been using the <a href="https://github.com/grgcombs/JSONTools">grgcombs/JSONTools</a> pod for this, while on python my pipenv nearly always has a <a href="https://pypi.org/project/jsonpointer/">jsonpointer</a> dependency. On Android, support for JSON pointer had long been eluding me. Recently, I discovered and got Jackson’s data bind library working with it’s built in support for the pointer spec, but it was hardly as popular (at least on Android) as Gson and gave me problems with ProGuard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*jeQt0Xwl5WrPPgGFFdvnDQ.png" /><figcaption>Kotlin is delightful!</figcaption></figure><p>Recently, I’ve been working with Kotlin for a quick project that I luckily got to try it on, and so far it’s been nothing short of delightful. I again set out to look for a JSON pointer alternative and <a href="https://groups.google.com/forum/#!topic/google-gson/i1P9S0Tw-FA">found</a> to my happy surprise, a working <a href="https://github.com/johnnylambada/gson">Gson fork</a> that added support for JSON pointers! I immediately adapted it to my use and then went further and made a library out of it, so that it could perhaps be useful to others as well.</p><p><a href="https://github.com/dhiraj/gsonpointer/">dhiraj/gsonpointer</a></p><p>Consider that you’re looking at an API which returns a sample list of todos: <a href="https://jsonplaceholder.typicode.com/todos">https://jsonplaceholder.typicode.com/todos</a> and the list JSON response looks a bit like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*rKUAbfKWUn4LLXZXpH0J1Q.png" /><figcaption>JSON response for a list of Todos</figcaption></figure><p>Granted, that such a response is a typical case where you’d actually use something like a List&lt;Todo&gt; data class, but that’s what we’re here to talk about.</p><p>To fetch this using Gson’s JsonParser using OKHttp you’d possibly end up writing something like this, where you’d end up with a JsonElement holding on to the root of the tree:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/281da9e4ddeb76733f5020b19fd5a4e7/href">https://medium.com/media/281da9e4ddeb76733f5020b19fd5a4e7/href</a></iframe><p>In our case with the above JSON response, the root is actually an array of objects, which, in Gson parlance works out to a JsonArray of JsonObject, each in turn having JsonPrimitive objects.</p><p>Working with this JsonParser API is cumbersome, confusing, unnecessarily verbose with type checking and somehow <em>still</em> prone to exceptions with nulls, etc. Kotlin smart casts help a bit, but they can only take you so far:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33bac9f1ea43a0915aa4ad1e380f6c34/href">https://medium.com/media/33bac9f1ea43a0915aa4ad1e380f6c34/href</a></iframe><p>In comparison, using the JsonPointer API looks somewhat like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61ca3f1c93487df459ceb2b9430debca/href">https://medium.com/media/61ca3f1c93487df459ceb2b9430debca/href</a></iframe><p>safeString and its siblings are added as extension properties on JsonElement using the magic of Kotlin. They’ll always return a valid value for what you’re expecting at the call site, so they can be useful even if you’re not using the pointer API to navigate the JSON tree.</p><p>All in all, I had great fun making this as an add on to support using JSON pointers alongside the great Gson library. I’m going to go ahead and use this in my production apps, the first chance that I get. Please know that this library is provided as is, under the MIT license. Feedback, criticism, pull requests are all welcome, of course! :-)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c70c96b649dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>