package Google::Data::JSON;

use warnings;
use strict;
use Carp;

use version; our $VERSION = qv('0.1.0');

use XML::Simple;
use JSON::Syck;
use List::MoreUtils qw( any uniq );
use File::Slurp;
use Perl6::Export::Attrs;
use UNIVERSAL::require;

## XML::Simple
my $CONFIG = {
    XMLin => {
	KeepRoot     => 1,
	ContentKey   => '$t',
	KeyAttr      => [],
	ForceArray   => 0,
	ForceContent => 1,
    },
    XMLout => {
	KeepRoot     => 1,
	ContentKey   => '$t',
	KeyAttr      => [],
	XMLDecl      => '<?xml version="1.0" encoding="utf-8"?>',
	NoSort       => 1,
    }
};

sub new {
    my $class  = shift;
    my ($stream) = @_;
    $stream = read_file $stream if $stream !~ /[\r\n]/ && -f $stream;
    my $type = ref $stream =~ /^XML::Atom/ ? 'atom'
	     : ref $stream eq 'HASH'       ? 'hash'
	     :     $stream =~ /^<\?xml/    ? 'xml'
	     :     $stream =~ /^\{/        ? 'json'
	     :                               croak "Bad stream: $stream" ;
    bless { $type => $stream }, $class;
}

sub gdata :Export { __PACKAGE__->new(@_) }

sub as_xml {
    my $self = shift;
    if ( $self->{xml} ) {
	return $self->{xml};
    }
    elsif ( $self->{atom} ) {
	return $self->{xml} = atom_to_xml( $self->{atom} );
    }
    elsif ( $self->{hash} ) {
	return $self->{xml} = hash_to_xml( $self->{hash} );
    }
    elsif ( $self->{json} ) {
	$self->{hash} = json_to_hash( $self->{json} );
	return $self->{xml} = hash_to_xml( $self->{hash} );
    }
}

sub as_atom {
    my $self = shift;
    if ( $self->{atom} ) {
	return $self->{atom};
    }
    elsif ( $self->{xml} ) {
	return $self->{atom} = xml_to_atom( $self->{xml} );
    }
    elsif ( $self->{hash} ) {
	$self->{xml} = hash_to_xml( $self->{hash} );
	return $self->{atom} = xml_to_atom( $self->{xml} );
    }
    elsif ( $self->{json} ) {
	$self->{hash} = json_to_hash( $self->{json} );
	$self->{xml} = hash_to_xml( $self->{hash} );
	return $self->{atom} = xml_to_atom( $self->{atom} );
    }
}

sub as_hash {
    my $self = shift;
    if ( $self->{hash} ) {
	return $self->{hash};
    }
    elsif ( $self->{json} ) {
	return $self->{hash} = json_to_hash( $self->{json} );
    }
    elsif ( $self->{xml} ) {
	return $self->{hash} = xml_to_hash( $self->{xml} );
    }
    elsif ( $self->{atom} ) {
	$self->{xml} = atom_to_xml( $self->{atom} );
	return $self->{hash} = xml_to_hash( $self->{xml} );
    }
}

sub as_json {
    my $self = shift;
    if ( $self->{json} ) {
	return $self->{json};
    }
    elsif ( $self->{hash} ) {
	return $self->{json} = hash_to_json( $self->{hash} );
    }
    elsif ( $self->{xml} ) {
	$self->{hash} = xml_to_hash( $self->{xml} );
	return $self->{json} = hash_to_json( $self->{hash} );
    }
    elsif ( $self->{atom} ) {
	$self->{xml} = atom_to_xml( $self->{atom} );
	$self->{hash} = xml_to_hash( $self->{xml} );
	return $self->{json} = hash_to_json( $self->{hash} );
    }
}

sub xml_to_atom :Export { 
    my ($xml) = shift;
    my ($root) = $xml =~ /<\?xml[^>]+?\?>\s*<(?:\w+:)?(\w+)/ms;
    my $module = 'XML::Atom::' . ucfirst($root);
    "$module"->require || croak $@;
    return $module->new(\$xml);
}

sub xml_to_hash :Export { XMLin( $_[0], %{ $CONFIG->{XMLin} } ) }

sub xml_to_json :Export { hash_to_json( xml_to_hash(@_) ) }

sub atom_to_xml :Export { $_[0]->as_xml }

sub atom_to_hash :Export { xml_to_hash( atom_to_xml(@_) ) }

sub atom_to_json :Export { xml_to_json( atom_to_xml(@_) ) }

sub hash_to_xml :Export { XMLout( $_[0], %{ $CONFIG->{XMLout} } ) }

sub hash_to_atom :Export { xml_to_atom( hash_to_xml(@_) ) }

sub hash_to_json :Export { JSON::Syck::Dump( $_[0] ) }

sub json_to_xml :Export { hash_to_xml( json_to_hash(@_) ) }

sub json_to_atom :Export { hash_to_atom( json_to_hash(@_) ) }

sub json_to_hash :Export { JSON::Syck::Load( $_[0] ) }

sub as_hashref {
    warn 'as_hashref is DEPRECATED and renamed to as_hash';
    $_[0]->as_hash;
}

sub xml_to_hashref :Export {
    warn 'xml_to_hashref is DEPRECATED and renamed to xml_to_hash';
    xml_to_hash(@_);
}

sub atom_to_hashref :Export {
    warn 'xml_to_hashref is DEPRECATED and renamed to atom_to_hash';
    atom_to_hash(@_);
}

sub json_to_hashref :Export {
    warn 'xml_to_hashref is DEPRECATED and renamed to json_to_hash';
    json_to_hash(@_);
}

1; # Magic true value required at end of module
__END__

=head1 NAME

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


=head1 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;

=head1 DESCRIPTION

B<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:

=head2 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.

=head2 Namespace

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

=head2 XML

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


=head1 METHODS

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

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

I<$stream> can be any one of the following:

=over 4

=item A filename

A filename of XML or JSON.

=item A string of XML or JSON

A string containing XML or JSON.

=item An XML::Atom object

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

=item A Perl HASH

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

=back

=head2 gdata($stream)

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

=head2 $gdata->as_xml

Converts into a string of XML.

=head2 $gdata->as_json

Converts into a string of JSON.

=head2 $gdata->as_atom

Converts into an XML::Atom object.

=head2 $gdata->as_hash

Converts into a Perl HASH.

=head2 $gdata->as_hashref

DEPRECATED

=head2 xml_to_json($xml)

=head2 xml_to_atom($xml)

=head2 xml_to_hash($xml)

=head2 json_to_xml($json)

=head2 json_to_atom($json)

=head2 json_to_hash($json)

=head2 atom_to_xml($atom)

=head2 atom_to_json($atom)

=head2 atom_to_hash($atom)

=head2 hash_to_xml($hash)

=head2 hash_to_json($hash)

=head2 hash_to_atom($hash)

=head2 atom_to_hashref

DEPRECATED

=head2 json_to_hashref

DEPRECATED

=head2 xml_to_hashref

DEPRECATED

=head1 EXPORT

None by default.

=head1 EXAMPLE OF XML and JSON

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

=head2 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>

=head2 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"
              }
            }
          }
        ]
      }
    }

=head1 SEE ALSO

L<XML::Atom>

=head1 AUTHOR

Takeru INOUE  C<< <takeru.inoue _ gmail.com> >>

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2007, Takeru INOUE C<< <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 L<perlartistic>.


=head1 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.

=cut
