Channels ▼
RSS

Web Development

GData: Accessing Google-Application Data


JSON

Fortunately, there's an alternative to Atom on the return side. If you put an alt=jasonc argument into your REST request's query sting, you get back a JSON-C version of the data rather than an Atom version. To see the difference, go to the end of this article and look at the XML that Google returns when you ask for a list of all your Calendars (and this is a relatively simple example). A full list of calendar items is much worse.

Now go to the end of this article and take a look at the JSON-C that Google returns for a similar request. It's much better, although still not perfect. Pressing the "author" field in service so that you can display the phrase "Phases of the Moon" is an odd choice, for example.

My main problem with Google's JSON is the way Google has incorrectly used generic tags like "data," "kind," and "items" rather than semantically meaningful names. Instead of

{
  ...
  "data": {
    "kind": "calendar#calendarFeed",
    ...
    "items": [
      {
        "kind": "calendar#calendar",
        ...
      },
      {
        "kind": "calendar#calendar",
        ...
      }
    ]
  }
}

They should have done it like this:

"calendarList": {
  ...
  "calendars": [
      {
        ...
      },
      {
        ...
      }
    ]
  }
}

As with the XML, proper semantic tagging (in the second example) makes it a lot easier to find what you're looking for in a large data set. More to the point, semantic tagging would have made it easy to do automated data binding to POJOs (which JSON libraries like Jackson can do quite handily). Although we can still do that, our POJO will not be nearly as easy to use as it could have been.

The main lesson from the foregoing code is that, in spite of my grousing, JSON is the better choice of return formats. Unfortunately, some of the recent additions to the APIs don't return JSON, so you're stuck with Atom feeds if you have to use those new APIs.

Just for ducks, and taking a clue from the JSON that Google actually does return, here's what the XML could (but does not) look like:

<calendarList 
    apiVersion='2.3'
    canPost='true'
    etag='W/\"AkQBSH8-fSp7IWA9WxBaGEs.'
    id='http:<i>//www.google.com/calendar/feeds/default/allcalendars/full'</i>
    updated="'010-03-29T13:12:39.155Z'
    feedLink='https:<i>//www.google.com/calendar/feeds/default/allcalendars/full'</i>
    selfLink='https:<i>//www.google.com/calendar/feeds/default/allcalendars/full?alt\u003djsonc'</i>
>
    <author displayName="Coach" email="user@gmail.com" />
    <calendar
        etag= "W/\"Ck4FQX47eCp7IWA9WxBaFEk.\""
        id                   =  "http:<i>//www.google.com/calendar/feeds/default/calendars/full/user%40google.com"</i>
        eventFeedLink        = "https:<i>//www.google.com/calendar/feeds/user%40gmail.com/private/full"</i>
        accessControlListLink= "https:<i>//www.google.com/calendar/feeds/user%40gmail.com/acl/full"</i>
        selfLink             = "https:<i>//www.google.com/calendar/feeds/default/allcalendars/full/user%40gmail.com"</i>
        created     = "2010-03-29T13:12:38.877Z"
        updated     = "2010-03-24T14:28:30.000Z"
        title       = "My Primary Calendar"
        canEdit     = "true"
        accessLevel = "owner"
        color       = "#A32929"
        hidden      = "false"
        selected    = "true"
        timeZone    = "America/Los_Angeles"
        location    = "Moutani View"
        timesCleaned= "0"
    >
        <author displayName="Coach" email="user@gmail.com" />
    </calendar>
    <calendar
        etag            = 'W/\"CUMCQX47eCp7IWA9WxBaGEk.\"'
        id              = "http:<i>//www.google.com/calendar/feeds/default/calendars/p%23moonphase%40group.v.calendar.google.com"</i>
        eventFeedLink   = "https:<i>//www.google.com/calendar/feeds/p%23moonphase%40group.v.calendar.google.com/private/full"</i>
        selfLink        = "https:<i>//www.google.com/calendar/feeds/default/allcalendars/full/p%23moonphase%40group.v.calendar.google.com"</i>
        created         = "2010-03-29T13:12:38.861Z"
        updated         = "2010-03-29T06:17:40.000Z"
        title           = "Phases of the Moon"
        details         = "Shows the primary phases of the Moon"
        canEdit         = "true"
        accessLevel     = "read"
        color           = "#AB8B00"
        hidden          = "false"
        selected        = "false"
        timeZone        = "Europe/Zurich"
        timesCleaned    = "0"
    >
        <author displayName="Coach" email="user@gmail.com" />
    </calendar>
</calendarList>

Parsing the Result

Once you have a result, you do have to parse it. There are several great libraries that will do that without difficulty. JDOM is your best choice to work with raw XML. At the Atom level, we have ROME and Eddie, although these are both really overkill for the current application.

If you opt for JSON, which I've done everywhere that I can, there's the somewhat creaky json.org parser, but there's also google-gson and the very fast and powerful Jackson library I mentioned earlier. All are better choices than the Java APIs that Google provides.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 
Dr. Dobb's TV