Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / i486-linux-gnu-thread-multi / JSON / Syck.pm
1 package JSON::Syck;
2 use strict;
3 use vars qw( $VERSION @EXPORT_OK @ISA );
4 use Exporter;
5 use YAML::Syck ();
6
7 BEGIN {
8     $VERSION    = '0.31';
9     @EXPORT_OK  = qw( Load Dump LoadFile DumpFile );
10     @ISA        = 'Exporter';
11     *Load       = \&YAML::Syck::LoadJSON;
12     *Dump       = \&YAML::Syck::DumpJSON;
13 }
14
15 sub DumpFile {
16     my $file = shift;
17     if ( YAML::Syck::_is_openhandle($file) ) {
18         print {$file} YAML::Syck::DumpJSON($_[0]);
19     }
20     else {
21         local *FH;
22         open FH, "> $file" or die "Cannot write to $file: $!";
23         print FH YAML::Syck::DumpJSON($_[0]);
24         close FH;
25     }
26 }
27
28
29 sub LoadFile {
30     my $file = shift;
31     if ( YAML::Syck::_is_openhandle($file) ) {
32         YAML::Syck::LoadJSON(do { local $/; <$file> });
33     }
34     else {
35         local *FH;
36         open FH, "< $file" or die "Cannot read from $file: $!";
37         YAML::Syck::LoadJSON(do { local $/; <FH> });
38     }
39 }
40
41 $JSON::Syck::ImplicitTyping  = 1;
42 $JSON::Syck::Headless        = 1;
43 $JSON::Syck::ImplicitUnicode = 0;
44 $JSON::Syck::SingleQuote     = 0;
45
46 1;
47
48 __END__
49
50 =head1 NAME
51
52 JSON::Syck - JSON is YAML (but consider using L<JSON::XS> instead!)
53
54 =head1 SYNOPSIS
55
56     use JSON::Syck; # no exports by default 
57
58     my $data = JSON::Syck::Load($json);
59     my $json = JSON::Syck::Dump($data);
60
61     # $file can be an IO object, or a filename
62     my $data = JSON::Syck::LoadFile($file);
63     JSON::Syck::DumpFile($file, $data);
64
65 =head1 DESCRIPTION
66
67 JSON::Syck is a syck implementation of JSON parsing and generation. Because
68 JSON is YAML (L<http://redhanded.hobix.com/inspect/yamlIsJson.html>), using
69 syck gives you a fast and memory-efficient parser and dumper for JSON data
70 representation.
71
72 However, a newer module L<JSON::XS>, has since emerged.  It is more flexible,
73 efficient and robust, so please consider using it instead of this module.
74
75 =head1 DIFFERENCE WITH JSON
76
77 You might want to know the difference between the I<JSON> module and
78 this one.
79
80 Since JSON is a pure-perl module and JSON::Syck is based on libsyck,
81 JSON::Syck is supposed to be very fast and memory efficient. See
82 chansen's benchmark table at
83 L<http://idisk.mac.com/christian.hansen/Public/perl/serialize.pl>
84
85 JSON.pm comes with dozens of ways to do the same thing and lots of
86 options, while JSON::Syck doesn't. There's only C<Load> and C<Dump>.
87
88 Oh, and JSON::Syck doesn't use camelCase method names :-)
89
90 =head1 REFERENCES
91
92 =head2 SCALAR REFERENCE
93
94 For now, when you pass a scalar reference to JSON::Syck, it
95 dereferences to get the actual scalar value.
96
97 JSON::Syck raises an exception when you pass in circular references.
98
99 If you want to serialize self refernecing stuff, you should use
100 YAML which supports it.
101
102 =head2 SUBROUTINE REFERENCE
103
104 When you pass subroutine reference, JSON::Syck dumps it as null.
105
106 =head1 UTF-8 FLAGS
107
108 By default this module doesn't touch any of utf-8 flags set in
109 strings, and assumes UTF-8 bytes to be passed and emit.
110
111 However, when you set C<$JSON::Syck::ImplicitUnicode> to 1, this
112 module properly decodes UTF-8 binaries and sets UTF-8 flag everywhere,
113 as in:
114
115   JSON (UTF-8 bytes)   => Perl (UTF-8 flagged)
116   JSON (UTF-8 flagged) => Perl (UTF-8 flagged)
117   Perl (UTF-8 bytes)   => JSON (UTF-8 flagged)
118   Perl (UTF-8 flagged) => JSON (UTF-8 flagged)
119
120 Unfortunately, there's no implicit way to dump Perl UTF-8 flagged data
121 structure to utf-8 encoded JSON. To do this, simply use Encode module, e.g.:
122
123   use Encode;
124   use JSON::Syck qw(Dump);
125
126   my $json = encode_utf8( Dump($data) );
127
128 Alternatively you can use Encode::JavaScript::UCS to encode Unicode
129 strings as in I<%uXXXX> form.
130
131   use Encode;
132   use Encode::JavaScript::UCS;
133   use JSON::Syck qw(Dump);
134
135   my $json_unicode_escaped = encode( 'JavaScript-UCS', Dump($data) );
136
137 =head1 QUOTING
138
139 According to the JSON specification, all JSON strings are to be double-quoted.
140 However, when embedding JavaScript in HTML attributes, it may be more
141 convenient to use single quotes.
142
143 Set C<$JSON::Syck::SingleQuote> to 1 will make both C<Dump> and C<Load> expect
144 single-quoted string literals.
145
146 =head1 SEE ALSO
147
148 L<JSON::XS>,
149
150 =head1 AUTHORS
151
152 Audrey Tang E<lt>cpan@audreyt.orgE<gt>
153
154 Tatsuhiko Miyagawa E<lt>miyagawa@gmail.comE<gt>
155
156 =head1 COPYRIGHT
157
158 Copyright 2005-2009 by Audrey Tang E<lt>cpan@audreyt.orgE<gt>.
159
160 This software is released under the MIT license cited below.
161
162 The F<libsyck> code bundled with this library is released by
163 "why the lucky stiff", under a BSD-style license.  See the F<COPYING>
164 file for details.
165
166 =head2 The "MIT" License
167
168 Permission is hereby granted, free of charge, to any person obtaining a copy
169 of this software and associated documentation files (the "Software"), to deal
170 in the Software without restriction, including without limitation the rights
171 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
172 copies of the Software, and to permit persons to whom the Software is
173 furnished to do so, subject to the following conditions:
174
175 The above copyright notice and this permission notice shall be included in
176 all copies or substantial portions of the Software.
177
178 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
179 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
180 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
181 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
182 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
183 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
184 DEALINGS IN THE SOFTWARE.
185
186 =cut