line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::YAJL::Generator; |
2
|
3
|
|
|
3
|
|
19
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
95
|
|
3
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
308
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require XSLoader; |
7
|
|
|
|
|
|
|
XSLoader::load( 'JSON::YAJL::Generator', $VERSION ); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
1; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
JSON::YAJL::Generator - JSON generation with YAJL |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use JSON::YAJL; |
18
|
|
|
|
|
|
|
my $generator = JSON::YAJL::Generator->new(); |
19
|
|
|
|
|
|
|
# or to beautify (indent): |
20
|
|
|
|
|
|
|
# my $generator = JSON::YAJL::Generator->new( 1, ' ' ); |
21
|
|
|
|
|
|
|
$generator->map_open(); |
22
|
|
|
|
|
|
|
$generator->string("integer"); |
23
|
|
|
|
|
|
|
$generator->integer(123); |
24
|
|
|
|
|
|
|
$generator->string("double"); |
25
|
|
|
|
|
|
|
$generator->double("1.23"); |
26
|
|
|
|
|
|
|
$generator->string("number"); |
27
|
|
|
|
|
|
|
$generator->number("3.141"); |
28
|
|
|
|
|
|
|
$generator->string("string"); |
29
|
|
|
|
|
|
|
$generator->string("a string"); |
30
|
|
|
|
|
|
|
$generator->string("string2"); |
31
|
|
|
|
|
|
|
$generator->string("another string"); |
32
|
|
|
|
|
|
|
$generator->string("null"); |
33
|
|
|
|
|
|
|
$generator->null(); |
34
|
|
|
|
|
|
|
$generator->string("true"); |
35
|
|
|
|
|
|
|
$generator->bool(1); |
36
|
|
|
|
|
|
|
$generator->string("false"); |
37
|
|
|
|
|
|
|
$generator->bool(0); |
38
|
|
|
|
|
|
|
$generator->string("map"); |
39
|
|
|
|
|
|
|
$generator->map_open(); |
40
|
|
|
|
|
|
|
$generator->string("key"); |
41
|
|
|
|
|
|
|
$generator->string("value"); |
42
|
|
|
|
|
|
|
$generator->string("array"); |
43
|
|
|
|
|
|
|
$generator->array_open(); |
44
|
|
|
|
|
|
|
$generator->integer(1); |
45
|
|
|
|
|
|
|
$generator->integer(2); |
46
|
|
|
|
|
|
|
$generator->integer(3); |
47
|
|
|
|
|
|
|
$generator->array_close(); |
48
|
|
|
|
|
|
|
$generator->map_close(); |
49
|
|
|
|
|
|
|
$generator->map_close(); |
50
|
|
|
|
|
|
|
print $generator->get_buf; |
51
|
|
|
|
|
|
|
$generator->clear; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# This prints non-beautified: |
54
|
|
|
|
|
|
|
{"integer":123,"double":1.2299999999999999822,"number":3.141,"string":"a string","string2":"another string","null":null,"true":true,"false":false,"map":{"key":"value","array":[1,2,3]}} |
55
|
|
|
|
|
|
|
# or beautified: |
56
|
|
|
|
|
|
|
{ |
57
|
|
|
|
|
|
|
"integer": 123, |
58
|
|
|
|
|
|
|
"double": 1.2299999999999999822, |
59
|
|
|
|
|
|
|
"number": 3.141, |
60
|
|
|
|
|
|
|
"string": "a string", |
61
|
|
|
|
|
|
|
"string2": "another string", |
62
|
|
|
|
|
|
|
"null": null, |
63
|
|
|
|
|
|
|
"true": true, |
64
|
|
|
|
|
|
|
"false": false, |
65
|
|
|
|
|
|
|
"map": { |
66
|
|
|
|
|
|
|
"key": "value", |
67
|
|
|
|
|
|
|
"array": [ |
68
|
|
|
|
|
|
|
1, |
69
|
|
|
|
|
|
|
2, |
70
|
|
|
|
|
|
|
3 |
71
|
|
|
|
|
|
|
] |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 DESCRIPTION |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
This module allows you to generate JSON with YAJL. This is quite a low-level |
78
|
|
|
|
|
|
|
interface for generating JSON and it accumulates JSON in an internal buffer |
79
|
|
|
|
|
|
|
until you fetch it. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
If you create certain invalid JSON constructs then this module throws an |
82
|
|
|
|
|
|
|
exception. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This is a very early release to see how cross-platform the underlying code is. |
85
|
|
|
|
|
|
|
The API may change in future. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 METHODS |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 new |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
The constructor. You can pass in a flag on whether to generate indented |
92
|
|
|
|
|
|
|
(beautiful) output and if so how many spaces to use: |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $generator = JSON::YAJL::Generator->new(); |
95
|
|
|
|
|
|
|
# or to beautify (indent): |
96
|
|
|
|
|
|
|
# my $generator = JSON::YAJL::Generator->new( 1, ' ' ); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 null |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Generate a null: |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
$generator->null(); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 bool |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Generate a boolean value: |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$generator->bool(1); |
109
|
|
|
|
|
|
|
$generator->bool(0); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 integer |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Generate an integer: |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
$generator->integer(123); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 double |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Generate a floating point number: |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
$generator->double("1.23"); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head2 number |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Generate a generic number. The underlying C library has to specify numeric |
126
|
|
|
|
|
|
|
types, but in Perl this is unnecessary so you probably just want to use this |
127
|
|
|
|
|
|
|
method: |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
$generator->number("3.141"); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 string |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Generate a string: |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
$generator->string("a string"); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 map_open |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Begins a hash: |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$generator->map_open(); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head2 map_close |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Ends a hash: |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$generator->map_close(); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 array_open |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Begins an array: |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
$generator->array_open(); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 array_close |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Ends an array: |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
$generator->array_close(); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 get_buf |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Access the null terminated generator buffer. If incrementally outputing JSON, |
164
|
|
|
|
|
|
|
one should call clear to clear the buffer. This allows stream generation. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
print $generator->get_buf; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 clear |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Clear the output buffer, but maintain all internal generation state. This |
171
|
|
|
|
|
|
|
function will not "reset" the generator state, and is intended to enable |
172
|
|
|
|
|
|
|
incremental JSON outputing: |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
$generator->clear; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 AUTHOR |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Leon Brocard |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 LICENSE |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
This module is free software; you can redistribute it or modify it under the same terms as Perl itself. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 SEE ALSO |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
L, L |