The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

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

SYNOPSIS

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

    ## Convert an XML feed into a JSON feed.
    print xml_to_json($xml);

    ## XML elements, which are not Atom standards, should be added into 
    ## the array, before converting to an XML feed or an XML::Atom 
    ## object.
    push @Google::Data::JSON::Elements, qw( div p i gd:when gd:where );

    ## Convert a JSON feed into an XML feed.
    print json_to_xml($json);

    ## Convert an XML::Atom object into a JSON feed.
    push @Google::Data::JSON::Elements, qw( div p i gd:when gd:where );
    print atom_to_xml($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 the Google Data APIs. Any XML documents can be converted into JSON-format.

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.

USAGE

xml_to_json($xml)

Converts an XML feed into a JSON feed. $xml can be string or file name.

xml_to_atom($xml)

Converts an XML feed into an XML::Atom object. $xml can be string or file name.

XML elements, which are not Atom standards, should be added into the array @Google::Data::JSON::Elements, before converting to an XML feed or an XML::Atom object.

xml_to_obj($xml)

Converts an XML feed into a perl object. $xml can be string or file name.

json_to_xml($json)

Converts a JSON feed into an XML feed. $json must be string, which meets the Google Data APIs.

XML elements, which are not Atom standards, should be added into the array @Google::Data::JSON::Elements, before converting to an XML feed or an XML::Atom object.

json_to_atom($json)

Converts a JSON feed into an XML::Atom object. $json must be string, which meets the Google Data APIs.

XML elements, which are not Atom standards, should be added into the array @Google::Data::JSON::Elements, before converting to an XML feed or an XML::Atom object.

json_to_obj($json)

Converts a JSON feed into a perl object. $json must be string, which meets the Google Data APIs.

atom_to_xml($atom)

Converts an XML::Atom object into an XML feed. $atom must be an XML::Atom::Feed or XML::Atom::Entry object.

XML elements, which are not Atom standards, should be added into the array @Google::Data::JSON::Elements, before converting to an XML feed or an XML::Atom object.

atom_to_json($atom)

Converts an XML::Atom object into a JSON feed. $atom must be an XML::Atom::Feed or XML::Atom::Entry object.

atom_to_obj($atom)

Converts an XML::Atom object into a perl object. $atom must be an XML::Atom::Feed or XML::Atom::Entry object.

obj_to_xml($obj)

Converts a perl object into an XML feed. $obj must be a perl object;

XML elements, which are not Atom standards, should be added into the array @Google::Data::JSON::Elements, before converting to an XML feed or an XML::Atom object.

obj_to_json($obj)

Converts a perl object into a JSON feed. $obj must be a perl object;

obj_to_atom($obj)

Converts a perl object into an XML::Atom object. $obj must be a perl object;

XML elements, which are not Atom standards, should be added into the array @Google::Data::JSON::Elements, before converting to an XML feed or an XML::Atom object.

_fix_keys

_is_element

_force_array

EXPORT

None by default.

EXAMPLE OF FEEDS

The following example shows XML, JSON and perl object versions of the same feed:

XML feed

        <?xml version="1.0" encoding="utf-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom">
          <title>dive into mark</title>
          <id>tag:example.org,2003:3</id>
          <updated>2005-07-31T12:29:29Z</updated>
          <link rel="alternate" type="text/html"
           hreflang="en" href="http://example.org/"/>
          <link rel="self" type="application/atom+xml"
           href="http://example.org/feed.atom"/>
          <entry>
            <title>Atom draft-07 snapshot</title>
            <id>tag:example.org,2003:3.2397</id>
            <updated>2005-07-31T12:29:29Z</updated>
            <published>2003-12-13T08:29:29-04:00</published>
            <link rel="alternate" type="text/html"
             href="http://example.org/2005/04/02/atom"/>
            <author>
              <name>Mark Pilgrim</name>
            </author>
            <content type="xhtml" xml:lang="en">
              <div xmlns="http://www.w3.org/1999/xhtml">
                <p><i>[Update: The Atom draft is finished.]</i></p>
              </div>
            </content>
          </entry>
        </feed>

JSON feed

        {
          "version" : "1.0",
          "encoding" : "utf-8"
          "feed" : {
            "xmlns" : "http://www.w3.org/2005/Atom",
            "link" : [
              {
                "rel" : "alternate",
                "href" : "http://example.org/",
                "type" : "text/html",
                "hreflang" : "en"
              },
              {
                "rel" : "self",
                "href" : "http://example.org/feed.atom",
                "type" : "application/atom+xml"
              }
            ],
            "entry" : {
              "link" : {
                "rel" : "alternate",
                "href" : "http://example.org/2005/04/02/atom",
                "type" : "text/html"
              },
              "published" : "2003-12-13T08:29:29-04:00",
              "content" : {
                "div" : {
                  "xmlns" : "http://www.w3.org/1999/xhtml",
                  "p" : {
                    "i" : "[Update: The Atom draft is finished.]"
                  }
                },
                "xml$lang" : "en",
                "type" : "xhtml"
              },
              "author" : {
                "name" : "Mark Pilgrim"
              },
              "updated" : "2005-07-31T12:29:29Z",
              "id" : "tag:example.org,2003:3.2397",
              "title" : "Atom draft-07 snapshot"
            },
            "title" : "dive into mark",
            "id" : "tag:example.org,2003:3",
            "updated" : "2005-07-31T12:29:29Z"
          },
        }

perl object

        $VAR1 = {
          'version' => '1.0',
          'encoding' => 'utf-8',
          'feed' => {
            'link' => [
              {
                'rel' => 'alternate',
                'href' => 'http://example.org/',
                'type' => 'text/html',
                'hreflang' => 'en'
              },
              {
                'rel' => 'self',
                'href' => 'http://example.org/feed.atom',
                'type' => 'application/atom+xml'
              }
            ],
            'xmlns' => 'http://www.w3.org/2005/Atom',
            'entry' => {
              'link' => {
                'rel' => 'alternate',
                'href' => 'http://example.org/2005/04/02/atom',
                'type' => 'text/html'
              },
              'published' => '2003-12-13T08:29:29-04:00',
              'content' => {
                'div' => {
                  'xmlns' => 'http://www.w3.org/1999/xhtml',
                  'p' => {
                    'i' => '[Update: The Atom draft is finished.]'
                  }
                },
                'type' => 'xhtml',
                'xml:lang' => 'en'
              },
              'author' => {
                'name' => 'Mark Pilgrim'
              },
              'updated' => '2005-07-31T12:29:29Z',
              'id' => 'tag:example.org,2003:3.2397',
              'title' => 'Atom draft-07 snapshot'
            },
            'updated' => '2005-07-31T12:29:29Z',
            'id' => 'tag:example.org,2003:3',
            'title' => 'dive into mark'
          },
        };
        

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.