NAME
Google::Data::JSON - General XML-JSON converter based on Google Data APIs
SYNOPSIS
use Google::Data::JSON qw( gdata add_elements );
## Convert an XML feed into a JSON feed.
$parser = Google::Data::JSON->new($xml);
print $parser->as_json;
## XML elements, which are not Atom/GData standards, should be
## added into the array by using Google::Data::JSON::add_elements,
## before converting to an XML feed or an XML::Atom object.
add_elements( qw( div p i ex:tag ) );
## Convert a JSON feed into an XML feed.
print Google::Data::JSON->new($json)->as_xml;
## gdata() is a shortcut for Google::Data::JSON->new()
print gdata($atom)->as_json;
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.
METHODS
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 hashref
-
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() .
as_xml
Converts into a string of XML.
XML elements, which are not Atom/GData standards, should be added into the array by using Google::Data::JSON::add_elements, before converting to an XML feed or an XML::Atom object.
as_json
Converts into a string of JSON.
as_atom
Converts into an XML::Atom object.
XML elements, which are not Atom/GData standards, should be added into the array by using Google::Data::JSON::add_elements, before converting to an XML feed or an XML::Atom object.
as_hashref
Converts into a Perl hash reference.
add_elements(@elements)
Adds a list of elements name, which are recognized as XML elements not attributes in converting.
get_elements
Returns a list of elements name, which are recognized as XML elements not attributes in converting.
xml_to_json($xml)
xml_to_atom($xml)
xml_to_hashref($xml)
json_to_xml($json)
json_to_atom($json)
json_to_hashref($json)
atom_to_xml($atom)
atom_to_json($atom)
atom_to_hashref($atom)
hashref_to_xml($hashref)
hashref_to_json($hashref)
hashref_to_atom($hashref)
hashref_to_hashref($hashref)
Extracts array references that have just one element.
_type_of
_is_element
_fix_keys_of
_force_array
_alleviate_array
EXPORT
None by default.
EXAMPLE OF FEEDS
The following example shows XML, JSON and Perl hash reference 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 hash reference
$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
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.