… | |
… | |
40 | Beginning with version 2.0 of the JSON module, when both JSON and |
40 | Beginning with version 2.0 of the JSON module, when both JSON and |
41 | JSON::XS are installed, then JSON will fall back on JSON::XS (this can be |
41 | JSON::XS are installed, then JSON will fall back on JSON::XS (this can be |
42 | overridden) with no overhead due to emulation (by inheriting constructor |
42 | overridden) with no overhead due to emulation (by inheriting constructor |
43 | and methods). If JSON::XS is not available, it will fall back to the |
43 | and methods). If JSON::XS is not available, it will fall back to the |
44 | compatible JSON::PP module as backend, so using JSON instead of JSON::XS |
44 | compatible JSON::PP module as backend, so using JSON instead of JSON::XS |
45 | gives you a portable JSON API that can be fast when you need and doesn't |
45 | gives you a portable JSON API that can be fast when you need it and |
46 | require a C compiler when that is a problem. |
46 | doesn't require a C compiler when that is a problem. |
47 | |
47 | |
48 | As this is the n-th-something JSON module on CPAN, what was the reason |
48 | As this is the n-th-something JSON module on CPAN, what was the reason |
49 | to write yet another JSON module? While it seems there are many JSON |
49 | to write yet another JSON module? While it seems there are many JSON |
50 | modules, none of them correctly handle all corner cases, and in most cases |
50 | modules, none of them correctly handle all corner cases, and in most cases |
51 | their maintainers are unresponsive, gone missing, or not listening to bug |
51 | their maintainers are unresponsive, gone missing, or not listening to bug |
… | |
… | |
101 | |
101 | |
102 | package JSON::XS; |
102 | package JSON::XS; |
103 | |
103 | |
104 | use common::sense; |
104 | use common::sense; |
105 | |
105 | |
106 | our $VERSION = 2.34; |
106 | our $VERSION = 3.04; |
107 | our @ISA = qw(Exporter); |
107 | our @ISA = qw(Exporter); |
108 | |
108 | |
109 | our @EXPORT = qw(encode_json decode_json); |
109 | our @EXPORT = qw(encode_json decode_json); |
110 | |
110 | |
111 | use Exporter; |
111 | use Exporter; |
… | |
… | |
131 | |
131 | |
132 | Except being faster. |
132 | Except being faster. |
133 | |
133 | |
134 | =item $perl_scalar = decode_json $json_text |
134 | =item $perl_scalar = decode_json $json_text |
135 | |
135 | |
136 | The opposite of C<encode_json>: expects an UTF-8 (binary) string and tries |
136 | The opposite of C<encode_json>: expects a UTF-8 (binary) string and tries |
137 | to parse that as an UTF-8 encoded JSON text, returning the resulting |
137 | to parse that as a UTF-8 encoded JSON text, returning the resulting |
138 | reference. Croaks on error. |
138 | reference. Croaks on error. |
139 | |
139 | |
140 | This function call is functionally identical to: |
140 | This function call is functionally identical to: |
141 | |
141 | |
142 | $perl_scalar = JSON::XS->new->utf8->decode ($json_text) |
142 | $perl_scalar = JSON::XS->new->utf8->decode ($json_text) |
… | |
… | |
270 | |
270 | |
271 | =item $enabled = $json->get_utf8 |
271 | =item $enabled = $json->get_utf8 |
272 | |
272 | |
273 | If C<$enable> is true (or missing), then the C<encode> method will encode |
273 | If C<$enable> is true (or missing), then the C<encode> method will encode |
274 | the JSON result into UTF-8, as required by many protocols, while the |
274 | the JSON result into UTF-8, as required by many protocols, while the |
275 | C<decode> method expects to be handled an UTF-8-encoded string. Please |
275 | C<decode> method expects to be handed a UTF-8-encoded string. Please |
276 | note that UTF-8-encoded strings do not contain any characters outside the |
276 | note that UTF-8-encoded strings do not contain any characters outside the |
277 | range C<0..255>, they are thus useful for bytewise/binary I/O. In future |
277 | range C<0..255>, they are thus useful for bytewise/binary I/O. In future |
278 | versions, enabling this option might enable autodetection of the UTF-16 |
278 | versions, enabling this option might enable autodetection of the UTF-16 |
279 | and UTF-32 encoding families, as described in RFC4627. |
279 | and UTF-32 encoding families, as described in RFC4627. |
280 | |
280 | |
… | |
… | |
365 | |
365 | |
366 | =item $enabled = $json->get_relaxed |
366 | =item $enabled = $json->get_relaxed |
367 | |
367 | |
368 | If C<$enable> is true (or missing), then C<decode> will accept some |
368 | If C<$enable> is true (or missing), then C<decode> will accept some |
369 | extensions to normal JSON syntax (see below). C<encode> will not be |
369 | extensions to normal JSON syntax (see below). C<encode> will not be |
370 | affected in anyway. I<Be aware that this option makes you accept invalid |
370 | affected in any way. I<Be aware that this option makes you accept invalid |
371 | JSON texts as if they were valid!>. I suggest only to use this option to |
371 | JSON texts as if they were valid!>. I suggest only to use this option to |
372 | parse application-specific files written by humans (configuration files, |
372 | parse application-specific files written by humans (configuration files, |
373 | resource files etc.) |
373 | resource files etc.) |
374 | |
374 | |
375 | If C<$enable> is false (the default), then C<decode> will only accept |
375 | If C<$enable> is false (the default), then C<decode> will only accept |
… | |
… | |
404 | [ |
404 | [ |
405 | 1, # this comment not allowed in JSON |
405 | 1, # this comment not allowed in JSON |
406 | # neither this one... |
406 | # neither this one... |
407 | ] |
407 | ] |
408 | |
408 | |
|
|
409 | =item * literal ASCII TAB characters in strings |
|
|
410 | |
|
|
411 | Literal ASCII TAB characters are now allowed in strings (and treated as |
|
|
412 | C<\t>). |
|
|
413 | |
|
|
414 | [ |
|
|
415 | "Hello\tWorld", |
|
|
416 | "Hello<TAB>World", # literal <TAB> would not normally be allowed |
|
|
417 | ] |
|
|
418 | |
409 | =back |
419 | =back |
410 | |
420 | |
411 | =item $json = $json->canonical ([$enable]) |
421 | =item $json = $json->canonical ([$enable]) |
412 | |
422 | |
413 | =item $enabled = $json->get_canonical |
423 | =item $enabled = $json->get_canonical |
… | |
… | |
467 | |
477 | |
468 | =item $json = $json->allow_blessed ([$enable]) |
478 | =item $json = $json->allow_blessed ([$enable]) |
469 | |
479 | |
470 | =item $enabled = $json->get_allow_blessed |
480 | =item $enabled = $json->get_allow_blessed |
471 | |
481 | |
|
|
482 | See L<OBJECT SERIALISATION> for details. |
|
|
483 | |
472 | If C<$enable> is true (or missing), then the C<encode> method will not |
484 | If C<$enable> is true (or missing), then the C<encode> method will not |
473 | barf when it encounters a blessed reference. Instead, the value of the |
485 | barf when it encounters a blessed reference that it cannot convert |
474 | B<convert_blessed> option will decide whether C<null> (C<convert_blessed> |
486 | otherwise. Instead, a JSON C<null> value is encoded instead of the object. |
475 | disabled or no C<TO_JSON> method found) or a representation of the |
|
|
476 | object (C<convert_blessed> enabled and C<TO_JSON> method found) is being |
|
|
477 | encoded. Has no effect on C<decode>. |
|
|
478 | |
487 | |
479 | If C<$enable> is false (the default), then C<encode> will throw an |
488 | If C<$enable> is false (the default), then C<encode> will throw an |
480 | exception when it encounters a blessed object. |
489 | exception when it encounters a blessed object that it cannot convert |
|
|
490 | otherwise. |
|
|
491 | |
|
|
492 | This setting has no effect on C<decode>. |
481 | |
493 | |
482 | =item $json = $json->convert_blessed ([$enable]) |
494 | =item $json = $json->convert_blessed ([$enable]) |
483 | |
495 | |
484 | =item $enabled = $json->get_convert_blessed |
496 | =item $enabled = $json->get_convert_blessed |
|
|
497 | |
|
|
498 | See L<OBJECT SERIALISATION> for details. |
485 | |
499 | |
486 | If C<$enable> is true (or missing), then C<encode>, upon encountering a |
500 | If C<$enable> is true (or missing), then C<encode>, upon encountering a |
487 | blessed object, will check for the availability of the C<TO_JSON> method |
501 | blessed object, will check for the availability of the C<TO_JSON> method |
488 | on the object's class. If found, it will be called in scalar context |
502 | on the object's class. If found, it will be called in scalar context and |
489 | and the resulting scalar will be encoded instead of the object. If no |
503 | the resulting scalar will be encoded instead of the object. |
490 | C<TO_JSON> method is found, the value of C<allow_blessed> will decide what |
|
|
491 | to do. |
|
|
492 | |
504 | |
493 | The C<TO_JSON> method may safely call die if it wants. If C<TO_JSON> |
505 | The C<TO_JSON> method may safely call die if it wants. If C<TO_JSON> |
494 | returns other blessed objects, those will be handled in the same |
506 | returns other blessed objects, those will be handled in the same |
495 | way. C<TO_JSON> must take care of not causing an endless recursion cycle |
507 | way. C<TO_JSON> must take care of not causing an endless recursion cycle |
496 | (== crash) in this case. The name of C<TO_JSON> was chosen because other |
508 | (== crash) in this case. The name of C<TO_JSON> was chosen because other |
497 | methods called by the Perl core (== not by the user of the object) are |
509 | methods called by the Perl core (== not by the user of the object) are |
498 | usually in upper case letters and to avoid collisions with any C<to_json> |
510 | usually in upper case letters and to avoid collisions with any C<to_json> |
499 | function or method. |
511 | function or method. |
500 | |
512 | |
501 | This setting does not yet influence C<decode> in any way, but in the |
513 | If C<$enable> is false (the default), then C<encode> will not consider |
502 | future, global hooks might get installed that influence C<decode> and are |
514 | this type of conversion. |
503 | enabled by this setting. |
|
|
504 | |
515 | |
505 | If C<$enable> is false, then the C<allow_blessed> setting will decide what |
516 | This setting has no effect on C<decode>. |
506 | to do when a blessed object is found. |
517 | |
|
|
518 | =item $json = $json->allow_tags ([$enable]) |
|
|
519 | |
|
|
520 | =item $enabled = $json->get_allow_tags |
|
|
521 | |
|
|
522 | See L<OBJECT SERIALISATION> for details. |
|
|
523 | |
|
|
524 | If C<$enable> is true (or missing), then C<encode>, upon encountering a |
|
|
525 | blessed object, will check for the availability of the C<FREEZE> method on |
|
|
526 | the object's class. If found, it will be used to serialise the object into |
|
|
527 | a nonstandard tagged JSON value (that JSON decoders cannot decode). |
|
|
528 | |
|
|
529 | It also causes C<decode> to parse such tagged JSON values and deserialise |
|
|
530 | them via a call to the C<THAW> method. |
|
|
531 | |
|
|
532 | If C<$enable> is false (the default), then C<encode> will not consider |
|
|
533 | this type of conversion, and tagged JSON values will cause a parse error |
|
|
534 | in C<decode>, as if tags were not part of the grammar. |
507 | |
535 | |
508 | =item $json = $json->filter_json_object ([$coderef->($hashref)]) |
536 | =item $json = $json->filter_json_object ([$coderef->($hashref)]) |
509 | |
537 | |
510 | When C<$coderef> is specified, it will be called from C<decode> each |
538 | When C<$coderef> is specified, it will be called from C<decode> each |
511 | time it decodes a JSON object. The only argument is a reference to the |
539 | time it decodes a JSON object. The only argument is a reference to |
512 | newly-created hash. If the code references returns a single scalar (which |
540 | the newly-created hash. If the code reference returns a single scalar |
513 | need not be a reference), this value (i.e. a copy of that scalar to avoid |
541 | (which need not be a reference), this value (or rather a copy of it) is |
514 | aliasing) is inserted into the deserialised data structure. If it returns |
542 | inserted into the deserialised data structure. If it returns an empty |
515 | an empty list (NOTE: I<not> C<undef>, which is a valid scalar), the |
543 | list (NOTE: I<not> C<undef>, which is a valid scalar), the original |
516 | original deserialised hash will be inserted. This setting can slow down |
544 | deserialised hash will be inserted. This setting can slow down decoding |
517 | decoding considerably. |
545 | considerably. |
518 | |
546 | |
519 | When C<$coderef> is omitted or undefined, any existing callback will |
547 | When C<$coderef> is omitted or undefined, any existing callback will |
520 | be removed and C<decode> will not change the deserialised hash in any |
548 | be removed and C<decode> will not change the deserialised hash in any |
521 | way. |
549 | way. |
522 | |
550 | |
… | |
… | |
669 | |
697 | |
670 | This is useful if your JSON texts are not delimited by an outer protocol |
698 | This is useful if your JSON texts are not delimited by an outer protocol |
671 | and you need to know where the JSON text ends. |
699 | and you need to know where the JSON text ends. |
672 | |
700 | |
673 | JSON::XS->new->decode_prefix ("[1] the tail") |
701 | JSON::XS->new->decode_prefix ("[1] the tail") |
674 | => ([], 3) |
702 | => ([1], 3) |
675 | |
703 | |
676 | =back |
704 | =back |
677 | |
705 | |
678 | |
706 | |
679 | =head1 INCREMENTAL PARSING |
707 | =head1 INCREMENTAL PARSING |
… | |
… | |
720 | C<incr_skip> to skip the erroneous part). This is the most common way of |
748 | C<incr_skip> to skip the erroneous part). This is the most common way of |
721 | using the method. |
749 | using the method. |
722 | |
750 | |
723 | And finally, in list context, it will try to extract as many objects |
751 | And finally, in list context, it will try to extract as many objects |
724 | from the stream as it can find and return them, or the empty list |
752 | from the stream as it can find and return them, or the empty list |
725 | otherwise. For this to work, there must be no separators between the JSON |
753 | otherwise. For this to work, there must be no separators (other than |
726 | objects or arrays, instead they must be concatenated back-to-back. If |
754 | whitespace) between the JSON objects or arrays, instead they must be |
727 | an error occurs, an exception will be raised as in the scalar context |
755 | concatenated back-to-back. If an error occurs, an exception will be |
728 | case. Note that in this case, any previously-parsed JSON texts will be |
756 | raised as in the scalar context case. Note that in this case, any |
729 | lost. |
757 | previously-parsed JSON texts will be lost. |
730 | |
758 | |
731 | Example: Parse some JSON arrays/objects in a given string and return |
759 | Example: Parse some JSON arrays/objects in a given string and return |
732 | them. |
760 | them. |
733 | |
761 | |
734 | my @objs = JSON::XS->new->incr_parse ("[5][7][1,2]"); |
762 | my @objs = JSON::XS->new->incr_parse ("[5][7][1,2]"); |
… | |
… | |
740 | C<incr_parse> in I<scalar context> successfully returned an object. Under |
768 | C<incr_parse> in I<scalar context> successfully returned an object. Under |
741 | all other circumstances you must not call this function (I mean it. |
769 | all other circumstances you must not call this function (I mean it. |
742 | although in simple tests it might actually work, it I<will> fail under |
770 | although in simple tests it might actually work, it I<will> fail under |
743 | real world conditions). As a special exception, you can also call this |
771 | real world conditions). As a special exception, you can also call this |
744 | method before having parsed anything. |
772 | method before having parsed anything. |
|
|
773 | |
|
|
774 | That means you can only use this function to look at or manipulate text |
|
|
775 | before or after complete JSON objects, not while the parser is in the |
|
|
776 | middle of parsing a JSON object. |
745 | |
777 | |
746 | This function is useful in two cases: a) finding the trailing text after a |
778 | This function is useful in two cases: a) finding the trailing text after a |
747 | JSON object or b) parsing multiple JSON objects separated by non-JSON text |
779 | JSON object or b) parsing multiple JSON objects separated by non-JSON text |
748 | (such as commas). |
780 | (such as commas). |
749 | |
781 | |
… | |
… | |
986 | |
1018 | |
987 | =item null |
1019 | =item null |
988 | |
1020 | |
989 | A JSON null atom becomes C<undef> in Perl. |
1021 | A JSON null atom becomes C<undef> in Perl. |
990 | |
1022 | |
|
|
1023 | =item shell-style comments (C<< # I<text> >>) |
|
|
1024 | |
|
|
1025 | As a nonstandard extension to the JSON syntax that is enabled by the |
|
|
1026 | C<relaxed> setting, shell-style comments are allowed. They can start |
|
|
1027 | anywhere outside strings and go till the end of the line. |
|
|
1028 | |
|
|
1029 | =item tagged values (C<< (I<tag>)I<value> >>). |
|
|
1030 | |
|
|
1031 | Another nonstandard extension to the JSON syntax, enabled with the |
|
|
1032 | C<allow_tags> setting, are tagged values. In this implementation, the |
|
|
1033 | I<tag> must be a perl package/class name encoded as a JSON string, and the |
|
|
1034 | I<value> must be a JSON array encoding optional constructor arguments. |
|
|
1035 | |
|
|
1036 | See L<OBJECT SERIALISATION>, below, for details. |
|
|
1037 | |
991 | =back |
1038 | =back |
992 | |
1039 | |
993 | |
1040 | |
994 | =head2 PERL -> JSON |
1041 | =head2 PERL -> JSON |
995 | |
1042 | |
… | |
… | |
1032 | and JSON false values, respectively. You can also use C<\1> and C<\0> |
1079 | and JSON false values, respectively. You can also use C<\1> and C<\0> |
1033 | directly if you want. |
1080 | directly if you want. |
1034 | |
1081 | |
1035 | =item blessed objects |
1082 | =item blessed objects |
1036 | |
1083 | |
1037 | Blessed objects are not directly representable in JSON. See the |
1084 | Blessed objects are not directly representable in JSON, but C<JSON::XS> |
1038 | C<allow_blessed> and C<convert_blessed> methods on various options on |
1085 | allows various ways of handling objects. See L<OBJECT SERIALISATION>, |
1039 | how to deal with this: basically, you can choose between throwing an |
1086 | below, for details. |
1040 | exception, encoding the reference as if it weren't blessed, or provide |
|
|
1041 | your own serialiser method. |
|
|
1042 | |
1087 | |
1043 | =item simple scalars |
1088 | =item simple scalars |
1044 | |
1089 | |
1045 | Simple Perl scalars (any scalar that is not a reference) are the most |
1090 | Simple Perl scalars (any scalar that is not a reference) are the most |
1046 | difficult objects to encode: JSON::XS will encode undefined scalars as |
1091 | difficult objects to encode: JSON::XS will encode undefined scalars as |
… | |
… | |
1082 | extensions to the floating point numbers of your platform, such as |
1127 | extensions to the floating point numbers of your platform, such as |
1083 | infinities or NaN's - these cannot be represented in JSON, and it is an |
1128 | infinities or NaN's - these cannot be represented in JSON, and it is an |
1084 | error to pass those in. |
1129 | error to pass those in. |
1085 | |
1130 | |
1086 | =back |
1131 | =back |
|
|
1132 | |
|
|
1133 | =head2 OBJECT SERIALISATION |
|
|
1134 | |
|
|
1135 | As JSON cannot directly represent Perl objects, you have to choose between |
|
|
1136 | a pure JSON representation (without the ability to deserialise the object |
|
|
1137 | automatically again), and a nonstandard extension to the JSON syntax, |
|
|
1138 | tagged values. |
|
|
1139 | |
|
|
1140 | =head3 SERIALISATION |
|
|
1141 | |
|
|
1142 | What happens when C<JSON::XS> encounters a Perl object depends on the |
|
|
1143 | C<allow_blessed>, C<convert_blessed> and C<allow_tags> settings, which are |
|
|
1144 | used in this order: |
|
|
1145 | |
|
|
1146 | =over 4 |
|
|
1147 | |
|
|
1148 | =item 1. C<allow_tags> is enabled and the object has a C<FREEZE> method. |
|
|
1149 | |
|
|
1150 | In this case, C<JSON::XS> uses the L<Types::Serialiser> object |
|
|
1151 | serialisation protocol to create a tagged JSON value, using a nonstandard |
|
|
1152 | extension to the JSON syntax. |
|
|
1153 | |
|
|
1154 | This works by invoking the C<FREEZE> method on the object, with the first |
|
|
1155 | argument being the object to serialise, and the second argument being the |
|
|
1156 | constant string C<JSON> to distinguish it from other serialisers. |
|
|
1157 | |
|
|
1158 | The C<FREEZE> method can return any number of values (i.e. zero or |
|
|
1159 | more). These values and the paclkage/classname of the object will then be |
|
|
1160 | encoded as a tagged JSON value in the following format: |
|
|
1161 | |
|
|
1162 | ("classname")[FREEZE return values...] |
|
|
1163 | |
|
|
1164 | e.g.: |
|
|
1165 | |
|
|
1166 | ("URI")["http://www.google.com/"] |
|
|
1167 | ("MyDate")[2013,10,29] |
|
|
1168 | ("ImageData::JPEG")["Z3...VlCg=="] |
|
|
1169 | |
|
|
1170 | For example, the hypothetical C<My::Object> C<FREEZE> method might use the |
|
|
1171 | objects C<type> and C<id> members to encode the object: |
|
|
1172 | |
|
|
1173 | sub My::Object::FREEZE { |
|
|
1174 | my ($self, $serialiser) = @_; |
|
|
1175 | |
|
|
1176 | ($self->{type}, $self->{id}) |
|
|
1177 | } |
|
|
1178 | |
|
|
1179 | =item 2. C<convert_blessed> is enabled and the object has a C<TO_JSON> method. |
|
|
1180 | |
|
|
1181 | In this case, the C<TO_JSON> method of the object is invoked in scalar |
|
|
1182 | context. It must return a single scalar that can be directly encoded into |
|
|
1183 | JSON. This scalar replaces the object in the JSON text. |
|
|
1184 | |
|
|
1185 | For example, the following C<TO_JSON> method will convert all L<URI> |
|
|
1186 | objects to JSON strings when serialised. The fatc that these values |
|
|
1187 | originally were L<URI> objects is lost. |
|
|
1188 | |
|
|
1189 | sub URI::TO_JSON { |
|
|
1190 | my ($uri) = @_; |
|
|
1191 | $uri->as_string |
|
|
1192 | } |
|
|
1193 | |
|
|
1194 | =item 3. C<allow_blessed> is enabled. |
|
|
1195 | |
|
|
1196 | The object will be serialised as a JSON null value. |
|
|
1197 | |
|
|
1198 | =item 4. none of the above |
|
|
1199 | |
|
|
1200 | If none of the settings are enabled or the respective methods are missing, |
|
|
1201 | C<JSON::XS> throws an exception. |
|
|
1202 | |
|
|
1203 | =back |
|
|
1204 | |
|
|
1205 | =head3 DESERIALISATION |
|
|
1206 | |
|
|
1207 | For deserialisation there are only two cases to consider: either |
|
|
1208 | nonstandard tagging was used, in which case C<allow_tags> decides, |
|
|
1209 | or objects cannot be automatically be deserialised, in which |
|
|
1210 | case you can use postprocessing or the C<filter_json_object> or |
|
|
1211 | C<filter_json_single_key_object> callbacks to get some real objects our of |
|
|
1212 | your JSON. |
|
|
1213 | |
|
|
1214 | This section only considers the tagged value case: I a tagged JSON object |
|
|
1215 | is encountered during decoding and C<allow_tags> is disabled, a parse |
|
|
1216 | error will result (as if tagged values were not part of the grammar). |
|
|
1217 | |
|
|
1218 | If C<allow_tags> is enabled, C<JSON::XS> will look up the C<THAW> method |
|
|
1219 | of the package/classname used during serialisation (it will not attempt |
|
|
1220 | to load the package as a Perl module). If there is no such method, the |
|
|
1221 | decoding will fail with an error. |
|
|
1222 | |
|
|
1223 | Otherwise, the C<THAW> method is invoked with the classname as first |
|
|
1224 | argument, the constant string C<JSON> as second argument, and all the |
|
|
1225 | values from the JSON array (the values originally returned by the |
|
|
1226 | C<FREEZE> method) as remaining arguments. |
|
|
1227 | |
|
|
1228 | The method must then return the object. While technically you can return |
|
|
1229 | any Perl scalar, you might have to enable the C<enable_nonref> setting to |
|
|
1230 | make that work in all cases, so better return an actual blessed reference. |
|
|
1231 | |
|
|
1232 | As an example, let's implement a C<THAW> function that regenerates the |
|
|
1233 | C<My::Object> from the C<FREEZE> example earlier: |
|
|
1234 | |
|
|
1235 | sub My::Object::THAW { |
|
|
1236 | my ($class, $serialiser, $type, $id) = @_; |
|
|
1237 | |
|
|
1238 | $class->new (type => $type, id => $id) |
|
|
1239 | } |
1087 | |
1240 | |
1088 | |
1241 | |
1089 | =head1 ENCODING/CODESET FLAG NOTES |
1242 | =head1 ENCODING/CODESET FLAG NOTES |
1090 | |
1243 | |
1091 | The interested reader might have seen a number of flags that signify |
1244 | The interested reader might have seen a number of flags that signify |
… | |
… | |
1136 | expect your input strings to be encoded as UTF-8, that is, no "character" |
1289 | expect your input strings to be encoded as UTF-8, that is, no "character" |
1137 | of the input string must have any value > 255, as UTF-8 does not allow |
1290 | of the input string must have any value > 255, as UTF-8 does not allow |
1138 | that. |
1291 | that. |
1139 | |
1292 | |
1140 | The C<utf8> flag therefore switches between two modes: disabled means you |
1293 | The C<utf8> flag therefore switches between two modes: disabled means you |
1141 | will get a Unicode string in Perl, enabled means you get an UTF-8 encoded |
1294 | will get a Unicode string in Perl, enabled means you get a UTF-8 encoded |
1142 | octet/binary string in Perl. |
1295 | octet/binary string in Perl. |
1143 | |
1296 | |
1144 | =item C<latin1> or C<ascii> flags enabled |
1297 | =item C<latin1> or C<ascii> flags enabled |
1145 | |
1298 | |
1146 | With C<latin1> (or C<ascii>) enabled, C<encode> will escape characters |
1299 | With C<latin1> (or C<ascii>) enabled, C<encode> will escape characters |
… | |
… | |
1414 | are browser design bugs, but it is still you who will have to deal with |
1567 | are browser design bugs, but it is still you who will have to deal with |
1415 | it, as major browser developers care only for features, not about getting |
1568 | it, as major browser developers care only for features, not about getting |
1416 | security right). |
1569 | security right). |
1417 | |
1570 | |
1418 | |
1571 | |
|
|
1572 | =head1 "OLD" VS. "NEW" JSON (RFC 4627 VS. RFC 7159) |
|
|
1573 | |
|
|
1574 | TL;DR: Due to security concerns, JSON::XS will not allow scalar data in |
|
|
1575 | JSON texts by default - you need to create your own JSON::XS object and |
|
|
1576 | enable C<allow_nonref>: |
|
|
1577 | |
|
|
1578 | |
|
|
1579 | my $json = JSON::XS->new->allow_nonref; |
|
|
1580 | |
|
|
1581 | $text = $json->encode ($data); |
|
|
1582 | $data = $json->decode ($text); |
|
|
1583 | |
|
|
1584 | The long version: JSON being an important and supposedly stable format, |
|
|
1585 | the IETF standardised it as RFC 4627 in 2006. Unfortunately, the inventor |
|
|
1586 | of JSON, Dougles Crockford, unilaterally changed the definition of JSON in |
|
|
1587 | javascript. Rather than create a fork, the IETF decided to standardise the |
|
|
1588 | new syntax (apparently, so Iw as told, without finding it very amusing). |
|
|
1589 | |
|
|
1590 | The biggest difference between thed original JSON and the new JSON is that |
|
|
1591 | the new JSON supports scalars (anything other than arrays and objects) at |
|
|
1592 | the toplevel of a JSON text. While this is strictly backwards compatible |
|
|
1593 | to older versions, it breaks a number of protocols that relied on sending |
|
|
1594 | JSON back-to-back, and is a minor security concern. |
|
|
1595 | |
|
|
1596 | For example, imagine you have two banks communicating, and on one side, |
|
|
1597 | trhe JSON coder gets upgraded. Two messages, such as C<10> and C<1000> |
|
|
1598 | might then be confused to mean C<101000>, something that couldn't happen |
|
|
1599 | in the original JSON, because niether of these messages would be valid |
|
|
1600 | JSON. |
|
|
1601 | |
|
|
1602 | If one side accepts these messages, then an upgrade in the coder on either |
|
|
1603 | side could result in this becoming exploitable. |
|
|
1604 | |
|
|
1605 | This module has always allowed these messages as an optional extension, by |
|
|
1606 | default disabled. The security concerns are the reason why the default is |
|
|
1607 | still disabled, but future versions might/will likely upgrade to the newer |
|
|
1608 | RFC as default format, so you are advised to check your implementation |
|
|
1609 | and/or override the default with C<< ->allow_nonref (0) >> to ensure that |
|
|
1610 | future versions are safe. |
|
|
1611 | |
|
|
1612 | |
1419 | =head1 INTEROPERABILITY WITH OTHER MODULES |
1613 | =head1 INTEROPERABILITY WITH OTHER MODULES |
1420 | |
1614 | |
1421 | C<JSON::XS> uses the L<Types::Serialiser> module to provide boolean |
1615 | C<JSON::XS> uses the L<Types::Serialiser> module to provide boolean |
1422 | constants. That means that the JSON true and false values will be |
1616 | constants. That means that the JSON true and false values will be |
1423 | comaptible to true and false values of iother modules that do the same, |
1617 | comaptible to true and false values of other modules that do the same, |
1424 | such as L<JSON::PP> and L<CBOR::XS>. |
1618 | such as L<JSON::PP> and L<CBOR::XS>. |
1425 | |
1619 | |
1426 | |
1620 | |
|
|
1621 | =head1 INTEROPERABILITY WITH OTHER JSON DECODERS |
|
|
1622 | |
|
|
1623 | As long as you only serialise data that can be directly expressed in JSON, |
|
|
1624 | C<JSON::XS> is incapable of generating invalid JSON output (modulo bugs, |
|
|
1625 | but C<JSON::XS> has found more bugs in the official JSON testsuite (1) |
|
|
1626 | than the official JSON testsuite has found in C<JSON::XS> (0)). |
|
|
1627 | |
|
|
1628 | When you have trouble decoding JSON generated by this module using other |
|
|
1629 | decoders, then it is very likely that you have an encoding mismatch or the |
|
|
1630 | other decoder is broken. |
|
|
1631 | |
|
|
1632 | When decoding, C<JSON::XS> is strict by default and will likely catch all |
|
|
1633 | errors. There are currently two settings that change this: C<relaxed> |
|
|
1634 | makes C<JSON::XS> accept (but not generate) some non-standard extensions, |
|
|
1635 | and C<allow_tags> will allow you to encode and decode Perl objects, at the |
|
|
1636 | cost of not outputting valid JSON anymore. |
|
|
1637 | |
|
|
1638 | =head2 TAGGED VALUE SYNTAX AND STANDARD JSON EN/DECODERS |
|
|
1639 | |
|
|
1640 | When you use C<allow_tags> to use the extended (and also nonstandard and |
|
|
1641 | invalid) JSON syntax for serialised objects, and you still want to decode |
|
|
1642 | the generated When you want to serialise objects, you can run a regex |
|
|
1643 | to replace the tagged syntax by standard JSON arrays (it only works for |
|
|
1644 | "normal" package names without comma, newlines or single colons). First, |
|
|
1645 | the readable Perl version: |
|
|
1646 | |
|
|
1647 | # if your FREEZE methods return no values, you need this replace first: |
|
|
1648 | $json =~ s/\( \s* (" (?: [^\\":,]+|\\.|::)* ") \s* \) \s* \[\s*\]/[$1]/gx; |
|
|
1649 | |
|
|
1650 | # this works for non-empty constructor arg lists: |
|
|
1651 | $json =~ s/\( \s* (" (?: [^\\":,]+|\\.|::)* ") \s* \) \s* \[/[$1,/gx; |
|
|
1652 | |
|
|
1653 | And here is a less readable version that is easy to adapt to other |
|
|
1654 | languages: |
|
|
1655 | |
|
|
1656 | $json =~ s/\(\s*("([^\\":,]+|\\.|::)*")\s*\)\s*\[/[$1,/g; |
|
|
1657 | |
|
|
1658 | Here is an ECMAScript version (same regex): |
|
|
1659 | |
|
|
1660 | json = json.replace (/\(\s*("([^\\":,]+|\\.|::)*")\s*\)\s*\[/g, "[$1,"); |
|
|
1661 | |
|
|
1662 | Since this syntax converts to standard JSON arrays, it might be hard to |
|
|
1663 | distinguish serialised objects from normal arrays. You can prepend a |
|
|
1664 | "magic number" as first array element to reduce chances of a collision: |
|
|
1665 | |
|
|
1666 | $json =~ s/\(\s*("([^\\":,]+|\\.|::)*")\s*\)\s*\[/["XU1peReLzT4ggEllLanBYq4G9VzliwKF",$1,/g; |
|
|
1667 | |
|
|
1668 | And after decoding the JSON text, you could walk the data |
|
|
1669 | structure looking for arrays with a first element of |
|
|
1670 | C<XU1peReLzT4ggEllLanBYq4G9VzliwKF>. |
|
|
1671 | |
|
|
1672 | The same approach can be used to create the tagged format with another |
|
|
1673 | encoder. First, you create an array with the magic string as first member, |
|
|
1674 | the classname as second, and constructor arguments last, encode it as part |
|
|
1675 | of your JSON structure, and then: |
|
|
1676 | |
|
|
1677 | $json =~ s/\[\s*"XU1peReLzT4ggEllLanBYq4G9VzliwKF"\s*,\s*("([^\\":,]+|\\.|::)*")\s*,/($1)[/g; |
|
|
1678 | |
|
|
1679 | Again, this has some limitations - the magic string must not be encoded |
|
|
1680 | with character escapes, and the constructor arguments must be non-empty. |
|
|
1681 | |
|
|
1682 | |
|
|
1683 | =head1 RFC7159 |
|
|
1684 | |
|
|
1685 | Since this module was written, Google has written a new JSON RFC, RFC 7159 |
|
|
1686 | (and RFC7158). Unfortunately, this RFC breaks compatibility with both the |
|
|
1687 | original JSON specification on www.json.org and RFC4627. |
|
|
1688 | |
|
|
1689 | As far as I can see, you can get partial compatibility when parsing by |
|
|
1690 | using C<< ->allow_nonref >>. However, consider the security implications |
|
|
1691 | of doing so. |
|
|
1692 | |
|
|
1693 | I haven't decided yet when to break compatibility with RFC4627 by default |
|
|
1694 | (and potentially leave applications insecure) and change the default to |
|
|
1695 | follow RFC7159, but application authors are well advised to call C<< |
|
|
1696 | ->allow_nonref(0) >> even if this is the current default, if they cannot |
|
|
1697 | handle non-reference values, in preparation for the day when the default |
|
|
1698 | will change. |
|
|
1699 | |
|
|
1700 | |
1427 | =head1 THREADS |
1701 | =head1 (I-)THREADS |
1428 | |
1702 | |
1429 | This module is I<not> guaranteed to be thread safe and there are no |
1703 | This module is I<not> guaranteed to be ithread (or MULTIPLICITY-) safe |
1430 | plans to change this until Perl gets thread support (as opposed to the |
1704 | and there are no plans to change this. Note that perl's builtin so-called |
1431 | horribly slow so-called "threads" which are simply slow and bloated |
1705 | threads/ithreads are officially deprecated and should not be used. |
1432 | process simulations - use fork, it's I<much> faster, cheaper, better). |
|
|
1433 | |
|
|
1434 | (It might actually work, but you have been warned). |
|
|
1435 | |
1706 | |
1436 | |
1707 | |
1437 | =head1 THE PERILS OF SETLOCALE |
1708 | =head1 THE PERILS OF SETLOCALE |
1438 | |
1709 | |
1439 | Sometimes people avoid the Perl locale support and directly call the |
1710 | Sometimes people avoid the Perl locale support and directly call the |