1JSON::Parse(3)        User Contributed Perl Documentation       JSON::Parse(3)
2
3
4

NAME

6       JSON::Parse - Parse JSON
7

SYNOPSIS

9           use JSON::Parse 'parse_json';
10           my $json = '["golden", "fleece"]';
11           my $perl = parse_json ($json);
12           # Same effect as $perl = ['golden', 'fleece'];
13
14       Convert JSON into Perl.
15

VERSION

17       This documents version 0.60 of JSON::Parse corresponding to git commit
18       27b70e98176290ddd145cadfe8aa6ff43bb71703
19       <https://github.com/benkasminbullock/JSON-
20       Parse/commit/27b70e98176290ddd145cadfe8aa6ff43bb71703> released on Tue
21       Jan 26 08:51:47 2021 +0900.
22

DESCRIPTION

24       A module for parsing JSON. (JSON means "JavaScript Object Notation" and
25       it is specified in "RFC 8259".)
26
27       JSON::Parse offers the function "parse_json", which takes a string
28       containing JSON, and returns an equivalent Perl structure. It also
29       offers validation of JSON via "valid_json", which returns true or false
30       depending on whether the JSON is correct or not, and
31       "assert_valid_json", which produces a descriptive fatal error if the
32       JSON is invalid. A function "read_json" reads JSON from a file, and
33       there is a safer version of "parse_json" called "parse_json_safe" which
34       doesn't throw exceptions.
35
36       For special cases of parsing, there are also methods "new" and "parse",
37       which create a JSON parsing object and run it on text. See "METHODS".
38
39       JSON::Parse accepts only UTF-8 as input. See "UTF-8 only" and "Handling
40       of Unicode".
41

FUNCTIONS

43   assert_valid_json
44           use JSON::Parse 'assert_valid_json';
45           eval {
46               assert_valid_json ('["xyz":"b"]');
47           };
48           if ($@) {
49               print "Your JSON was invalid: $@\n";
50           }
51           # Prints "Unexpected character ':' parsing array"
52
53       produces output
54
55           Your JSON was invalid: JSON error at line 1, byte 7/11: Unexpected character ':' parsing array starting from byte 1: expecting whitespace: 'n', '\r', '\t', ' ' or comma: ',' or end of array: ']' at /usr/home/ben/projects/json-parse/examples/assert.pl line 6.
56
57       (This example is included as assert.pl
58       <https://fastapi.metacpan.org/source/BKB/JSON-
59       Parse-0.60/examples/assert.pl> in the distribution.)
60
61       This is the underlying function for "valid_json". It runs at the same
62       speed, but it throws an error if the JSON is wrong, rather than
63       returning 1 or 0. See "DIAGNOSTICS" for the error format, which is
64       identical to "parse_json".
65
66       This cannot detect key collisions in the JSON since it does not store
67       values. See "Key collisions" for more on this module's handling of non-
68       unique names in the JSON.
69
70       The method equivalent to this is "check".
71
72       The behaviour of disallowing empty inputs was changed in version 0.49.
73
74   parse_json
75           use JSON::Parse 'parse_json';
76           my $perl = parse_json ('{"x":1, "y":2}');
77
78       This function converts JSON into a Perl structure, either an array
79       reference, a hash reference, or a scalar.
80
81       If the first argument does not contain a complete valid JSON text, is
82       the undefined value, an empty string, or a string containing only
83       whitespace "parse_json" throws a fatal error ("dies").
84
85       If the argument contains valid JSON, the return value is either a hash
86       reference, an array reference, or a scalar. If the input JSON text is a
87       serialized object, a hash reference is returned:
88
89           use JSON::Parse ':all';
90           my $perl = parse_json ('{"a":1, "b":2}');
91           print ref $perl, "\n";
92
93       produces output
94
95           HASH
96
97       (This example is included as hash.pl
98       <https://fastapi.metacpan.org/source/BKB/JSON-
99       Parse-0.60/examples/hash.pl> in the distribution.)
100
101       If the input JSON text is a serialized array, an array reference is
102       returned:
103
104           use JSON::Parse ':all';
105           my $perl = parse_json ('["a", "b", "c"]');
106           print ref $perl, "\n";
107
108       produces output
109
110           ARRAY
111
112       (This example is included as array.pl
113       <https://fastapi.metacpan.org/source/BKB/JSON-
114       Parse-0.60/examples/array.pl> in the distribution.)
115
116       Otherwise a Perl scalar is returned.
117
118       The behaviour of allowing a scalar was added in version 0.32 of this
119       module. This brings it into line with the new specification for JSON.
120       The behaviour of disallowing empty inputs was changed in version 0.49.
121
122       The function "parse_json_safe" offers a version of this function with
123       various safety features enabled. The method "parse" is equivalent to
124       this.
125
126   parse_json_safe
127       This is almost the same thing as "parse_json", but has the following
128       differences:
129
130       Does not throw exceptions
131           If the JSON is invalid, a warning is printed and the undefined
132           value is returned, as if calling "parse_json" like this:
133
134               eval {
135                   $out = parse_json ($json);
136               };
137               if ($@) {
138                   carp $@;
139                   $out = undef;
140               }
141
142       Detects key collisions
143           This switches on "detect_collisions", so that if the JSON contains
144           non-unique names, a warning is printed and the undefined value is
145           returned. See "Key collisions" for an explanation of what a key
146           collision is.
147
148       Booleans are not read-only
149           This switches on "copy_literals" so that JSON true, false and null
150           values are copied. These values can be modified, but they will not
151           be converted back into "true" and "false" by JSON::Create.
152
153       Errors are reported by carp
154           Parsing errors are reported by "carp" in Carp, so the error line
155           number refers to the caller's line.
156
157       As the name implies, this is meant to be a "safety-first" version of
158       "parse_json".
159
160       ๐ŸŽฒ This function was added in version 0.38.
161
162   read_json
163           use JSON::Parse 'read_json';
164           my $p = read_json ('filename');
165
166       This is exactly the same as "parse_json" except that it reads the JSON
167       from the specified file rather than a scalar. The file must be in the
168       UTF-8 encoding, and is opened as a character file using
169       ":encoding(utf8)" (see PerlIO::encoding and perluniintro). The output
170       is marked as character strings.
171
172       The method equivalent is "read".
173
174       This is a convenience function written in Perl. You may prefer to read
175       the file yourself using another module if you need faster performance.
176
177       This was renamed from "json_file_to_perl" in version 0.59. The old name
178       will also continue to work indefinitely.
179
180   valid_json
181           use JSON::Parse 'valid_json';
182           if (valid_json ($json)) {
183               # do something
184           }
185
186       "valid_json" returns 1 if its argument is valid JSON and 0 if not. It
187       runs several times faster than "parse_json". This gain in speed is
188       obtained because it discards the input data after reading it, rather
189       than storing it into Perl variables.
190
191       This does not supply the actual errors which caused invalidity. Use
192       "assert_valid_json" to get error messages when the JSON is invalid.
193
194       This cannot detect duplicate keys in JSON objects because it does not
195       store values. See "Key collisions" for more on this module's handling
196       of non-unique names in the JSON.
197

METHODS

199       If you need to parse JSON and you are not satisfied with the parsing
200       options offered by "parse_json" and "parse_json_safe", you can create a
201       JSON parsing object with "new" and set various options on the object,
202       then use it with "parse" or "read".
203
204       There are options to copy JSON literals ("true", "false", "null") with
205       "copy_literals", switch off fatal errors with "warn_only", detect
206       duplicate keys in objects with "detect_collisions", set the maximum
207       depth of nested objects and arrays with "set_max_depth", produce
208       machine-readable parsing errors with "diagnostics_hash", and set the
209       JSON literals to user defined values with the methods described under
210       "Methods for manipulating literals".
211
212       These methods only affect the object created with "new"; they do not
213       globally affect the behaviour of "parse_json" or "parse_json_safe".
214
215   check
216           eval {
217               $jp->check ($json);
218           };
219
220       This does the same thing as "assert_valid_json", except its behaviour
221       can be modified using the "diagnostics_hash" method.
222
223       ๐ŸŽฒ This method was added in version 0.48. This is for the benefit of
224       JSON::Repair.
225
226   copy_literals
227           $jp->copy_literals (1);
228
229       With a true value, copy JSON literal values ("null", "true", and
230       "false") into new Perl scalar values, and don't put read-only values
231       into the output.
232
233       With a false value, use read-only scalars:
234
235           $jp->copy_literals (0);
236
237       The "copy_literals (1)" behaviour is the behaviour of
238       "parse_json_safe". The "copy_literals (0)" behaviour is the behaviour
239       of "parse_json".
240
241       If the user also sets user-defined literals with "set_true",
242       "set_false" and "set_null", that takes precedence over this.
243
244       ๐ŸŽฒ This method was added in version 0.38.
245
246   detect_collisions
247           $jp->detect_collisions (1);
248
249       This switches on a check for hash key collisions (non-unique names in
250       JSON objects). If a collision is found, an error message "Name is not
251       unique" is printed, which also gives the non-unique name and the byte
252       position where the start of the colliding string was found:
253
254           use JSON::Parse;
255           my $jp = JSON::Parse->new ();
256           $jp->detect_collisions (1);
257           eval {
258               $jp->parse ('{"animals":{"cat":"moggy","cat":"feline","cat":"neko"}}');
259           };
260           print "$@\n" if $@;
261
262       produces output
263
264           JSON error at line 1, byte 28/55: Name is not unique: "cat" parsing object starting from byte 12 at /usr/home/ben/projects/json-parse/examples/../blib/lib/JSON/Parse.pm line 131.
265
266       (This example is included as collide.pl
267       <https://fastapi.metacpan.org/source/BKB/JSON-
268       Parse-0.60/examples/collide.pl> in the distribution.)
269
270       The "detect_collisions (1)" behaviour is the behaviour of
271       "parse_json_safe". The "detect_collisions (0)" behaviour is the
272       behaviour of "parse_json".
273
274       ๐ŸŽฒ This method was added in version 0.38.
275
276   diagnostics_hash
277           $jp->diagnostics_hash (1);
278
279       This changes diagnostics produced by errors from a simple string into a
280       hash reference containing various fields. This is incompatible with
281       "warn_only".
282
283       This replaces the previous experimental global variable
284       $json_diagnostics, which was removed from the module. The hash keys and
285       values are identical to those provided in the object returned by
286       $json_diagnostics, with the addition of a key "error as string" which
287       returns the usual error.
288
289       This requires Perl version 5.14 or later.
290
291       An example of the use of this method to "repair" broken JSON is in the
292       module "JSON::Repair".
293
294       ๐ŸŽฒ This method was added in version 0.46.
295
296   get_max_depth
297          my $max_depth = $jp->get_max_depth ();
298
299       This returns the maximum nesting depth of objects or arrays in the
300       input JSON. The default value is 10,000.
301
302       ๐ŸŽฒ This method was added in version 0.58.
303
304   new
305           my $jp = JSON::Parse->new ();
306
307       Create a new JSON::Parse object.
308
309       ๐ŸŽฒ This method was added in version 0.38.
310
311   parse
312           my $out = $jp->parse ($json);
313
314       This does the same thing as "parse_json", except its behaviour can be
315       modified using the methods below.
316
317       ๐ŸŽฒ This method was added in version 0.38.
318
319       This was renamed from "run" in version 0.60.
320
321   read
322           my $json = $jp->read ($file);
323
324       Read a file, parse the contained JSON, and return the output. This
325       method is equivalent to the function "read_json".
326
327       ๐ŸŽฒ This method was added in version 0.60.
328
329   set_max_depth
330           $jp->set_max_depth (42);
331
332       Set the maximum nesting depth of objects or arrays in the input JSON.
333       The default value is 10,000.
334
335       ๐ŸŽฒ This method was added in version 0.58.
336
337   warn_only
338           $jp->warn_only (1);
339
340       Warn, don't die, on error. Failed parsing returns the undefined value,
341       "undef", and prints a warning.
342
343       This can be switched off again using any false value:
344
345           $jp->warn_only ('');
346
347       ๐ŸŽฒ This method was added in version 0.41.
348
349   Methods for manipulating literals
350       These methods alter what is written into the Perl structure when the
351       parser sees a literal value, "true", "false" or "null" in the input
352       JSON.
353
354       This number of methods is needed because of the possibility that a user
355       wants to set the output for "false" to be "undef":
356
357           $jp->set_false (undef);
358
359       Thus, we cannot use a single function "$jp->false (undef)" to cover
360       both setting and deleting of values.
361
362       ๐ŸŽฒ This facility was added in version 0.38.
363
364       set_true
365
366           $jp->set_true ("Yes, that is so true");
367
368       Supply a scalar to be used in place of the JSON "true" literal.
369
370       This example puts the string "Yes, that is so true" into the hash or
371       array when we hit a "true" literal, rather than the default read-only
372       scalar:
373
374           use JSON::Parse;
375           my $json = '{"yes":true,"no":false}';
376           my $jp = JSON::Parse->new ();
377           $jp->set_true ('Yes, that is so true');
378           my $out = $jp->parse ($json);
379           print $out->{yes}, "\n";
380
381       prints
382
383           Yes, that is so true
384
385       To override the previous value, call it again with a new value. To
386       delete the value and revert to the default behaviour, use
387       "delete_true".
388
389       If you give this a value which is not "true", as in Perl will evaluate
390       it as a false in an if statement, it prints a warning "User-defined
391       value for JSON true evaluates as false".  You can switch this warning
392       off with "no_warn_literals".
393
394       ๐ŸŽฒ This method was added in version 0.38.
395
396       delete_true
397
398           $jp->delete_true ();
399
400       Delete the user-defined true value. See "set_true".
401
402       This method is "safe" in that it has absolutely no effect if no user-
403       defined value is in place. It does not return a value.
404
405       ๐ŸŽฒ This method was added in version 0.38.
406
407       set_false
408
409           $jp->set_false (JSON::PP::Boolean::false);
410
411       Supply a scalar to be used in place of the JSON "false" literal.
412
413       In the above example, when we hit a "false" literal, we put
414       "JSON::PP::Boolean::false" in the output, similar to JSON::PP and other
415       CPAN modules like Mojo::JSON or JSON::XS.
416
417       To override the previous value, call it again with a new value. To
418       delete the value and revert to the default behaviour, use
419       "delete_false".
420
421       If you give this a value which is not "false", as in Perl will evaluate
422       it as a false in an if statement, it prints a warning "User-defined
423       value for JSON false evaluates as true".  You can switch this warning
424       off with "no_warn_literals".
425
426       ๐ŸŽฒ This method was added in version 0.38.
427
428       delete_false
429
430           $jp->delete_false ();
431
432       Delete the user-defined false value. See "set_false".
433
434       This method is "safe" in that it has absolutely no effect if no user-
435       defined value is in place. It does not return a value.
436
437       ๐ŸŽฒ This method was added in version 0.38.
438
439       set_null
440
441           $jp->set_null (0);
442
443       Supply a scalar to be used in place of the JSON "null" literal.
444
445       To override the previous value, call it again with a new value. To
446       delete the value and revert to the default behaviour, use
447       "delete_null".
448
449       ๐ŸŽฒ This method was added in version 0.38.
450
451       delete_null
452
453           $jp->delete_null ();
454
455       Delete the user-defined null value. See "set_null".
456
457       This method is "safe" in that it has absolutely no effect if no user-
458       defined value is in place. It does not return a value.
459
460       ๐ŸŽฒ This method was added in version 0.38.
461
462       no_warn_literals
463
464           $jp->no_warn_literals (1);
465
466       Use a true value to switch off warnings about setting boolean values to
467       contradictory things. For example if you want to set the JSON "false"
468       literal to turn into the string "false",
469
470           $jp->no_warn_literals (1);
471           $jp->set_false ("false");
472
473       See also "Contradictory values for "true" and "false"".
474
475       This also switches off the warning "User-defined value overrules
476       copy_literals".
477
478       ๐ŸŽฒ This method was added in version 0.38.
479

OLD INTERFACE

481       The following alternative function names are accepted. These are the
482       names used for the functions in old versions of this module. These
483       names are not deprecated and will never be removed from the module.
484
485       The names ending in "_to_perl" seem quite silly in retrospect since
486       surely it is obvious that one is programming in Perl.
487
488   json_to_perl
489       This is exactly the same function as "parse_json".
490
491   json_file_to_perl
492       This is exactly the same function as "read_json". The function was
493       renamed in version 0.59, after the same function in
494       "File::JSON::Slurper".
495
496   run
497       This is the old name for "parse".
498
499   validate_json
500       This is exactly the same function as "assert_valid_json".
501

Mapping from JSON to Perl

503       JSON elements are mapped to Perl as follows:
504
505   JSON numbers
506       JSON numbers become Perl numbers, either integers or double-precision
507       floating point numbers, or possibly strings containing the number if
508       parsing of a number by the usual methods fails somehow.
509
510       JSON does not allow leading zeros, like 0123, or leading plus signs,
511       like +100, in numbers, so these cause an "Unexpected character" error.
512       JSON also does not allow numbers of the form 1., but it does allow
513       things like 0e0 or 1E999999. As far as possible these are accepted by
514       JSON::Parse.
515
516   JSON strings
517       JSON strings become Perl strings. The JSON escape characters such as
518       "\t" for the tab character (see section 2.5 of "RFC 8259") are mapped
519       to the equivalent ASCII character.
520
521       Handling of Unicode
522
523       Inputs must be in the UTF-8 format. See "UTF-8 only".
524
525       In addition, JSON::Parse rejects UTF-8 which encodes non-characters
526       such as "U+FFFF" and ill-formed characters such as incomplete halves of
527       surrogate pairs.
528
529       Unicode encoding points in the input of the form "\u3000" are converted
530       into the equivalent UTF-8 bytes.
531
532       Surrogate pairs in the form "\uD834\uDD1E" are also handled. If the
533       second half of the surrogate pair is missing, an "Unexpected character"
534       or "Unexpected end of input" error is thrown. If the second half of the
535       surrogate pair is present but contains an impossible value, a "Not
536       surrogate pair" error is thrown.
537
538       If the input to "parse_json" is marked as Unicode characters, the
539       output strings will be marked as Unicode characters. If the input is
540       not marked as Unicode characters, the output strings will not be marked
541       as Unicode characters. Thus,
542
543           use JSON::Parse ':all';
544           # The scalar $sasori looks like Unicode to Perl
545           use utf8;
546           my $sasori = '["่ "]';
547           my $p = parse_json ($sasori);
548           print utf8::is_utf8 ($p->[0]);
549           # Prints 1.
550
551       but
552
553           use JSON::Parse ':all';
554           # The scalar $ebi does not look like Unicode to Perl
555           no utf8;
556           my $ebi = '["ๆตท่€"]';
557           my $p = parse_json ($ebi);
558           print utf8::is_utf8 ($p->[0]);
559           # Prints nothing.
560
561       Escapes of the form \uXXXX (see page three of "RFC 8259") are mapped to
562       ASCII if XXXX is less than 0x80, or to UTF-8 if XXXX is greater than or
563       equal to 0x80.
564
565       Strings containing \uXXXX escapes greater than 0x80 are also upgraded
566       to character strings, regardless of whether the input is a character
567       string or a byte string, thus regardless of whether Perl thinks the
568       input string is Unicode, escapes like \u87f9 are converted into the
569       equivalent UTF-8 bytes and the particular string in which they occur is
570       marked as a character string:
571
572           use JSON::Parse ':all';
573           no utf8;
574           # ่Ÿน
575           my $kani = '["\u87f9"]';
576           my $p = parse_json ($kani);
577           print "It's marked as a character string" if utf8::is_utf8 ($p->[0]);
578           # Prints "It's marked as a character string" because it's upgraded
579           # regardless of the input string's flags.
580
581       This is modelled on the behaviour of Perl's "chr":
582
583           no utf8;
584           my $kani = '87f9';
585           print "hex is character string\n" if utf8::is_utf8 ($kani);
586           # prints nothing
587           $kani = chr (hex ($kani));
588           print "chr makes it a character string\n" if utf8::is_utf8 ($kani);
589           # prints "chr makes it a character string"
590
591       However, JSON::Parse also upgrades the remaining part of the string
592       into a character string, even when it's not marked as a character
593       string. For example,
594
595           use JSON::Parse ':all';
596           use Unicode::UTF8 'decode_utf8';
597           no utf8;
598           my $highbytes = "ใ‹";
599           my $not_utf8 = "$highbytes\\u3042";
600           my $test = "{\"a\":\"$not_utf8\"}";
601           my $out = parse_json ($test);
602           # JSON::Parse does something unusual here in promoting the first part
603           # of the string into UTF-8.
604           print "JSON::Parse gives this: ", $out->{a}, "\n";
605           # Perl cannot assume that $highbytes is in UTF-8, so it has to just
606           # turn the initial characters into garbage.
607           my $add_chr = $highbytes . chr (0x3042);
608           print "Perl's output is like this: ", $add_chr, "\n";
609           # In fact JSON::Parse's behaviour is equivalent to this:
610           my $equiv = decode_utf8 ($highbytes) . chr (0x3042);
611           print "JSON::Parse did something like this: ", $equiv, "\n";
612           # With character strings switched on, Perl and JSON::Parse do the same
613           # thing.
614           use utf8;
615           my $is_utf8 = "ใ‹";
616           my $test2 = "{\"a\":\"$is_utf8\\u3042\"}";
617           my $out2 = parse_json ($test2);
618           print "JSON::Parse: ", $out2->{a}, "\n";
619           my $add_chr2 = $is_utf8 . chr (0x3042);
620           print "Native Perl: ", $add_chr2, "\n";
621
622       produces output
623
624           JSON::Parse gives this: ใ‹ใ‚
625           Perl's output is like this: รฃยย‹ใ‚
626           JSON::Parse did something like this: ใ‹ใ‚
627           JSON::Parse: ใ‹ใ‚
628           Native Perl: ใ‹ใ‚
629
630       (This example is included as unicode-details.pl
631       <https://fastapi.metacpan.org/source/BKB/JSON-
632       Parse-0.60/examples/unicode-details.pl> in the distribution.)
633
634       Although in general the above would be an unsafe practice, JSON::Parse
635       can do things this way because JSON is a text-only, Unicode-only
636       format. To ensure that invalid inputs are never upgraded, JSON::Parse
637       checks each input byte to make sure that it forms UTF-8. See also
638       "UTF-8 only". Doing things this way, rather than the way that Perl does
639       it, was one of the original motivations for writing this module.
640
641   JSON arrays
642       JSON arrays become Perl array references. The elements of the Perl
643       array are in the same order as they appear in the JSON.
644
645       Thus
646
647           my $p = parse_json ('["monday", "tuesday", "wednesday"]');
648
649       has the same result as a Perl declaration of the form
650
651           my $p = [ 'monday', 'tuesday', 'wednesday' ];
652
653   JSON objects
654       JSON objects become Perl hashes. The members of the JSON object become
655       key and value pairs in the Perl hash. The string part of each object
656       member becomes the key of the Perl hash. The value part of each member
657       is mapped to the value of the Perl hash.
658
659       Thus
660
661           my $j = <<EOF;
662           {"monday":["blue", "black"],
663            "tuesday":["grey", "heart attack"],
664            "friday":"Gotta get down on Friday"}
665           EOF
666
667           my $p = parse_json ($j);
668
669       has the same result as a Perl declaration of the form
670
671           my $p = {
672               monday => ['blue', 'black'],
673               tuesday => ['grey', 'heart attack'],
674               friday => 'Gotta get down on Friday',
675           };
676
677       Key collisions
678
679       A key collision is something like the following.
680
681           use JSON::Parse qw/parse_json parse_json_safe/;
682           my $j = '{"a":1, "a":2}';
683           my $p = parse_json ($j);
684           print "Ambiguous key 'a' is ", $p->{a}, "\n";
685           my $q = parse_json_safe ($j);
686
687       produces output
688
689           JSON::Parse::parse_json_safe: Name is not unique: "a" parsing object starting from byte 1 at /usr/home/ben/projects/json-parse/examples/key-collision.pl line 8.
690           Ambiguous key 'a' is 2
691
692       (This example is included as key-collision.pl
693       <https://fastapi.metacpan.org/source/BKB/JSON-Parse-0.60/examples/key-
694       collision.pl> in the distribution.)
695
696       Here the key "a" could be either 1 or 2. As seen in the example,
697       "parse_json" overwrites the first value with the second value.
698       "parse_json_safe" halts and prints a warning. If you use "new" you can
699       switch key collision on and off with the "detect_collisions" method.
700
701       The rationale for "parse_json" not to give warnings is that Perl
702       doesn't give information about collisions when storing into hash
703       values, and checking for collisions for every key will degrade
704       performance for the sake of an unlikely occurrence. The JSON
705       specification says "The names within an object SHOULD be unique." (see
706       "RFC 8259", page 5), although it's not a requirement.
707
708       For performance, "valid_json" and "assert_valid_json" do not store hash
709       keys, thus they cannot detect this variety of problem.
710
711   Literals
712       false
713
714       "parse_json" maps the JSON false literal to a readonly scalar which
715       evaluates to the empty string, or to zero in a numeric context. (This
716       behaviour changed from version 0.36 to 0.37. In versions up to 0.36,
717       the false literal was mapped to a readonly scalar which evaluated to 0
718       only.) "parse_json_safe" maps the JSON literal to a similar scalar
719       without the readonly constraints. If you use a parser created with
720       "new", you can choose either of these behaviours with "copy_literals",
721       or you can tell JSON::Parse to put your own value in place of falses
722       using the "set_false" method.
723
724       null
725
726       "parse_json" maps the JSON null literal to a readonly scalar
727       $JSON::Parse::null which evaluates to "undef". "parse_json_safe" maps
728       the JSON literal to the undefined value. If you use a parser created
729       with "new", you can choose either of these behaviours with
730       "copy_literals", or you can tell JSON::Parse to put your own value in
731       place of nulls using the "set_null" method.
732
733       true
734
735       "parse_json" maps the JSON true literal to a readonly scalar which
736       evaluates to 1. "parse_json_safe" maps the JSON literal to the value 1.
737       If you use a parser created with "new", you can choose either of these
738       behaviours with "copy_literals", or you can tell JSON::Parse to put
739       your own value in place of trues using the "set_true" method.
740
741       Round trips and compatibility
742
743       The Perl versions of literals produced by "parse_json" will be
744       converted back to JSON literals if you use "create_json" in
745       JSON::Create. However, JSON::Parse's literals are incompatible with the
746       other CPAN JSON modules. For compatibility with other CPAN modules,
747       create a JSON::Parse object with "new", and set JSON::Parse's literals
748       with "set_true", "set_false", and "set_null".
749
750       A round trip with JSON::Tiny
751
752       This example demonstrates round-trip compatibility using JSON::Tiny,
753       version 0.58:
754
755           use utf8;
756           use JSON::Tiny '0.58', qw(decode_json encode_json);
757           use JSON::Parse;
758           use JSON::Create;
759           my $cream = '{"clapton":true,"hendrix":false}';
760           my $jp = JSON::Parse->new ();
761           my $jc = JSON::Create->new (sort => 1);
762
763           print "First do a round-trip of our modules:\n\n";
764           print $jc->create ($jp->parse ($cream)), "\n\n";
765
766           print "Now do a round-trip of JSON::Tiny:\n\n";
767           print encode_json (decode_json ($cream)), "\n\n";
768
769           print "๐Ÿฅด First, incompatible mode:\n\n";
770           print 'tiny(parse): ', encode_json ($jp->parse ($cream)), "\n";
771           print 'create(tiny): ', $jc->create (decode_json ($cream)), "\n\n";
772
773           # Set our parser to produce these things as literals:
774           $jp->set_true (JSON::Tiny::true);
775           $jp->set_false (JSON::Tiny::false);
776
777           print "๐Ÿ”„ Compatibility with JSON::Parse:\n\n";
778           print 'tiny(parse):', encode_json ($jp->parse ($cream)), "\n\n";
779           $jc->bool ('JSON::Tiny::_Bool');
780
781           print "๐Ÿ”„ Compatibility with JSON::Create:\n\n";
782           print 'create(tiny):', $jc->create (decode_json ($cream)), "\n\n";
783
784           print "๐Ÿ”„ JSON::Parse and JSON::Create are still compatible too:\n\n";
785           print $jc->create ($jp->parse ($cream)), "\n";
786
787       produces output
788
789           First do a round-trip of our modules:
790
791           {"clapton":true,"hendrix":false}
792
793           Now do a round-trip of JSON::Tiny:
794
795           {"clapton":true,"hendrix":false}
796
797           ๐Ÿฅด First, incompatible mode:
798
799           tiny(parse): {"clapton":1,"hendrix":""}
800           create(tiny): {"clapton":1,"hendrix":0}
801
802           ๐Ÿ”„ Compatibility with JSON::Parse:
803
804           tiny(parse):{"clapton":true,"hendrix":false}
805
806           ๐Ÿ”„ Compatibility with JSON::Create:
807
808           create(tiny):{"clapton":true,"hendrix":false}
809
810           ๐Ÿ”„ JSON::Parse and JSON::Create are still compatible too:
811
812           {"clapton":true,"hendrix":false}
813
814       (This example is included as json-tiny-round-trip-demo.pl
815       <https://fastapi.metacpan.org/source/BKB/JSON-Parse-0.60/examples/json-
816       tiny-round-trip-demo.pl> in the distribution.)
817
818       Most of the other CPAN modules use similar methods to JSON::Tiny, so
819       the above example can easily be adapted. See also "Interoperability" in
820       JSON::Create for various examples.
821
822       Modifying the values
823
824       "parse_json" maps all the literals to read-only values. Because of
825       this, attempting to modifying the boolean values in the hash reference
826       returned by "parse_json" will cause "Modification of a read-only value
827       attempted" errors:
828
829           my $in = '{"hocus":true,"pocus":false,"focus":null}';
830           my $p = json_parse ($in);
831           $p->{hocus} = 99;
832           # "Modification of a read-only value attempted" error occurs
833
834       Since the hash values are read-only scalars, "$p->{hocus} = 99" is like
835       this:
836
837           undef = 99;
838
839       If you need to modify the returned hash reference, then delete the
840       value first:
841
842           my $in = '{"hocus":true,"pocus":false,"focus":null}';
843           my $p = json_parse ($in);
844           delete $p->{pocus};
845           $p->{pocus} = 99;
846           # OK
847
848       Similarly with array references, delete the value before altering:
849
850           my $in = '[true,false,null]';
851           my $q = json_parse ($in);
852           delete $q->[1];
853           $q->[1] = 'magic';
854
855       Note that the return values from parsing bare literals are not read-
856       only scalars, so
857
858           my $true = JSON::Parse::json_parse ('true');
859           $true = 99;
860
861       produces no error. This is because Perl copies the scalar.
862

RESTRICTIONS

864       This module imposes the following restrictions on its input.
865
866       JSON only
867           JSON::Parse is a strict parser. It only accepts input which exactly
868           meets the criteria of "RFC 8259". That means, for example,
869           JSON::Parse does not accept single quotes (') instead of double
870           quotes ("), or numbers with leading zeros, like 0123. JSON::Parse
871           does not accept control characters (0x00 - 0x1F) in strings,
872           missing commas between array or hash elements like "["a" "b"]", or
873           trailing commas like "["a","b","c",]". It also does not accept
874           trailing non-whitespace, like the second "]" in "["a"]]".
875
876           You may find "JSON::Repair" by the same authors as JSON::Parse
877           useful if you need to process JSON-like text with tolerance for
878           errors.
879
880       No incremental parsing
881           JSON::Parse does not parse incrementally. It only parses fully-
882           formed JSON strings which include all opening and closing brackets.
883           This is an inherent part of the design of the module. Incremental
884           parsing in the style of XML::Parser would require some kind of
885           callback structure to deal with the elements of the partially
886           digested structures, but JSON::Parse was never designed to do this;
887           it merely converts what it sees into a Perl structure. Claims to
888           offer incremental JSON parsing in other modules' documentation
889           should be diligently verified.
890
891       UTF-8 only
892           JSON::Parse only parses the UTF-8 format. If input is in a
893           different Unicode encoding than UTF-8, convert the input before
894           handing it to this module. For example, for the UTF-16 format,
895
896               use Encode 'decode';
897               my $input_utf8 = decode ('UTF-16', $input);
898               my $perl = parse_json ($input_utf8);
899
900           or, for a file, use ":encoding" (see PerlIO::encoding and
901           perluniintro):
902
903               open my $input, "<:encoding(UTF-16)", 'some-json-file';
904
905           JSON::Parse does not try to determine the nature of the octet
906           stream using BOM markers. A BOM marker in the input consists of
907           bytes 0xFE and 0xFF, both of which are invalid as UTF-8, and thus
908           will cause a fatal error.
909
910           This restriction to UTF-8 applies regardless of whether Perl thinks
911           that the input string is a character string or a byte string.
912           Non-UTF-8 input will cause an "Unexpected character" error.
913
914           The latest specification for JSON, "RFC 8259", specifies it to be a
915           UTF-8 only format.
916
917           JSON::Parse does not accept Unicode non-characters (U+FFFF, UFDDO,
918           etc.), UTF-8 representing surrogate pair code points, or bytes
919           outside the range of Unicode code points as UTF-8 bytes.
920

DIAGNOSTICS

922       "valid_json" does not produce error messages. "parse_json" and
923       "assert_valid_json" die on encountering invalid input.
924       "parse_json_safe" uses "carp" in Carp to pass error messages as
925       warnings.
926
927       Error messages have the line number, and the byte number where
928       appropriate, of the input which caused the problem. The line number is
929       formed simply by counting the number of "\n" (linefeed, ASCII 0x0A)
930       characters in the whitespace part of the JSON.
931
932       In "parse_json" and "assert_valid_json", parsing errors are fatal, so
933       to continue after an error occurs, put the parsing into an "eval"
934       block:
935
936           my $p;
937           eval {
938               $p = parse_json ($j);
939           };
940           if ($@) {
941               # handle error
942           }
943
944       The following error messages are produced:
945
946       Unexpected character
947           An unexpected character (byte) was encountered in the input. For
948           example, when looking at the beginning of a string supposedly
949           containing JSON, if the module encounters a plus sign, it will give
950           an error like this:
951
952               assert_valid_json ('+');
953
954           gives output
955
956               JSON error at line 1, byte 1/1: Unexpected character '+' parsing initial state: expecting whitespace: 'n', '\r', '\t', ' ' or start of string: '"' or digit: '0-9' or minus: '-' or start of an array or object: '{', '[' or start of literal: 't', 'f', 'n'
957
958           The message always includes a list of what characters are allowed.
959
960           If there is some recognizable structure being parsed, the error
961           message will include its starting point in the form "starting from
962           byte n":
963
964               assert_valid_json ('{"this":"\a"}');
965
966           gives output
967
968               JSON error at line 1, byte 11/13: Unexpected character 'a' parsing string starting from byte 9: expecting escape: '', '/', '"', 'b', 'f', 'n', 'r', 't', 'u'
969
970           A feature of JSON is that parsing it requires only one byte to be
971           examined at a time. Thus almost all parsing problems can be handled
972           using the "Unexpected character" error type, including spelling
973           errors in literals:
974
975               assert_valid_json ('[true,folse]');
976
977           gives output
978
979               JSON error at line 1, byte 8/12: Unexpected character 'o' parsing literal starting from byte 7: expecting 'a'
980
981           and the missing second half of a surrogate pair:
982
983               assert_valid_json ('["\udc00? <-- should be a second half here"]');
984
985           gives output
986
987               JSON error at line 1, byte 9/44: Unexpected character '?' parsing unicode escape starting from byte 3: expecting '\'
988
989           All kinds of errors can occur parsing numbers, for example a
990           missing fraction,
991
992               assert_valid_json ('[1.e9]');
993
994           gives output
995
996               JSON error at line 1, byte 4/6: Unexpected character 'e' parsing number starting from byte 2: expecting digit: '0-9'
997
998           and a leading zero,
999
1000               assert_valid_json ('[0123]');
1001
1002           gives output
1003
1004               JSON error at line 1, byte 3/6: Unexpected character '1' parsing number starting from byte 2: expecting whitespace: 'n', '\r', '\t', ' ' or comma: ',' or end of array: ']' or dot: '.' or exponential sign: 'e', 'E'
1005
1006           The error message is this complicated because all of the following
1007           are valid here: whitespace: "[0 ]"; comma: "[0,1]", end of array:
1008           "[0]", dot: "[0.1]", or exponential: "[0e0]".
1009
1010           These are all handled by this error.  Thus the error messages are a
1011           little confusing as diagnostics.
1012
1013           Versions of this module prior to 0.29 gave more informative
1014           messages like "leading zero in number". (The messages weren't
1015           documented.) The reason to change over to the single message was
1016           because it makes the parsing code simpler, and because the testing
1017           code described in "TESTING" makes use of the internals of this
1018           error to check that the error message produced actually do
1019           correspond to the invalid and valid bytes allowed by the parser, at
1020           the exact byte given.
1021
1022           This is a bytewise error, thus for example if a miscoded UTF-8
1023           appears in the input, an error message saying what bytes would be
1024           valid at that point will be printed.
1025
1026               no utf8;
1027               use JSON::Parse 'assert_valid_json';
1028
1029               # Error in first byte:
1030
1031               my $bad_utf8_1 = chr (hex ("81"));
1032               eval { assert_valid_json ("[\"$bad_utf8_1\"]"); };
1033               print "$@\n";
1034
1035               # Error in third byte:
1036
1037               my $bad_utf8_2 = chr (hex ('e2')) . chr (hex ('9C')) . 'b';
1038               eval { assert_valid_json ("[\"$bad_utf8_2\"]"); };
1039               print "$@\n";
1040
1041           prints
1042
1043               JSON error at line 1, byte 3/5: Unexpected character 0x81 parsing string starting from byte 2: expecting printable ASCII or first byte of UTF-8: '\x20-\x7f', '\xC2-\xF4' at examples/bad-utf8.pl line 10.
1044
1045               JSON error at line 1, byte 5/7: Unexpected character 'b' parsing string starting from byte 2: expecting bytes in range 80-bf: '\x80-\xbf' at examples/bad-utf8.pl line 16.
1046
1047       Unexpected end of input
1048           The end of the string was encountered before the end of whatever
1049           was being parsed was. For example, if a quote is missing from the
1050           end of the string, it will give an error like this:
1051
1052               assert_valid_json ('{"first":"Suzuki","second":"Murakami","third":"Asada}');
1053
1054           gives output
1055
1056               JSON error at line 1: Unexpected end of input parsing string starting from byte 47
1057
1058       Not surrogate pair
1059           While parsing a string, a surrogate pair was encountered. While
1060           trying to turn this into UTF-8, the second half of the surrogate
1061           pair turned out to be an invalid value.
1062
1063               assert_valid_json ('["\uDC00\uABCD"]');
1064
1065           gives output
1066
1067               JSON error at line 1: Not surrogate pair parsing unicode escape starting from byte 11
1068
1069       Empty input
1070           This error occurs for an input which is an empty (no length or
1071           whitespace only) or an undefined value.
1072
1073               assert_valid_json ('');
1074
1075           gives output
1076
1077               JSON error: Empty input parsing initial state
1078
1079           Prior to version 0.49, this error was produced by
1080           "assert_valid_json" only, but it is now also produced by
1081           "parse_json".
1082
1083       Name is not unique
1084           This error occurs when parsing JSON when the user has chosen
1085           "detect_collisions". For example an input like
1086
1087               my $p = JSON::Parse->new ();
1088               $p->detect_collisions (1);
1089               $p->run ('{"hocus":1,"pocus":2,"hocus":3}');
1090
1091           gives output
1092
1093               JSON error at line 1, byte 23/31: Name is not unique: "hocus" parsing object starting from byte 1 at blib/lib/JSON/Parse.pm line 131.
1094
1095           where the JSON object has two keys with the same name, "hocus". The
1096           terminology "name is not unique" is from the JSON specification.
1097
1098       Contradictory values for "true" and "false"
1099           User-defined value for JSON false evaluates as true
1100               This happens if you set JSON false to map to a true value:
1101
1102                   $jp->set_false (1);
1103
1104               To switch off this warning, use "no_warn_literals".
1105
1106               ๐ŸŽฒ This warning was added in version 0.38.
1107
1108           User-defined value for JSON true evaluates as false
1109               This happens if you set JSON true to map to a false value:
1110
1111                   $jp->set_true (undef);
1112
1113               To switch off this warning, use "no_warn_literals".
1114
1115               ๐ŸŽฒ This warning was added in version 0.38.
1116
1117           User-defined value overrules copy_literals
1118               This warning is given if you set up literals with
1119               "copy_literals" then you also set up your own true, false, or
1120               null values with "set_true", "set_false", or "set_null".
1121
1122               ๐ŸŽฒ This warning was added in version 0.38.
1123

PERFORMANCE

1125       On the author's computer, the module's speed of parsing is
1126       approximately the same as JSON::XS, with small variations depending on
1127       the type of input. For validation, "valid_json" is faster than any
1128       other module known to the author, and up to ten times faster than
1129       JSON::XS.
1130
1131       Some special types of input, such as floating point numbers containing
1132       an exponential part, like "1e09", seem to be about two or three times
1133       faster to parse with this module than with JSON::XS. In JSON::Parse,
1134       parsing of exponentials is done by the system's "strtod" function, but
1135       JSON::XS contains its own parser for exponentials, so these results may
1136       be system-dependent.
1137
1138       At the moment the main place JSON::XS wins over JSON::Parse is in
1139       strings containing escape characters, where JSON::XS is about 10%
1140       faster on the module author's computer and compiler. As of version
1141       0.33, despite some progress in improving JSON::Parse, I haven't been
1142       able to fully work out the reason behind the better speed.
1143
1144       There is some benchmarking code in the github repository under the
1145       directory "benchmarks" for those wishing to test these claims. The
1146       script benchmarks/bench <https://github.com/benkasminbullock/JSON-
1147       Parse/27b70e98176290ddd145cadfe8aa6ff43bb71703/benchmarks/bench> is an
1148       adaptation of the similar script in the JSON::XS distribution. The
1149       script benchmarks/pub-bench.pl
1150       <https://github.com/benkasminbullock/JSON-
1151       Parse/27b70e98176290ddd145cadfe8aa6ff43bb71703/benchmarks/pub-bench.pl>
1152       runs the benchmarks and prints them out as POD.
1153
1154       The following benchmark tests used version 0.58_01 of JSON::Parse,
1155       version 4.03 of "JSON::XS", and version 4.25 of "Cpanel::JSON::XS" on
1156       Perl version v5.32.0 compiled with Clang version FreeBSD clang version
1157       10.0.1 on FreeBSD 12.2. The files in the "benchmarks" directory of
1158       JSON::Parse. short.json and long.json are the benchmarks used by
1159       "JSON::XS".
1160
1161       short.json
1162               Repetitions: 10 x 100 = 1000
1163               --------------+------------+------------+
1164               module        |      1/min |        min |
1165               --------------|------------|------------|
1166               Cpanel        | 313007.761 |  0.0000319 |
1167               JP::valid     | 838860.800 |  0.0000119 |
1168               JSON::Parse   | 310689.185 |  0.0000322 |
1169               JSON::XS      | 303935.072 |  0.0000329 |
1170               --------------+------------+------------+
1171
1172       long.json
1173               Repetitions: 10 x 100 = 1000
1174               --------------+------------+------------+
1175               module        |      1/min |        min |
1176               --------------|------------|------------|
1177               Cpanel        |   5611.860 |  0.0017819 |
1178               JP::valid     |  13586.991 |  0.0007360 |
1179               JSON::Parse   |   4924.048 |  0.0020308 |
1180               JSON::XS      |   6406.452 |  0.0015609 |
1181               --------------+------------+------------+
1182
1183       words-array.json
1184               Repetitions: 10 x 100 = 1000
1185               --------------+------------+------------+
1186               module        |      1/min |        min |
1187               --------------|------------|------------|
1188               Cpanel        |  34749.826 |  0.0002878 |
1189               JP::valid     | 270600.258 |  0.0000370 |
1190               JSON::Parse   |  34017.064 |  0.0002940 |
1191               JSON::XS      |  35726.610 |  0.0002799 |
1192               --------------+------------+------------+
1193
1194       exp.json
1195               Repetitions: 10 x 100 = 1000
1196               --------------+------------+------------+
1197               module        |      1/min |        min |
1198               --------------|------------|------------|
1199               Cpanel        |  46759.242 |  0.0002139 |
1200               JP::valid     | 117817.528 |  0.0000849 |
1201               JSON::Parse   |  46759.242 |  0.0002139 |
1202               JSON::XS      |  19195.899 |  0.0005209 |
1203               --------------+------------+------------+
1204
1205       literals.json
1206               Repetitions: 10 x 100 = 1000
1207               --------------+------------+------------+
1208               module        |      1/min |        min |
1209               --------------|------------|------------|
1210               Cpanel        |  33026.016 |  0.0003028 |
1211               JP::valid     | 384798.532 |  0.0000260 |
1212               JSON::Parse   |  40840.351 |  0.0002449 |
1213               JSON::XS      |  33689.189 |  0.0002968 |
1214               --------------+------------+------------+
1215
1216       cpantesters.json
1217               Repetitions: 10 x 100 = 1000
1218               --------------+------------+------------+
1219               module        |      1/min |        min |
1220               --------------|------------|------------|
1221               Cpanel        |    212.377 |  0.0470860 |
1222               JP::valid     |   1309.043 |  0.0076392 |
1223               JSON::Parse   |    207.491 |  0.0481949 |
1224               JSON::XS      |    226.439 |  0.0441620 |
1225               --------------+------------+------------+
1226

SEE ALSO

1228       RFC 8259
1229           JSON is specified in RFC 8259 "The JavaScript Object Notation
1230           (JSON) Data Interchange Format"
1231           <http://www.ietf.org/rfc/rfc8259.txt>.
1232
1233       json.org
1234           <https://json.org> is the website for JSON, authored by Douglas
1235           Crockford.
1236
1237   Other CPAN modules for parsing and producing JSON
1238       The โญ represents the number of votes this module has received on
1239       metacpan, on a logarithmic scale. Modules which we recommend are marked
1240       with ๐Ÿ‘. Deprecated modules and modules which are definitely buggy (bug
1241       reports/pull requests ignored) and abandoned (no releases for several
1242       years) are marked with ๐Ÿ‘Ž and/or ๐Ÿ›. Modules we can't work out are
1243       marked with ๐Ÿ˜•.
1244
1245       Modules by the same author
1246           JSON::Create
1247               ๐Ÿ‘ JSON::Create is a companion module to JSON::Parse by the
1248               same author.
1249
1250           JSON::Repair
1251               JSON::Repair is an example module which demonstrates using
1252               JSON::Parse to apply some kinds of heuristics to repair
1253               "relaxed JSON" or otherwise broken JSON into compliant JSON.
1254
1255           JSON::Server
1256               JSON::Server is a module which offers a JSON-only, UTF-8 only
1257               server using "JSON::Parse" and "JSON::Create".
1258
1259           JSON::Tokenize
1260               JSON::Tokenize is part of the JSON::Parse distribution, a
1261               tokenizer which reduces a JSON string to tokens. This makes the
1262               JSON::Parse tokenizer available to people who want to write
1263               their own JSON parsers.
1264
1265           JSON::Whitespace
1266               JSON::Whitespace is for stripping whitespace from JSON.
1267
1268       Reading and writing JSON
1269           Cpanel::JSON::XS
1270               [โญโญ Author: RURBAN <https://metacpan.org/author/RURBAN>;
1271               Date: "2020-10-28"; Version: 4.25]
1272
1273               This is a fork of JSON::XS. Please see the module for details
1274               about the reasons for the fork.
1275
1276           File::JSON::Slurper
1277               [โญ Author: NEILB <https://metacpan.org/author/NEILB>; Date:
1278               "2020-11-18"; Version: 1.00]
1279
1280               Slurp a JSON file into a data structure, and the reverse. It
1281               relies on "JSON::MaybeXS".
1282
1283           Glib::JSON
1284               [โญ Author: EBASSI <https://metacpan.org/author/EBASSI>; Date:
1285               "2015-04-19"; Version: 0.002]
1286
1287               Uses the JSON library from Glib, a library of C functions for
1288               the Linux GNOME desktop project, so it is independent of the
1289               other CPAN modules. Judging from the fairly sparse
1290               documentation, it seems to be a module where you build the JSON
1291               on the fly rather than converting a Perl structure wholesale
1292               into JSON.
1293
1294           JSON
1295               [โญโญ Author: ISHIGAKI <https://metacpan.org/author/ISHIGAKI>;
1296               Date: "2021-01-24"; Version: 4.03]
1297
1298               This calls on either JSON::PP or JSON::XS.
1299
1300           JSON::DWIW
1301               [Author: DOWENS <https://metacpan.org/author/DOWENS>; Date:
1302               "2010-09-29"; Version: 0.47]
1303
1304               ๐Ÿ‘Ž๐Ÿ› This module "Does What I Want", where "I" refers to the
1305               module's author. Development seems to have ceased in 2010,
1306               there is a long list of unfixed bugs, and some of the module's
1307               features seem to predate Unicode support in Perl. It is written
1308               in XS, and it claims to accept a wide variety of non-JSON
1309               formats such as comments, single-quoted strings, trailing
1310               commas, etc.
1311
1312           JSON::PP
1313               [โญโญ Author: ISHIGAKI <https://metacpan.org/author/ISHIGAKI>;
1314               Date: "2021-01-23"; Version: 4.06]
1315
1316               This is part of the Perl core, installed when you install Perl.
1317               "PP" stands for "Pure Perl", which means it is in Perl-only
1318               without the XS (C-based) parsing. This is slower but may be
1319               necessary if you cannot install modules requiring a C compiler.
1320
1321           JSON::Slurper
1322               [โญ Author: SRCHULO <https://metacpan.org/author/SRCHULO>;
1323               Date: "2019-10-30"; Version: 0.12]
1324
1325               Convenient file slurping and spurting of data using JSON. Uses
1326               "JSON::PP" or "Cpanel::JSON::XS" if available. The basic idea
1327               seems to be that it uses context to return arrays or hashes as
1328               required, and read and write files without extra stages of
1329               opening and closing the file.
1330
1331           JSON::Syck
1332               [โญโญ Author: TODDR <https://metacpan.org/author/TODDR>; Date:
1333               "2020-10-26"; Version: 1.34]
1334
1335               ๐Ÿ‘Ž๐Ÿ› Takes advantage of a similarity between YAML (yet another
1336               markup language) and JSON to provide a JSON parser/producer
1337               using YAML::Syck.
1338
1339               We have never tried this module, but it seems to be semi-
1340               deprecated (the ABSTRACT says "consider using JSON::XS
1341               instead!")  and there are a lot of bug reports
1342               <https://github.com/toddr/YAML-Syck/issues> about things like
1343               failing to process equals signs. However, the maintainer is
1344               fixing some of the bugs and making new releases, so we're not
1345               really sure.
1346
1347           JSON::Tiny
1348               [โญโญ Author: DAVIDO <https://metacpan.org/author/DAVIDO>;
1349               Date: "2017-11-12"; Version: 0.58]
1350
1351               This is a fork of "Mojo::JSON".
1352
1353           JSON::XS
1354               [โญโญโญ Author: MLEHMANN
1355               <https://metacpan.org/author/MLEHMANN>; Date: "2020-10-27";
1356               Version: 4.03]
1357
1358               This is an all-purpose JSON module in XS, which means it
1359               requires a C compiler to install.
1360
1361           JSON::YAJL
1362               [โญ Author: LBROCARD <https://metacpan.org/author/LBROCARD>;
1363               Date: "2011-08-05"; Version: 0.10]
1364
1365               ๐Ÿ‘Ž๐Ÿ› Wraps a C library called yajl. The module has been
1366               abandoned since ten years ago. Bug reports include serious
1367               errors, and pull requests have been ignored.
1368
1369           Mojo::JSON
1370               [โญโญโญ Author: SRI <https://metacpan.org/author/SRI>; Date:
1371               "2021-01-17"; Version: 8.71]
1372
1373               Part of the Mojolicious standalone web framework, "pure Perl"
1374               JSON reader/writer. As of version 8.70 of Mojolicious, this
1375               actually depends on "JSON::PP" but will load "Cpanel::JSON::XS"
1376               if it is available.
1377
1378       Combination modules
1379           These modules rely on more than one back-end module to process JSON
1380           for you.
1381
1382           JSON::Any
1383               [โญ Author: ETHER <https://metacpan.org/author/ETHER>; Date:
1384               "2015-06-10"; Version: 1.39]
1385
1386               ๐Ÿ‘Ž This now-deprecated module combines "JSON::DWIW", "JSON::XS"
1387               versions one and two, and "JSON::Syck".
1388
1389           JSON::MaybeXS
1390               [โญโญ Author: ETHER <https://metacpan.org/author/ETHER>; Date:
1391               "2020-11-13"; Version: 1.004003]
1392
1393               A module which combines "Cpanel::JSON::XS", "JSON::XS", and
1394               "JSON::PP". The original "JSON" combines "JSON::XS" and
1395               "JSON::PP", but this prioritizes "Cpanel::JSON::XS" over
1396               "JSON::XS".
1397
1398           JSON::XS::VersionOneAndTwo
1399               [Author: LBROCARD <https://metacpan.org/author/LBROCARD>; Date:
1400               "2008-02-13"; Version: 0.31]
1401
1402               ๐Ÿ‘Ž A "combination module" which supports two different
1403               interfaces of "JSON::XS". However, JSON::XS is now onto version
1404               4.
1405
1406           Mojo::JSON::MaybeXS
1407               [โญ Author: DBOOK <https://metacpan.org/author/DBOOK>; Date:
1408               "2019-08-07"; Version: 1.002]
1409
1410               ๐Ÿ‘Ž This pulls in "JSON::MaybeXS" instead of "Mojo::JSON" for
1411               Mojolicious users. It seems to have been rendered obsolete by
1412               modern versions of Mojolicious due to changes to make that
1413               depend on "Cpanel::JSON::XS" if available.
1414
1415       Test-related modules
1416           Test2::Tools::JSON
1417               [Author: AKIYM <https://metacpan.org/author/AKIYM>; Date:
1418               "2019-08-07"; Version: 0.05]
1419
1420           Test::Deep::JSON
1421               [โญ Author: MOTEMEN <https://metacpan.org/author/MOTEMEN>;
1422               Date: "2018-04-24"; Version: 0.05]
1423
1424               Compare JSON with Test::Deep. As of version 0.05, it relies on
1425               "JSON::MaybeXS".
1426
1427           Test::JSON
1428               [โญ Author: OVID <https://metacpan.org/author/OVID>; Date:
1429               "2009-08-09"; Version: 0.11]
1430
1431               ๐Ÿ‘Ž This offers a way to compare two different JSON strings to
1432               see if they refer to the same object. The most recent version,
1433               0.11, was released in 2009, and it relies on the deprecated
1434               "JSON::Any", which makes it essentially abandoned.
1435
1436           Test::JSON::Entails
1437               [Author: VOJ <https://metacpan.org/author/VOJ>; Date:
1438               "2012-09-14"; Version: 0.2]
1439
1440               ๐Ÿ‘Ž Test whether one JSON or Perl structure entails/subsumes
1441               another. The most recent version is from 2012, and it relies on
1442               "JSON::Any", so it is probably abandoned. Also, oddly but not
1443               uniquely for CPAN modules with the name JSON in the title, it
1444               seems to not actually have that much to do with JSON, which is
1445               a data serialisation format, but actually be testing Perl
1446               hashes and arrays.
1447
1448           Test::JSON::More
1449               [Author: BAYASHI <https://metacpan.org/author/BAYASHI>; Date:
1450               "2016-04-28"; Version: 0.02]
1451
1452               JSON Test Utility. As of version 0.02, it relies on "JSON" but
1453               it is able to use "JSON::XS" instead, and so probably
1454               "Cpanel::JSON::XS" would be OK too. According to the
1455               documentation, it can test JSON for validity and compare JSON
1456               strings with keys in a different order, and presumably with
1457               different whitespace.
1458
1459       Type-related modules
1460           These untangle numbers, strings, and booleans into JSON types.
1461
1462           JSON::TypeInference
1463               [Author: AEREAL <https://metacpan.org/author/AEREAL>; Date:
1464               "2015-10-26"; Version: "v1.0.2"]
1465
1466               ๐Ÿ˜• Virtually undocumented, it's not clear what this does.
1467
1468           JSON::Types
1469               [โญ Author: TYPESTER <https://metacpan.org/author/TYPESTER>;
1470               Date: "2012-10-17"; Version: 0.05]
1471
1472               Change the type of a Perl variable so that it comes out as a
1473               number, a string, or a boolean in the output JSON.
1474
1475           JSON::Types::Flexible
1476               [Author: PINE <https://metacpan.org/author/PINE>; Date:
1477               "2017-04-01"; Version: 0.03]
1478
1479               The module is barely documented, but from looking at the test
1480               file <https://metacpan.org/source/PINE/JSON-Types-
1481               Flexible-0.03/t%2Fjson%2Ftypes%2Fflexible%2Fclass.t>, this
1482               seems to enable you to change the output type of a number or a
1483               string so that you can, for example, make the number 1 come out
1484               as either a number, 1, a string "1", or a boolean, "true", in
1485               the output JSON.
1486
1487           JSON::Typist
1488               [โญ Author: RJBS <https://metacpan.org/author/RJBS>; Date:
1489               "2019-12-26"; Version: 0.006]
1490
1491               "Replace mushy strings and numbers with rigidly typed
1492               replacements"
1493
1494               Since Perl muddles strings and numbers, this enables you to
1495               work out whether your input JSON was "123" (a string) or 123 (a
1496               number).
1497
1498       Special-purpose modules
1499           App::JSON::to
1500               [โญ Author: DOLMEN <https://metacpan.org/author/DOLMEN>; Date:
1501               "2015-03-04"; Version: 1.000]
1502
1503               Convert JSON data to other formats. It reads your JSON file or
1504               input and converts it into either YAML or Perl native format
1505               using Data::Dumper.
1506
1507           boolean
1508               [โญโญ Author: INGY <https://metacpan.org/author/INGY>; Date:
1509               "2016-07-08"; Version: 0.46]
1510
1511               ๐Ÿ‘ This module offers "true" and "false" literals in Perl, so
1512               you just have
1513
1514                   use boolean;
1515                   my $something = true;
1516
1517               This is very useful for dealing with JSON.
1518
1519           Config::JSON
1520               [Author: RIZEN <https://metacpan.org/author/RIZEN>; Date:
1521               "2014-12-25"; Version: 1.5202]
1522
1523               Configuration files in JSON, with hash comments also allowed.
1524
1525           Devel::JSON
1526               [โญ Author: DOLMEN <https://metacpan.org/author/DOLMEN>; Date:
1527               "2017-09-03"; Version: 1.001]
1528
1529               For one-liners.
1530
1531                   If you use this module from the command-line, the last
1532                   value of your one-liner (-e) code will be serialized as
1533                   JSON data.
1534
1535           Inline::JSON
1536               [Author: KILNA <https://metacpan.org/author/KILNA>; Date:
1537               "2012-07-27"; Version: "v1.0.4"]
1538
1539               "Embed JSON data structures directly into your Perl code".
1540               Relies on "JSON".
1541
1542           JSON::Builder
1543               [Author: KNI <https://metacpan.org/author/KNI>; Date:
1544               "2015-04-16"; Version: 0.04]
1545
1546               Create JSON under memory limitations.
1547
1548           JSON::Color
1549               [โญ Author: PERLANCAR <https://metacpan.org/author/PERLANCAR>;
1550               Date: "2020-06-09"; Version: 0.130]
1551
1552               ๐ŸŒˆ This module generates JSON colorized with ANSI escape
1553               sequences.
1554
1555           JSON_File
1556               [โญ Author: GETTY <https://metacpan.org/author/GETTY>; Date:
1557               "2014-09-11"; Version: 0.004]
1558
1559           JSON::MultiValueOrdered
1560               [Author: TOBYINK <https://metacpan.org/author/TOBYINK>; Date:
1561               "2020-01-27"; Version: 0.006]
1562
1563               "JSON::MultiValueOrdered" is a special-purpose module for
1564               parsing JSON objects which have key collisions (something like
1565               "{"a":1,"a":2}") within objects.
1566
1567               (JSON::Parse's handling of key collisions is discussed in "Key
1568               collisions" in this document.)
1569
1570           JSON::String
1571               [Author: BRUMMETT <https://metacpan.org/author/BRUMMETT>; Date:
1572               "2015-02-04"; Version: "v0.2.0"]
1573
1574               Automatically change a JSON string when a data structure
1575               changes using tied scalars.
1576
1577       Patch, path, pointer, schema, and transform modules
1578           JSON::Assert
1579               [Author: SGREEN <https://metacpan.org/author/SGREEN>; Date:
1580               "2017-07-07"; Version: 0.08]
1581
1582               "Asserts JSONPaths into a JSON data structure for correct
1583               values/matches"
1584
1585           JSON::MergePatch
1586               [โญ Author: SOJIRO <https://metacpan.org/author/SOJIRO>; Date:
1587               "2016-02-24"; Version: 0.04]
1588
1589           JSON::Patch
1590               [Author: MIXAS <https://metacpan.org/author/MIXAS>; Date:
1591               "2018-10-25"; Version: 0.04]
1592
1593               ๐Ÿ˜• We don't know what this does, or how it relates to JSON. The
1594               example in the synopsis section of the document doesn't show
1595               any JSON, it shows an example of altering nested hashes in
1596               Perl.
1597
1598           JSON::Path
1599               [โญ Author: POPEFELIX <https://metacpan.org/author/POPEFELIX>;
1600               Date: "2018-05-05"; Version: 0.420]
1601
1602               Search nested hashref/arrayref structures using JSONPath.
1603
1604           JSON::Pointer
1605               [โญ Author: ZIGOROU <https://metacpan.org/author/ZIGOROU>;
1606               Date: "2015-08-13"; Version: 0.07]
1607
1608               Extract parts of a JSON string.
1609
1610           JSON::T
1611               [โญ Author: TOBYINK <https://metacpan.org/author/TOBYINK>;
1612               Date: "2014-09-28"; Version: 0.104]
1613
1614               Transform JSON using JsonT
1615
1616           JSON::Transform
1617               [โญ Author: ETJ <https://metacpan.org/author/ETJ>; Date:
1618               "2020-01-01"; Version: 0.03]
1619
1620           JSON::Validator
1621               [โญโญ Author: JHTHORSEN
1622               <https://metacpan.org/author/JHTHORSEN>; Date: "2021-01-24";
1623               Version: 4.12]
1624
1625               "Validate data against a JSON schema" - you can decide what the
1626               JSON is supposed to contain.
1627
1628       JSON extensions
1629           These modules extend JSON with comments and other things.
1630
1631           JSON::Diffable
1632               [โญ Author: PHAYLON <https://metacpan.org/author/PHAYLON>;
1633               Date: "2014-12-10"; Version: 0.000002]
1634
1635               "A relaxed and easy diffable JSON variant"
1636
1637           JSON::Relaxed
1638               [Author: MIKO <https://metacpan.org/author/MIKO>; Date:
1639               "2016-04-30"; Version: 0.05]
1640
1641               "An extension of JSON that allows for better human-
1642               readability".
1643
1644           JSONY
1645               [โญ Author: INGY <https://metacpan.org/author/INGY>; Date:
1646               "2020-04-27"; Version: "v0.1.21"]
1647
1648               "Relaxed JSON with a little bit of YAML"
1649
1650       Web interactions via JSON
1651           Crypt::JWT
1652               [โญโญ Author: MIK <https://metacpan.org/author/MIK>; Date:
1653               "2021-01-10"; Version: 0.031]
1654
1655               Module covers JSON Web Tokens, JSON Web Signature, and JSON Web
1656               Encryption.
1657
1658           JSON::API
1659               [โญ Author: GFRANKS <https://metacpan.org/author/GFRANKS>;
1660               Date: "2019-07-01"; Version: "v1.1.1"]
1661
1662               Combines LWP::UserAgent and JSON to make a unified module to
1663               communicate with a web server via JSON.
1664
1665           LWP::JSON::Tiny
1666               [โญ Author: SKINGTON <https://metacpan.org/author/SKINGTON>;
1667               Date: "2018-05-11"; Version: 0.014]
1668
1669           WWW::JSON
1670               [โญ Author: ANTIPASTA <https://metacpan.org/author/ANTIPASTA>;
1671               Date: "2015-05-27"; Version: 1.02]
1672
1673               "Make working with JSON Web API's as painless as possible"
1674
1675       Extension modules
1676           These modules extend the existing modules with some extra bits.
1677
1678           JSON::XS::Sugar
1679               [Author: MAXMIND <https://metacpan.org/author/MAXMIND>; Date:
1680               "2015-04-01"; Version: 1.01]
1681
1682               Provides booleans and number/string forcing for "JSON::XS".
1683
1684           Silki::JSON
1685               [โญ Author: DROLSKY <https://metacpan.org/author/DROLSKY>;
1686               Date: "2011-09-19"; Version: 0.29]
1687
1688               Switches on formatting and strict utf8 in a "JSON::XS" object.
1689
1690       Demonstration modules
1691           These modules provide a JSON parser as a demonstration of another
1692           technology.
1693
1694           JSON::Decode::Marpa
1695               [Author: PERLANCAR <https://metacpan.org/author/PERLANCAR>;
1696               Date: "2014-08-27"; Version: 0.02]
1697
1698           JSON::Decode::Regexp
1699               [Author: PERLANCAR <https://metacpan.org/author/PERLANCAR>;
1700               Date: "2018-03-25"; Version: 0.101]
1701
1702               ๐Ÿ›๐ŸฆŸ๐Ÿฆ‹๐Ÿž JSON parser as a single Perl Regex, originally by
1703               Randal Schwartz. This may be ingenious, but it's not remotely a
1704               useful JSON parser. For example, looking at the string part, it
1705               provides no Unicode validation, no support for Unicode escapes
1706               <https://metacpan.org/release/JSON-Decode-
1707               Regexp/source/lib/JSON/Decode/Regexp.pm#L141> and it allows
1708               invalid escapes such as "\xFF"
1709               <https://metacpan.org/release/JSON-Decode-
1710               Regexp/source/lib/JSON/Decode/Regexp.pm#L137>.
1711
1712           MarpaX::Demo::JSONParser
1713               [Author: RSAVAGE <https://metacpan.org/author/RSAVAGE>; Date:
1714               "2019-06-18"; Version: 1.08]
1715
1716           Pegex::JSON
1717               [Author: INGY <https://metacpan.org/author/INGY>; Date:
1718               "2020-01-22"; Version: 0.31]
1719
1720               ๐Ÿ› Based on Pegex.  See our bug report
1721               <https://github.com/pegex-parser/pegex-json-pm/issues/3>.
1722
1723       Other modules
1724           Modules which are parts of bigger distributions have not been
1725           included here except by accident.
1726
1727           App::JSON::Tools
1728               [Author: KABLAMO <https://metacpan.org/author/KABLAMO>; Date:
1729               "2016-08-05"; Version: 0.01]
1730
1731               Undocumented command-line tools for JSON.
1732
1733           App::JSONPretty
1734               [โญ Author: MSTROUT <https://metacpan.org/author/MSTROUT>;
1735               Date: "2011-02-02"; Version: 1]
1736
1737               ๐Ÿ‘Ž๐Ÿ› JSON prettification script. For whatever reason the script
1738               encapsulates the entirety of an old version of the "JSON"
1739               module dating from before "JSON::PP" was included in the Perl
1740               core.
1741
1742               If you need this kind of script, there is something called
1743               json_xs which comes with "JSON::XS", or equivalently
1744               cpanel_json_xs in the forked module "Cpanel::JSON::XS".
1745
1746           ARGV::JSON
1747               [โญ Author: MOTEMEN <https://metacpan.org/author/MOTEMEN>;
1748               Date: "2013-12-18"; Version: 0.01]
1749
1750           Jasonify
1751               [Author: BOBK <https://metacpan.org/author/BOBK>; Date:
1752               "2020-03-04"; Version: "v0.20.064"]
1753
1754           JS::JSON
1755               [Author: INGY <https://metacpan.org/author/INGY>; Date:
1756               "2008-08-30"; Version: 0.02]
1757
1758               ๐Ÿ‘Ž This is JavaScript code which was uploaded to CPAN. The
1759               original JavaScript is now obsolete since the thing it codes is
1760               included in all modern web browsers.
1761
1762           JSON::Eval
1763               [Author: TOBYINK <https://metacpan.org/author/TOBYINK>; Date:
1764               "2019-10-27"; Version: 0.002]
1765
1766               Eval Perl code found in JSON. This module enables one to encode
1767               and decode Perl scalar references and code references to JSON.
1768
1769           JSON::ize
1770               [โญ Author: MAJENSEN <https://metacpan.org/author/MAJENSEN>;
1771               Date: "2019-07-13"; Version: 0.202]
1772
1773               Something about one-liners.
1774
1775           JSON::JSend
1776               [Author: HOEKIT <https://metacpan.org/author/HOEKIT>; Date:
1777               "2016-04-23"; Version: 0.02]
1778
1779           JSON::Lines
1780               [โญ Author: LNATION <https://metacpan.org/author/LNATION>;
1781               Date: "2020-10-25"; Version: 0.03]
1782
1783               "JSON Lines is a convenient format for storing structured data
1784               that may be processed one record at a time."
1785
1786           JSON::Meth
1787               [โญ Author: ZOFFIX <https://metacpan.org/author/ZOFFIX>; Date:
1788               "2015-11-28"; Version: 1.001007]
1789
1790               ๐Ÿ˜• Claims to be "no nonsense JSON encoding/decoding as method
1791               calls on data". From the documentation:
1792
1793                   Don't make me think and give me what I want! This module
1794                   automatically figures out whether you want to encode a Perl
1795                   data structure to JSON or decode a JSON string to a Perl
1796                   data structure.
1797
1798           JSON::ON
1799               [Author: EWILHELM <https://metacpan.org/author/EWILHELM>; Date:
1800               "2013-06-26"; Version: "v0.0.3"]
1801
1802               JavaScript object notation object notator.
1803
1804           JSON::SL
1805               [โญ Author: MNUNBERG <https://metacpan.org/author/MNUNBERG>;
1806               Date: "2017-11-10"; Version: "v1.0.7"]
1807
1808           JSON::Streaming::Reader
1809               [โญ Author: MART <https://metacpan.org/author/MART>; Date:
1810               "2012-11-24"; Version: 0.06]
1811
1812           JSON::Streaming::Writer
1813               [Author: MART <https://metacpan.org/author/MART>; Date:
1814               "2012-11-24"; Version: 0.03]
1815
1816           JSON::Util
1817               [Author: JKUTEJ <https://metacpan.org/author/JKUTEJ>; Date:
1818               "2015-09-03"; Version: 0.06]
1819
1820               Relies on JSON::MaybeXS and the author's other module IO::Any,
1821               so that you can put either a file name or a JSON string as the
1822               argument and it tries to work out which one you have given it.
1823               That is ingenious, but it seems that if you are a programmer
1824               who cannot distinguish whether your input string is a file name
1825               or JSON, you have a very serious problem.
1826
1827           JSON::XS::ByteString
1828               [โญ Author: CINDY <https://metacpan.org/author/CINDY>; Date:
1829               "2020-04-18"; Version: 1.004]
1830
1831               ๐Ÿ˜• The README <https://metacpan.org/source/CINDY/JSON-XS-
1832               ByteString-1.004/README> claims it is a "thin wrapper around
1833               JSON::XS", but it contains a complete implementation of JSON
1834               <https://metacpan.org/source/CINDY/JSON-XS-
1835               ByteString-1.004/ByteString.xs>, which seems to have partly
1836               been copy-pasted from the JSON::XS source code, but internally
1837               it doesn't make any reference to JSON::XS. The licence and
1838               copyright statement don't mention JSON::XS's original author at
1839               all so we're not sure if this is a fork, a wrapper, or a
1840               reimplementation.
1841
1842               We haven't tried downloading this or installing it, but
1843               according to the documentation, this module encodes numbers
1844               with quotes around them, so "{this => 2}" turns into
1845               "{"this":"2"}".
1846
1847           JSON_minify
1848               [Author: RCOSCALI <https://metacpan.org/author/RCOSCALI>; Date:
1849               "2021-01-24"; Version: 1.1]
1850
1851           Text::JSON::Nibble
1852               [Author: DAEMON <https://metacpan.org/author/DAEMON>; Date:
1853               "2017-05-02"; Version: 1.01]
1854
1855               Nibble complete JSON objects from buffers.
1856
1857               This seems to be for extracting JSON from the midst of noise.
1858

SCRIPT

1860       A script "validjson" is supplied with the module. This runs
1861       "assert_valid_json" on its inputs, so run it like this.
1862
1863            validjson *.json
1864
1865       The default behaviour is to just do nothing if the input is valid. For
1866       invalid input it prints what the problem is:
1867
1868           validjson ids.go
1869           ids.go: JSON error at line 1, byte 1/7588: Unexpected character '/' parsing initial state: expecting whitespace: '\n', '\r', '\t', ' ' or start of string: '"' or digit: '0-9' or minus: '-' or start of an array or object: '{', '[' or start of literal: 't', 'f', 'n'.
1870
1871       If you need confirmation, use its --verbose option:
1872
1873           validjson -v *.json
1874
1875           atoms.json is valid JSON.
1876           ids.json is valid JSON.
1877           kanjidic.json is valid JSON.
1878           linedecomps.json is valid JSON.
1879           radkfile-radicals.json is valid JSON.
1880

DEPENDENCIES

1882       Carp
1883

EXPORTS

1885       The module exports nothing by default. Functions "parse_json",
1886       "parse_json_safe", "read_json", "valid_json" and "assert_valid_json",
1887       as well as the old function names "validate_json", "json_file_to_perl",
1888       and "json_to_perl", can be exported on request.
1889
1890       All of the functions can be exported using the tag ':all':
1891
1892           use JSON::Parse ':all';
1893

TESTING

1895   Internal testing code
1896       The module incorporates extensive testing related to the production of
1897       error messages and validation of input. Some of the testing code is
1898       supplied with the module in the /t/ subdirectory of the distribution.
1899
1900       More extensive testing code is in the git repository. This is not
1901       supplied in the CPAN distribution. A script, randomjson.pl
1902       <https://github.com/benkasminbullock/JSON-
1903       Parse/27b70e98176290ddd145cadfe8aa6ff43bb71703/randomjson.pl>,
1904       generates a set number of bytes of random JSON and checks that the
1905       module's bytewise validation of input is correct. It does this by
1906       taking a valid fragment, then adding each possible byte from 0 to 255
1907       to see whether the module correctly identifies it as valid or invalid
1908       at that point, then randomly picking one of the valid bytes and adding
1909       it to the fragment and continuing the process until a complete valid
1910       JSON input is formed. The module has undergone about a billion
1911       repetitions of this test.
1912
1913       This setup relies on a C file, json-random-test.c
1914       <https://github.com/benkasminbullock/JSON-
1915       Parse/27b70e98176290ddd145cadfe8aa6ff43bb71703/json-random-test.c>,
1916       which isn't in the CPAN distribution, and it also requires Json3.xs
1917       <https://github.com/benkasminbullock/JSON-
1918       Parse/27b70e98176290ddd145cadfe8aa6ff43bb71703/Json3.xs> to be edited
1919       to make the macro "TESTRANDOM" true (uncomment line 7 of the file). The
1920       testing code uses C setjmp/longjmp, so it's not guaranteed to work on
1921       all operating systems and is commented out for CPAN releases.
1922
1923       A pure C version called random-test.c
1924       <https://github.com/benkasminbullock/JSON-
1925       Parse/27b70e98176290ddd145cadfe8aa6ff43bb71703/random-test.c> also
1926       exists. This applies exactly the same tests, and requires no Perl at
1927       all.
1928
1929       If you're interested in testing your own JSON parser, the outputs
1930       generated by randomjson.pl <https://github.com/benkasminbullock/JSON-
1931       Parse/27b70e98176290ddd145cadfe8aa6ff43bb71703/randomjson.pl> are quite
1932       a good place to start. The default is to produce UTF-8 output, which
1933       looks pretty horrible since it tends to produce long strings of UTF-8
1934       garbage. (This is because it chooses randomly from 256 bytes and the
1935       end-of-string marker """ has only a 1/256 chance of being chosen, so
1936       the strings tend to get long and messy). You can mess with the
1937       internals of JSON::Parse by setting MAXBYTE in json-common.c to 0x80,
1938       recompiling (you can ignore the compiler warnings), and running
1939       randomjson.pl again to get just ASCII random JSON things. This breaks
1940       the UTF-8 functionality of JSON::Parse, so please don't install that
1941       version.
1942
1943   JSON Parsing Test Suite
1944       JSON::Parse version 0.58 passes most of the JSON Parsing Test Suite,
1945       with the exception that JSON::Parse rejects various erroneous UTF-8
1946       inputs, for example JSON::Parse will throw an error for non-character
1947       code points like Unicode U+FFFF and U+10FFFF. This parser only accepts
1948       valid UTF-8 as input. See "UTF-8 only".
1949
1950       In our opinion it would be a disservice to users of this module to
1951       allow bytes containing useless fragments such as incomplete parts of
1952       surrogate pairs, or invalid characters, just because the JSON
1953       specification doesn't actually explicitly demand rejecting these kinds
1954       of garbage inputs. Please see the function "daft_test" in the file
1955       xt/JPXT.pm for exactly which of these elements of the test suite we do
1956       not comply with. We note that this comment from Douglas Crockford, the
1957       inventor of JSON, JSON parser
1958       <https://github.com/douglascrockford/JSON-
1959       c/blob/master/utf8_decode.c#L38-L43>, dated 2005, agrees with our
1960       opinion on this point.
1961
1962       JSON::Parse version 0.58 also introduced "get_max_depth" and
1963       "set_max_depth" to prevent the stack overflow errors caused by some
1964       very deeply nested inputs such as those of the JSON Parsing Test Suite.
1965
1966   Test results
1967       CPAN testers <http://matrix.cpantesters.org/?dist=JSON-Parse+0.60>
1968       Travis CI <https://travis-ci.com/github/benkasminbullock/JSON-Parse>
1969

ACKNOWLEDGEMENTS

1971       Toby Inkster (TOBYINK) suggested some of the new function names which
1972       replaced the "OLD INTERFACE" names. Nicolas Immelman and Shlomi Fish
1973       (SHLOMIF) reported memory leaks which were fixed in 0.32 and 0.40.
1974       Github user kolmogorov42 reported a bug which led to 0.42. Github user
1975       SteveGlassman found an error in string copying for long strings, fixed
1976       in 0.57. Lars Dษชแด‡แด„แด‹แดแดก (DAXIM) pointed out problems with the JSON
1977       Parsing Test Suite which led to the addition of stack protection and
1978       "set_max_depth" and "get_max_depth" in 0.58.
1979

AUTHOR

1981       Ben Bullock, <bkb@cpan.org>
1982
1984       This package and associated files are copyright (C) 2013-2021 Ben
1985       Bullock.
1986
1987       You can use, copy, modify and redistribute this package and associated
1988       files under the Perl Artistic Licence or the GNU General Public
1989       Licence.
1990
1991
1992
1993perl v5.32.1                      2021-01-31                    JSON::Parse(3)
Impressum