NAME
Google::Data::JSON - General XML-JSON converter based on Google Data APIs
SYNOPSIS
use Google::Data::JSON;
## Convert an XML document into a JSON and a Perl HASH.
$gdata = Google::Data::JSON->new(xml => $xml);
$json = $gdata->as_json;
$hash = $gdata->as_hash;
## Convert a JSON into an XML document and an Atom object.
$gdata = Google::Data::JSON->new(json => $json);
$xml = $gdata->as_xml;
$atom = $gdata->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($type => $stream)
Creates a new parser object from $stream of $type, and returns the new Google::Data::JSON object. On failure, return "undef";
$type must be one of the followings:
- xml
-
$stream must be a string containing XML.
- json
-
$stream must be a string containing JSON.
- atom
-
$stream must be an XML::Atom object, such as XML::Atom::Feed, XML::Atom::Entry, XML::Atom::Service, and XML::Atom::Categories.
- hash
-
$stream must be a Perl hash referece, strictly saying, that is a reference to a data structure combined with HASH and ARRAY.
- file
-
$stream must be a filename of XML or JSON.
gdata($stream or type => $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
Google::Data::JSON->errstr or $gdata->errstr
Returns an error message.
INTERNAL METHODS
error($message)
get_type_as_dwim($stream)
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
fix_ns($hash)
fix_ns2($hash)
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"
xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">
<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"/>
<openSearch:startIndex>1</openSearch:startIndex>
<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",
"xmlns$openSearch" : "http://a9.com/-/spec/opensearchrss/1.0/",
"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"
},
"openSearch$startIndex" : {
"$t" : "1"
},
"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
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.