3 use vars qw( $VERSION @EXPORT_OK @ISA );
9 @EXPORT_OK = qw( Load Dump LoadFile DumpFile );
11 *Load = \&YAML::Syck::LoadJSON;
12 *Dump = \&YAML::Syck::DumpJSON;
17 if ( YAML::Syck::_is_openhandle($file) ) {
18 print {$file} YAML::Syck::DumpJSON($_[0]);
22 open FH, "> $file" or die "Cannot write to $file: $!";
23 print FH YAML::Syck::DumpJSON($_[0]);
31 if ( YAML::Syck::_is_openhandle($file) ) {
32 YAML::Syck::LoadJSON(do { local $/; <$file> });
36 open FH, "< $file" or die "Cannot read from $file: $!";
37 YAML::Syck::LoadJSON(do { local $/; <FH> });
41 $JSON::Syck::ImplicitTyping = 1;
42 $JSON::Syck::Headless = 1;
43 $JSON::Syck::ImplicitUnicode = 0;
44 $JSON::Syck::SingleQuote = 0;
52 JSON::Syck - JSON is YAML (but consider using L<JSON::XS> instead!)
56 use JSON::Syck; # no exports by default
58 my $data = JSON::Syck::Load($json);
59 my $json = JSON::Syck::Dump($data);
61 # $file can be an IO object, or a filename
62 my $data = JSON::Syck::LoadFile($file);
63 JSON::Syck::DumpFile($file, $data);
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
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.
75 =head1 DIFFERENCE WITH JSON
77 You might want to know the difference between the I<JSON> module and
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>
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>.
88 Oh, and JSON::Syck doesn't use camelCase method names :-)
92 =head2 SCALAR REFERENCE
94 For now, when you pass a scalar reference to JSON::Syck, it
95 dereferences to get the actual scalar value.
97 JSON::Syck raises an exception when you pass in circular references.
99 If you want to serialize self refernecing stuff, you should use
100 YAML which supports it.
102 =head2 SUBROUTINE REFERENCE
104 When you pass subroutine reference, JSON::Syck dumps it as null.
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.
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,
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)
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.:
124 use JSON::Syck qw(Dump);
126 my $json = encode_utf8( Dump($data) );
128 Alternatively you can use Encode::JavaScript::UCS to encode Unicode
129 strings as in I<%uXXXX> form.
132 use Encode::JavaScript::UCS;
133 use JSON::Syck qw(Dump);
135 my $json_unicode_escaped = encode( 'JavaScript-UCS', Dump($data) );
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.
143 Set C<$JSON::Syck::SingleQuote> to 1 will make both C<Dump> and C<Load> expect
144 single-quoted string literals.
152 Audrey Tang E<lt>cpan@audreyt.orgE<gt>
154 Tatsuhiko Miyagawa E<lt>miyagawa@gmail.comE<gt>
158 Copyright 2005-2009 by Audrey Tang E<lt>cpan@audreyt.orgE<gt>.
160 This software is released under the MIT license cited below.
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>
166 =head2 The "MIT" License
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:
175 The above copyright notice and this permission notice shall be included in
176 all copies or substantial portions of the Software.
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.