File Coverage

blib/lib/JSON/XS/ByteString.pm
Criterion Covered Total %
statement 8 8 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 11 11 100.0


line stmt bran cond sub pod time code
1             package JSON::XS::ByteString;
2              
3 1     1   82472 use 5.008;
  1         4  
4 1     1   5 use strict;
  1         2  
  1         21  
5 1     1   5 use warnings;
  1         2  
  1         131  
6              
7             require Exporter;
8              
9             our @ISA = qw(Exporter);
10             our @EXPORT_OK = qw(encode_json encode_json_unblessed decode_json decode_json_safe);
11             our $VERSION = 1.005000;
12              
13             require XSLoader;
14             XSLoader::load('JSON::XS::ByteString', $VERSION);
15              
16             =head1 NAME
17              
18             JSON::XS::ByteString - A more predictable and convenient XS implementation for JSON
19              
20             =head1 SYNOPSIS
21              
22             use JSON::XS::ByteString qw(encode_json encode_json_unblessed decode_json decode_json_safe);
23              
24             $json_string = encode_json($perl_data);
25             $perl_data = decode_json($json_string);
26             $perl_data = decode_json($json_string, 1); # die if $json_string is not valid JSON string
27              
28             $json_string = encode_json_unblessed($perl_data);
29             # the same behavior as encode_json
30             # but encode blessed references as reference strings,
31             # like 'Object=HASH(0xffffffff)'
32              
33             =head1 DESCRIPTION
34              
35             This module is a XS implementation for JSON. It provide a more predictable behavior than L by always producing strings in JSON for normal scalars.
36             And you can force it to produce numbers in JSON by putting references to numbers.
37              
38             All the string data are treated as UTF-8 octets and just copy them in and out directly, except C<">, C<\> and characters that C<< ord($char) < 32 >>
39              
40             C will return an undef without exceptions with invalid json string.
41              
42             =head1 DESIGN CONSIDERATION
43              
44             =head2 I didn't transfer the numeric value from C back to string values
45              
46             Because in the pure Perl world, there's insignificant difference between numeric or string.
47             So I think we don't need to do it since the result will be used in Perl.
48              
49             =head2 I didn't transfer the numeric value from C back to reference values
50              
51             Let C preserve the identical structure as it received.
52              
53             =head1 FUNCTIONS
54              
55             =head2 $json_string = encode_json($perl_data)
56              
57             Get a JSON string from a perl data structure. Treat blessed objects as normal references.
58              
59             =head2 $json_string = encode_json_unblessed($perl_data)
60              
61             Get a JSON string from a perl data structure. Treat blessed objects as strings (such as C<'Object=HASH(0xffffffff)'>)
62              
63             =head2 $perl_data = decode_json($json_string, $warn2die=0)
64              
65             Get the perl data structure back from a JSON string.
66              
67             If the given string is not a valid JSON string, it will return an C.
68             If the C<$warn2die> is false or not specified, this function will not die but warns an offset where it encountered the unrecognized character.
69             If the C<$warn2die> is true, this function will die with the error message which is identical to the warning one.
70              
71             =head2 $perl_data = decode_json_safe($json_string)
72              
73             The same as C except that C will not warn at all.
74              
75             =head1 SEE ALSO
76              
77             L
78              
79             This mod's github repository L
80              
81             =head1 AUTHOR
82              
83             Cindy Wang (CindyLinz)
84              
85             =head1 COPYRIGHT AND LICENSE
86              
87             Copyright (C) 2014-2021 by Cindy Wang (CindyLinz)
88              
89             This library is free software; you can redistribute it and/or modify
90             it under the same terms as Perl itself, either Perl version 5.8 or,
91             at your option, any later version of Perl 5 you may have available.
92              
93             =cut
94              
95             1;