epic-s6ts
[kivitendo-erp.git] / SL / JSON.pm
1 package SL::JSON;
2
3 use strict;
4
5 use JSON ();
6
7 use parent qw(Exporter);
8 our @EXPORT = qw(encode_json decode_json to_json from_json);
9
10 sub new {
11   shift;
12   return JSON->new(@_)->convert_blessed(1);
13 }
14
15 sub encode_json {
16   return JSON->new->convert_blessed(1)->encode(@_);
17 }
18
19 sub decode_json {
20   goto &JSON::decode_json;
21 }
22
23 sub to_json {
24   my ($object, $options)      = @_;
25   $options                  ||= {};
26   $options->{convert_blessed} = 1;
27   return JSON::to_json($object, $options);
28 }
29
30 sub from_json {
31   goto &JSON::from_json;
32 }
33
34 1;
35 __END__
36
37 =pod
38
39 =encoding utf8
40
41 =head1 NAME
42
43 SL::JSON - Thin wrapper around the JSON module that provides default options
44
45 =head1 SYNOPSIS
46
47   use SL::JSON;
48
49   my $escaped_text_object = SL::Presenter->get->render('some/template');
50   my $json = encode_json($escaped_text_object);
51
52 =head1 OVERVIEW
53
54 JSON by default does not dump or stringify blessed
55 objects. kivitendo's rendering infrastructure always returns thin
56 proxy objects as instances of L<SL::Presenter::EscapedText>. This
57 module provides the same functions that L<JSON> does but changes their
58 default regarding converting blessed arguments.
59
60 =head1 FUNCTIONS
61
62 =over 4
63
64 =item C<decode_json $json>
65
66 Same as L<JSON/decode_json>.
67
68 =item C<encode_json $object>
69
70 Same as L<JSON/encode_json> but sets C<convert_blessed> first.
71
72 =item C<from_json $object [, $options]>
73
74 Same as L<JSON/from_json>.
75
76 =item C<to_json $object [, $options ]>
77
78 Same as L<JSON/to_json> but sets C<convert_blessed> first.
79
80 =back
81
82 =head1 BUGS
83
84 Nothing here yet.
85
86 =head1 AUTHOR
87
88 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
89
90 =cut