Relaxed.pm

filter: Statementstmt 71.8% 23 / 32 Branchbran n/a Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 77.7% 7 / 9 Podpod 66.6% 2 / 3 totaltotal 68.0% 32 / 47 CC 2 SCAR 7.1
CC
2
Cov
68%
from_rjson
6.0
decode_rjson
1.3
BEGIN
1.0
CRAP 2.0
« Relaxed/ErrorCodes.pm ↑ Summary
1 #! perl
2  
3 10 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package JSON::Relaxed;
7  
8 10 ▶use JSON::Relaxed::Parser; our $VERSION = $JSON::Relaxed::Parser::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =encoding UTF-8
11  
12 =head1 NAME
13  
14 JSON::Relaxed -- An extension of JSON that allows for better human-readability
15  
16 =head1 Relaxed JSON?
17  
18 There's been increasing support for the idea of expanding JSON to improve
19 human-readability.
20 "Relaxed" JSON (RJSON) is a term that has been used to describe a
21 JSON-ish format that has some human-friendly features that JSON doesn't.
22 Most notably, RJSON allows the use of JavaScript-like comments and
23 eliminates the need to quote all keys and values.
24 An (official) specification can be found on
25 L<RelaxedJSON.org|https://www.relaxedjson.org>.
26  
27 I<Note that by definition every valid JSON document is also a valid
28 RJSON document.>
29  
30 =head1 SYNOPSIS
31  
32 use JSON::Relaxed;
33  
34 # Some raw RJSON data.
35 my $rjson = <<'RAW_DATA';
36 /* Javascript-like comments. */
37 {
38 // Keys do not require quotes.
39 // Single, double and backtick quotes.
40 a : 'Larry',
41 b : "Curly",
42 c : `Phoey`,
43 // Simple values do not require quotes.
44 d: unquoted
45  
46 // Nested structures.
47 e: [
48 { a:1, b:2 },
49 ],
50  
51 // Like Perl, trailing commas are allowed.
52 f: "more stuff",
53 }
54 RAW_DATA
55  
56 # Functional parsing.
57 my $hash = decode_rjson($rjson);
58  
59 # Object-oriented parsing.
60 my $parser = JSON::Relaxed->new();
61 $hash = $parser->decode($rjson);
62  
63 =head1 DESCRIPTION
64  
65 JSON::Relaxed is a lightweight parser and serializer for RJSON.
66 It is fully compliant to the
67 L<RelaxedJSON.org|https://www.relaxedjson.org/specification> specification.
68  
69 It does, however, have some additional extensions to make it really
70 relaxed.
71  
72 =head1 LEGACY MODE
73  
74 The old static method C<from_rjson> has been renamed to C<decode_rjson>,
75 to conform to many other modules of this kind.
76 For compatibility with pre-0.060 versions
77 C<from_rjson> is kept as a synonym for C<decode_rjson>.
78  
79 For the same reason, the old parser method C<parse> has been renamed
80 to C<decode>.
81 For compatibility C<parse> is kept as a synonym for C<decode>.
82  
83 When called by one of the old names, JSON::Relaxed will operate in
84 legacy mode. This changes the way errors are handled.
85  
86 =head1 REALLY RELAXED EXTENSIONS
87  
88 Extensions are disabled if option C<strict> is set.
89 Otherwise, most extensions are enabled by default.
90 Some extensions need an additional option setting.
91  
92 =head2 Leading commas in lists
93  
94 For example,
95  
96 [ , 1 ]
97  
98 Enabled by default, overruled by C<strict>.
99  
100 =head2 Hash keys without values
101  
102 JSON::Relaxed supports object keys without a specified value.
103 In that case the hash element is simply assigned the undefined value.
104  
105 In the following example, a is assigned 1, and b is assigned undef:
106  
107 { a:1, b }
108  
109 Enabled by default, overruled by C<strict>.
110  
111 =head2 String continuation
112  
113 Long strings can be aesthetically split over multiple lines by putting
114 a backslash at the end of the line:
115  
116 "this is a " \
117 "long string"
118  
119 Note that this is different from
120  
121 "this is a \
122 long string"
123  
124 which B<embeds> the newline into the string, and requires continuation
125 lines to start at the beginning of the line to prevent unwanted spaces.
126  
127 Enabled by default, overruled by C<strict>.
128  
129 =head2 Extended Unicode escapes
130  
131 Unicode escapes in strings may contain an arbitrary number of hexadecimal
132 digits enclosed in braces:
133  
134 \u{1d10e}
135  
136 This eliminates the need to use
137 L<surrogates|https://unicode.org/faq/utf_bom.html#utf16-2>
138 to obtain the same character:
139  
140 \uD834\uDD0E
141  
142 Enabled by default, overruled by C<strict>.
143  
144 =head2 Combined hash keys
145  
146 Hash keys that contain periods are considered subkeys, e.g.
147  
148 foo.bar: blech
149  
150 is equivalent to
151  
152 foo: {
153 bar: blech
154 }
155  
156 Requires C<combined_keys> or C<prp> option. Overruled by C<strict>.
157  
158 =head2 Implied outer hash
159  
160 If the JSON looks like a hash, i.e. a string (key) followed by a
161 C<:>, the outer C<{> and C<}> are implied.
162  
163 For example:
164  
165 foo : bar
166  
167 is equivalent to:
168  
169 { foo : bar }
170  
171 Requires C<implied_outer_hash> or C<prp> option. Overruled by C<strict>.
172  
173 =head2 Garbage after JSON structure
174  
175 Requires C<extra_tokens_ok> option. Overruled by C<strict>.
176  
177 Normally, parsing will fail unless the input contains exactly one
178 valid JSON structure, i.e. a string, a hash or an array.
179  
180 With C<extra_tokens_ok> the first JSON structure is parsed and the
181 rest is ignored.
182  
183 =head2 PRP extensions
184  
185 Requires C<prp> option. Overruled by C<strict>.
186  
187 Enables some specific extensions:
188  
189 The equal sign C<=> can be used as an alternative to C<:> (colon).
190  
191 Colon (and equal sign) is optional between a key and its hash value.
192  
193 Single-line comments may start with C<#>.
194  
195 For example:
196  
197 # This is a sample PRP extended Really Relaxed JSON.
198 pdf.formats {
199 title.footer = [ "%{copyright}" "" "%{page}" ]
200 first.footer = [ "%{copyright}" "" "" ]
201 }
202  
203 This is equivalent to Really Relaxed JSON:
204  
205 // This is a sample Really Relaxed JSON.
206 pdf.formats: {
207 title.footer: [ "%{copyright}" "" "%{page}" ]
208 first.footer: [ "%{copyright}" "" "" ]
209 }
210  
211 And Relaxed JSON:
212  
213 // This is a sample Relaxed JSON.
214 {
215 pdf: {
216 formats: {
217 title: {
218 footer: [ "%{copyright}" "" "%{page}" ]
219 }
220 first: {
221 footer: [ "%{copyright}" "" "" ]
222 }
223 }
224 }
225 }
226  
227 And JSON:
228  
229 {
230 "pdf" : {
231 "formats" : {
232 "title" : {
233 "footer" : [ "%{copyright}", "", "%{page}" ]
234 }
235 },
236 "first" : {
237 "footer" : [ "%{copyright}", "", "" ]
238 }
239 }
240 }
241 }
242  
243 You decide what is easiest to write ☺.
244  
245 =head1 SUBROUTINES
246  
247 =head2 decode_rjson
248  
249 $structure = decode_rjson( $data, %options )
250  
251 C<decode_rjson()> is the simple way to parse an RJSON string.
252 It is exported by default.
253 C<decode_rjson> takes a single parameter, the string to be parsed.
254  
255 Optionally an additional hash with options can be passed
256 to change the behaviour of the parser.
257 See L<below|"Object-oriented-parsing">.
258  
259 $structure = decode_rjson( $rjson, %options );
260  
261 =cut
262  
263 10 our $err_id;
264 10 our $err_msg;
265  
266 0 ▶sub decode_rjson {
Subroutine decode_rjson: not called CC=1 SCAR=2.6
Pod documented
267 0 my ( $raw, %options ) = @_;
268 10 ▶ use JSON::Relaxed::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 0 my $parser = JSON::Relaxed::Parser->new(%options);
270 0 my $res = $parser->decode($raw);
271 # Legacy.
272 0 $err_id = $parser->err_id;
273 0 $err_msg = $parser->err_msg;
274  
275 0 return $res;
276 }
277  
278 # Legacy.
279 0 ▶sub from_rjson {
Subroutine from_rjson: not called CC=2 SCAR=17.9
Pod undocumented
280 0 my ( $raw, %options ) = @_;
281 0 ▶ $options{croak_on_error} //= 0;
Condition0%0/2
$options{"croak_on_error"} //= 0
l!l
00
MC/DC0%0/1
$options{"croak_on_error"} //= 0
$options{"croak_on_error"}
Truth table0%0/2
A
A= $options{"croak_on_error"}
Aresult
11
00
282 0 decode_rjson( $raw, %options );
283 }
284  
285 =head1 OBJECT-ORIENTED PARSING
286  
287  
288 =head2 new
289  
290 Create a C<JSON::Relaxed> object, suitable for one or many operations.
291  
292 $parser = JSON::Relaxed->new( %options );
293  
294 Options:
295  
296 =over 4
297  
298 =item strict
299  
300 When set to a true value, enforces full compliance with the
301 L<RelaxedJSON.org|https://www.relaxedjson.org/specification> specification.
302  
303 Default value is false, enabling JSON::Relaxed extensions.
304  
305 =item croak_on_error
306  
307 Disabled by default in legacy mode, enabled otherwise.
308  
309 Causes parsing error to be signalled with an exception.
310  
311 See L</"ERROR HANDLING">.
312  
313 =item extra_tokens_ok
314  
315 =item combined_keys
316  
317 =item implied_outer_hash
318  
319 =item prp
320  
321 Enables/disables some of the extensions described
322 L<above|/REALLY RELAXED EXTENSIONS">.
323  
324 =item key_order
325  
326 Adds a key C<" key order "> to each hash, containing an array with the
327 hash keys in order of appearance. This is used for pretty printing.
328  
329 =back
330  
331 =head2 decode
332  
333 This method parses the JSON string, passed as argument.
334  
335 $structure = $parser->decode($rjson);
336  
337 =head2 parse
338  
339 This is the same as decode, but also enables legacy mode.
340  
341 =head2 err_id
342  
343 =head2 err_pos
344  
345 =head2 err_msg
346  
347 Fetches the error information for the last error, if any.
348  
349 Error ids are simple short strings, like C<"multiple-structures">.
350  
351 C<err_pos> fetches the text position in the JSON string where the
352 error occured. Returns -1 if this information is not available.
353  
354 C<err_msg> Fetches the text of the last error message.
355  
356 For a full list, see L<JSON::Relaxed::ErrorCodes>.
357  
358 =head2 strict
359  
360 =head2 croak_on_error
361  
362 =head2 extra_tokens_ok
363  
364 =head2 combined_keys
365  
366 =head2 implied_outer_hash
367  
368 =head2 prp
369  
370 =head2 pretty (see "encode")
371  
372 =head2 key_order
373  
374 =head2 booleans (see L<"Boolean values">)
375  
376 Sets/resets options.
377  
378 Note that the value must be assigned to, e.g.
379  
380 $parser->strict = 1; # enable
381  
382 =head2 encode
383  
384 $string = $parser->encode( data => $data, %options )
385  
386 Produces a string with a really relaxed rendition of the data.
387 With option C<pretty>, the rendition is pretty-printed.
388  
389 With option C<key_order> the order of hash keys will be taken from a
390 pseudo-key C<" key order ">. This pseudo-key is added when option
391 C<key_order> is passed to C<decode>.
392  
393 A Perl structure is passed as C<data> option. This structure is encoded.
394 Note however that this structure may contain only strings, arrays and hashes.
395  
396 Option C<schema> can be used to provide schema data for structure to
397 be encoded. For each item to be encoded, the schema is consulted and
398 the following schema items are prepended as comments: C<title>,
399 C<description>, and C<infoText>.
400  
401 =cut
402  
403 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
404 1 my ($class, %opts) = @_;
405 1 return JSON::Relaxed::Parser->new(%opts);
406 }
407  
408 10 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
409 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
410 10 our @EXPORT = qw(decode_rjson);
411 10 our @EXPORT_OK = ( @EXPORT, qw(from_rjson) );
412 10 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
413 }
414  
415 =head1 MAPPING
416  
417 =head2 RRJSON to Perl
418  
419 =over 4
420  
421 =item *
422  
423 Numbers are unquoted strings. They will be mapped to numbers if the
424 repesentation is identical to the source.
425 For example, the unquoted string C<1> and the quoted string C<"1">
426 will both yield the number C<1>
427 The unquoted string C<1.0> will also yield the number C<1>,
428 but C<"1.0"> will yield the string C<"1.0">.
429  
430 =item *
431  
432 Unquoted C<null> will become C<undef>.
433  
434 =item *
435  
436 Unquoted C<true> and C<false> will yield JSON::Boolean objects that
437 test as boolean (true resp. false) and stringify as C<"true"> resp.
438 C<"false">. See L</"Boolean values"> how to change this behaviour.
439  
440 Likewise unquoted C<on> and C<off> when option C<prp> is specified.
441  
442 =item *
443  
444 Other unquoted strings will be treated as quoted strings.
445  
446 =back
447  
448 =head2 Perl to RRJSON
449  
450 =over 4
451  
452 =item *
453  
454 Numbers will be output as numbers.
455  
456 =item *
457  
458 Strings will be output as unquoted strings if possible, quoted strings
459 otherwise. Non-latin characters will be output as C<\u> escapes.
460 When some of the quotes C<" ' `> are embedded the others will be tried
461 for the string, e.g. C<"a\"b"> will yield C<'a"b'>.
462  
463 All quotes are equal, there is no difference in interpretation.
464  
465 =item *
466  
467 Boolean objects will be output as unquoted C<true> and C<false>.
468  
469 =item *
470  
471 Undefined values will be output as C<null>.
472  
473 =back
474  
475 =head2 Boolean values
476  
477 By default JSON::Boolean objects will be used for unquoted C<true> and
478 C<false>. The C<booleans> method can be used to change this.
479  
480 $parser->booleans = [ false-value, true-value ]
481  
482 This sets the values to be used for C<true> and C<false>.
483 Default is
484  
485 $parser->booleans = [ $JSON::Boolean::false, $JSON::Boolean::true ]
486  
487 A non-array true value establishes the default.
488  
489 Setting to a false value is the same as
490  
491 $parser->booleans = [ 0, 1 ]
492  
493 With option C<prp>, unquoted C<on> is the same as C<true>, and C<off>
494 is the same as C<false>.
495  
496 =head1 ERROR HANDLING
497  
498 If the document cannot be parsed, JSON::Relaxed will throw an
499 exception.
500  
501 In legacy mode, JSON::Relaxed returns an undefined
502 value and sets error indicators in $JSON::Relaxed::err_id and
503 $JSON::Relaxed::err_msg.
504  
505 If parser property C<croak_on_error> is set to a false
506 value, it will behave as if in legacy mode.
507  
508 For a full list of error codes, see L<JSON::Relaxed::ErrorCodes>.
509  
510 =head1 AUTHOR
511  
512 Johan Vromans F<jv@cpan.org>
513  
514 Based on original code from Miko O'Sullivan F<miko@idocs.com>.
515  
516 =head1 SUPPORT
517  
518 Development of this module takes place on GitHub:
519 L<https://github.com/sciurius/perl-JSON-Relaxed>.
520  
521 You can find documentation for this module with the perldoc command.
522  
523 perldoc JSON::Relaxed
524  
525 Please report any bugs or feature requests using the issue tracker on
526 GitHub.
527  
528 =head1 LICENSE
529  
530 Copyright (c) 2024 by Johan Vromans. All rights reserved. This
531 program is free software; you can redistribute it and/or modify it
532 under the same terms as Perl itself. This software comes with B<NO
533 WARRANTY> of any kind.
534  
535 =cut
536  
537 10 1;
« Relaxed/ErrorCodes.pm ↑ Summary