NAME

Google::Data::JSON - General XML-JSON converter based on Google Data APIs

SYNOPSIS

use Google::Data::JSON qw( gdata );

## Convert an XML document into a JSON.
$json = gdata($xml)->as_json;

## Convert an XML document into a Perl HASH.
$hash = gdata($xml)->as_hash;

## Convert a JSON into an XML document.
$xml = $gdata($json)->as_xml;

## Convert a JSON into an Atom object.
$atom = $gdata($json)->as_atom;

DESCRIPTION

Google::Data::JSON provides several methods to convert an XML feed into a JSON feed, and vice versa. The JSON format is defined in Google Data APIs, http://code.google.com/apis/gdata/json.html .

This module is not restricted to Atom Feed. Any XML documents can be converted into JSON-format, and vice versa.

The following rules are described in Google Data APIs:

Basic

- The feed is represented as a JSON object; each nested element or attribute is represented as a name/value property of the object.

- Attributes are converted to String properties.

- Child elements are converted to Object properties.

- Elements that may appear more than once are converted to Array properties.

- Text values of tags are converted to $t properties.

Namespace

- If an element has a namespace alias, the alias and element are concatenated using "$". For example, ns:element becomes ns$element.

XML

- XML version and encoding attributes are converted to attribute version and encoding of the root element, respectively.

METHODS

Google::Data::JSON->new($stream)

Creates a new parser object from $stream, such as XML and JSON, and returns the new Google::Data::JSON object. On failure, return "undef";

$stream can be any one of the following:

A filename

A filename of XML or JSON.

A string of XML or JSON

A string containing XML or JSON.

An XML::Atom object

An XML::Atom object, such as XML::Atom::Feed, XML::Atom::Entry, XML::Atom::Service, and XML::Atom::Categories.

A Perl HASH

A Perl hash referece, strictly saying, that is a reference to a data structure combined with HASH and ARRAY.

gdata($stream)

Shortcut for Google::Data::JSON->new() .

$gdata->as_xml

Converts into a string of XML.

$gdata->as_json

Converts into a string of JSON.

$gdata->as_atom

Converts into an XML::Atom object.

$gdata->as_hash

Converts into a Perl HASH.

$gdata->as_hashref

DEPRECATED

xml_to_json($xml)

xml_to_atom($xml)

xml_to_hash($xml)

json_to_xml($json)

json_to_atom($json)

json_to_hash($json)

atom_to_xml($atom)

atom_to_json($atom)

atom_to_hash($atom)

hash_to_xml($hash)

hash_to_json($hash)

hash_to_atom($hash)

atom_to_hashref

DEPRECATED

json_to_hashref

DEPRECATED

xml_to_hashref

DEPRECATED

EXPORT

None by default.

EXAMPLE OF XML and JSON

The following example shows XML and JSON versions of the same document:

XML

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Test Feed</title>
  <id>tag:example.com,2007:1</id>
  <updated>2007-01-01T00:00:00Z</updated>
  <link rel="self" href="http://example.com/feed.atom"/>
  <entry>
    <title>Test Entry 1</title>
    <id>tag:example.com,2007:2</id>
    <updated>2007-02-01T00:00:00Z</updated>
    <published>2007-02-01T00:00:00Z</published>
    <link rel="alternate" href="http://example.com/1"/>
    <link rel="edit" href="http://example.com/edit/1"/>
    <author>
      <name>Foo</name>
      <email>foo@example.com</email>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <span>Test 1</span>
      </div>
    </content>
  </entry>
  <entry>
    <title>Test Entry 2</title>
    <id>tag:example.com,2007:3</id>
    <updated>2007-03-01T00:00:00Z</updated>
    <published>2007-03-01T00:00:00Z</published>
    <link rel="alternate" href="http://example.com/2"/>
    <link rel="edit" href="http://example.com/edit/2"/>
    <author>
      <name>Bar</name>
      <email>bar@example.com</email>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <span>Test 2</span>
      </div>
    </content>
  </entry>
</feed>

JSON

{
  "feed" : {
    "xmlns" : "http://www.w3.org/2005/Atom",
    "title" : {
      "$t" : "Test Feed"
    },
    "id" : {
      "$t" : "tag:example.com,2007:1"
    },
    "updated" : {
      "$t" : "2007-01-01T00:00:00Z"
    },
    "link" : {
      "rel" : "self",
      "href" : "http://example.com/feed.atom"
    },
    "entry" : [
      {
        "published" : {
          "$t" : "2007-02-01T00:00:00Z"
        },
        "link" : [
          {
            "rel" : "alternate",
            "href" : "http://example.com/1"
          },
          {
            "rel" : "edit",
            "href" : "http://example.com/edit/1"
          }
        ],
        "content" : {
          "div" : {
            "xmlns" : "http://www.w3.org/1999/xhtml",
            "span" : {
              "$t" : "Test 1"
            }
          },
          "type" : "xhtml"
        },
        "title" : {
          "$t" : "Test Entry 1"
        },
        "id" : {
          "$t" : "tag:example.com,2007:2"
        },
        "updated" : {
          "$t" : "2007-02-01T00:00:00Z"
        },
        "author" : {
          "email" : {
            "$t" : "foo@example.com"
          },
          "name" : {
            "$t" : "Foo"
          }
        }
      },
      {
        "published" : {
          "$t" : "2007-03-01T00:00:00Z"
        },
        "link" : [
          {
            "rel" : "alternate",
            "href" : "http://example.com/2"
          },
          {
            "rel" : "edit",
            "href" : "http://example.com/edit/2"
          }
        ],
        "content" : {
          "div" : {
            "xmlns" : "http://www.w3.org/1999/xhtml",
            "span" : {
              "$t" : "Test 2"
            }
          },
          "type" : "xhtml"
        },
        "title" : {
          "$t" : "Test Entry 2"
        },
        "id" : {
          "$t" : "tag:example.com,2007:3"
        },
        "updated" : {
          "$t" : "2007-03-01T00:00:00Z"
        },
        "author" : {
          "email" : {
            "$t" : "bar@example.com"
          },
          "name" : {
            "$t" : "Bar"
          }
        }
      }
    ]
  }
}

SEE ALSO

XML::Atom

AUTHOR

Takeru INOUE <takeru.inoue _ gmail.com>

LICENCE AND COPYRIGHT

Copyright (c) 2007, Takeru INOUE <takeru.inoue _ gmail.com>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.