… | |
… | |
16 | $coder = CBOR::XS->new; |
16 | $coder = CBOR::XS->new; |
17 | #TODO |
17 | #TODO |
18 | |
18 | |
19 | =head1 DESCRIPTION |
19 | =head1 DESCRIPTION |
20 | |
20 | |
21 | WARNING! THIS IS A PRE-ALPHA RELEASE! IT WILL CRASH, CORRUPT YOUR DATA AND |
21 | WARNING! THIS IS A PRE-ALPHA RELEASE! IT WILL CRASH, CORRUPT YOUR DATA |
22 | EAT YOUR CHILDREN! |
22 | AND EAT YOUR CHILDREN! (Actually, apart from being untested and a bit |
|
|
23 | feature-limited, it might already be useful). |
23 | |
24 | |
24 | This module converts Perl data structures to CBOR and vice versa. Its |
25 | This module converts Perl data structures to the Concise Binary Object |
|
|
26 | Representation (CBOR) and vice versa. CBOR is a fast binary serialisation |
|
|
27 | format that aims to use a superset of the JSON data model, i.e. when you |
|
|
28 | can represent something in JSON, you should be able to represent it in |
|
|
29 | CBOR. |
|
|
30 | |
|
|
31 | This makes it a faster and more compact binary alternative to JSON. |
|
|
32 | |
25 | primary goal is to be I<correct> and its secondary goal is to be |
33 | The primary goal of this module is to be I<correct> and the secondary goal |
26 | I<fast>. To reach the latter goal it was written in C. |
34 | is to be I<fast>. To reach the latter goal it was written in C. |
27 | |
35 | |
28 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
36 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
29 | vice versa. |
37 | vice versa. |
30 | |
38 | |
31 | =cut |
39 | =cut |
32 | |
40 | |
33 | package CBOR::XS; |
41 | package CBOR::XS; |
34 | |
42 | |
35 | use common::sense; |
43 | use common::sense; |
36 | |
44 | |
37 | our $VERSION = 0.02; |
45 | our $VERSION = 0.03; |
38 | our @ISA = qw(Exporter); |
46 | our @ISA = qw(Exporter); |
39 | |
47 | |
40 | our @EXPORT = qw(encode_cbor decode_cbor); |
48 | our @EXPORT = qw(encode_cbor decode_cbor); |
41 | |
49 | |
42 | use Exporter; |
50 | use Exporter; |
… | |
… | |
163 | |
171 | |
164 | =head2 CBOR -> PERL |
172 | =head2 CBOR -> PERL |
165 | |
173 | |
166 | =over 4 |
174 | =over 4 |
167 | |
175 | |
|
|
176 | =item integers |
|
|
177 | |
|
|
178 | CBOR integers become (numeric) perl scalars. On perls without 64 bit |
|
|
179 | support, 64 bit integers will be truncated or otherwise corrupted. |
|
|
180 | |
|
|
181 | =item byte strings |
|
|
182 | |
|
|
183 | Byte strings will become octet strings in Perl (the byte values 0..255 |
|
|
184 | will simply become characters of the same value in Perl). |
|
|
185 | |
|
|
186 | =item UTF-8 strings |
|
|
187 | |
|
|
188 | UTF-8 strings in CBOR will be decoded, i.e. the UTF-8 octets will be |
|
|
189 | decoded into proper Unicode code points. At the moment, the validity of |
|
|
190 | the UTF-8 octets will not be validated - corrupt input will result in |
|
|
191 | corrupted Perl strings. |
|
|
192 | |
|
|
193 | =item arrays, maps |
|
|
194 | |
|
|
195 | CBOR arrays and CBOR maps will be converted into references to a Perl |
|
|
196 | array or hash, respectively. The keys of the map will be stringified |
|
|
197 | during this process. |
|
|
198 | |
168 | =item True, False |
199 | =item true, false |
169 | |
200 | |
170 | These CBOR values become C<CBOR::XS::true> and C<CBOR::XS::false>, |
201 | These CBOR values become C<CBOR::XS::true> and C<CBOR::XS::false>, |
171 | respectively. They are overloaded to act almost exactly like the numbers |
202 | respectively. They are overloaded to act almost exactly like the numbers |
172 | C<1> and C<0>. You can check whether a scalar is a CBOR boolean by using |
203 | C<1> and C<0>. You can check whether a scalar is a CBOR boolean by using |
173 | the C<CBOR::XS::is_bool> function. |
204 | the C<CBOR::XS::is_bool> function. |
174 | |
205 | |
175 | =item Null, Undefined |
206 | =item null, undefined |
176 | |
207 | |
177 | CBOR Null and Undefined values becomes C<undef> in Perl (in the future, |
208 | CBOR null and undefined values becomes C<undef> in Perl (in the future, |
178 | Undefined may raise an exception). |
209 | Undefined may raise an exception or something else). |
|
|
210 | |
|
|
211 | =item tags |
|
|
212 | |
|
|
213 | Tagged items consists of a numeric tag and another CBOR value. The tag |
|
|
214 | 55799 is ignored (this tag implements the magic header). |
|
|
215 | |
|
|
216 | All other tags are currently converted into a L<CBOR::XS::Tagged> object, |
|
|
217 | which is simply a blessed array reference consistsing of the numeric tag |
|
|
218 | value followed by the (decoded) BOR value. |
|
|
219 | |
|
|
220 | =item anything else |
|
|
221 | |
|
|
222 | Anything else (e.g. unsupported simple values) will raise a decoding |
|
|
223 | error. |
179 | |
224 | |
180 | =back |
225 | =back |
181 | |
226 | |
182 | |
227 | |
183 | =head2 PERL -> CBOR |
228 | =head2 PERL -> CBOR |
… | |
… | |
188 | |
233 | |
189 | =over 4 |
234 | =over 4 |
190 | |
235 | |
191 | =item hash references |
236 | =item hash references |
192 | |
237 | |
193 | Perl hash references become CBOR maps. As there is no inherent ordering |
238 | Perl hash references become CBOR maps. As there is no inherent ordering in |
194 | in hash keys (or CBOR maps), they will usually be encoded in a |
239 | hash keys (or CBOR maps), they will usually be encoded in a pseudo-random |
195 | pseudo-random order. |
240 | order. |
|
|
241 | |
|
|
242 | Currently, tied hashes will use the indefinite-length format, while normal |
|
|
243 | hashes will use the fixed-length format. |
196 | |
244 | |
197 | =item array references |
245 | =item array references |
198 | |
246 | |
199 | Perl array references become CBOR arrays. |
247 | Perl array references become fixed-length CBOR arrays. |
200 | |
248 | |
201 | =item other references |
249 | =item other references |
202 | |
250 | |
203 | Other unblessed references are generally not allowed and will cause an |
251 | Other unblessed references are generally not allowed and will cause an |
204 | exception to be thrown, except for references to the integers C<0> and |
252 | exception to be thrown, except for references to the integers C<0> and |
205 | C<1>, which get turned into C<False> and C<True> in CBOR. |
253 | C<1>, which get turned into false and true in CBOR. |
|
|
254 | |
|
|
255 | =item CBOR::XS::Tagged objects |
|
|
256 | |
|
|
257 | Objects of this type must be arrays consisting of a single C<[tag, value]> |
|
|
258 | pair. The (numerical) tag will be encoded as a CBOR tag, the value will be |
|
|
259 | encoded as appropriate for the value. |
206 | |
260 | |
207 | =item CBOR::XS::true, CBOR::XS::false |
261 | =item CBOR::XS::true, CBOR::XS::false |
208 | |
262 | |
209 | These special values become CBOR True and CBOR False values, |
263 | These special values become CBOR true and CBOR false values, |
210 | respectively. You can also use C<\1> and C<\0> directly if you want. |
264 | respectively. You can also use C<\1> and C<\0> directly if you want. |
211 | |
265 | |
212 | =item blessed objects |
266 | =item blessed objects |
213 | |
267 | |
214 | Blessed objects are not directly representable in CBOR. TODO |
268 | Other blessed objects currently need to have a C<TO_CBOR> method. It |
215 | See the |
269 | will be called on every object that is being serialised, and must return |
216 | C<allow_blessed> and C<convert_blessed> methods on various options on |
270 | something that can be encoded in CBOR. |
217 | how to deal with this: basically, you can choose between throwing an |
|
|
218 | exception, encoding the reference as if it weren't blessed, or provide |
|
|
219 | your own serialiser method. |
|
|
220 | |
271 | |
221 | =item simple scalars |
272 | =item simple scalars |
222 | |
273 | |
223 | TODO |
274 | TODO |
224 | Simple Perl scalars (any scalar that is not a reference) are the most |
275 | Simple Perl scalars (any scalar that is not a reference) are the most |
225 | difficult objects to encode: CBOR::XS will encode undefined scalars as |
276 | difficult objects to encode: CBOR::XS will encode undefined scalars as |
226 | CBOR C<Null> values, scalars that have last been used in a string context |
277 | CBOR null values, scalars that have last been used in a string context |
227 | before encoding as CBOR strings, and anything else as number value: |
278 | before encoding as CBOR strings, and anything else as number value: |
228 | |
279 | |
229 | # dump as number |
280 | # dump as number |
230 | encode_cbor [2] # yields [2] |
281 | encode_cbor [2] # yields [2] |
231 | encode_cbor [-3.0e17] # yields [-3e+17] |
282 | encode_cbor [-3.0e17] # yields [-3e+17] |
… | |
… | |
253 | |
304 | |
254 | You can not currently force the type in other, less obscure, ways. Tell me |
305 | You can not currently force the type in other, less obscure, ways. Tell me |
255 | if you need this capability (but don't forget to explain why it's needed |
306 | if you need this capability (but don't forget to explain why it's needed |
256 | :). |
307 | :). |
257 | |
308 | |
258 | Note that numerical precision has the same meaning as under Perl (so |
309 | Perl values that seem to be integers generally use the shortest possible |
259 | binary to decimal conversion follows the same rules as in Perl, which |
310 | representation. Floating-point values will use either the IEEE single |
260 | can differ to other languages). Also, your perl interpreter might expose |
311 | format if possible without loss of precision, otherwise the IEEE double |
261 | extensions to the floating point numbers of your platform, such as |
312 | format will be used. Perls that use formats other than IEEE double to |
262 | infinities or NaN's - these cannot be represented in CBOR, and it is an |
313 | represent numerical values are supported, but might suffer loss of |
263 | error to pass those in. |
314 | precision. |
264 | |
315 | |
265 | =back |
316 | =back |
266 | |
317 | |
267 | |
318 | |
268 | =head2 MAGIC HEADER |
319 | =head2 MAGIC HEADER |
… | |
… | |
278 | required. |
329 | required. |
279 | |
330 | |
280 | |
331 | |
281 | =head2 CBOR and JSON |
332 | =head2 CBOR and JSON |
282 | |
333 | |
283 | TODO |
334 | CBOR is supposed to implement a superset of the JSON data model, and is, |
|
|
335 | with some coercion, able to represent all JSON texts (something that other |
|
|
336 | "binary JSON" formats such as BSON generally do not support). |
|
|
337 | |
|
|
338 | CBOR implements some extra hints and support for JSON interoperability, |
|
|
339 | and the spec offers further guidance for conversion between CBOR and |
|
|
340 | JSON. None of this is currently implemented in CBOR, and the guidelines |
|
|
341 | in the spec do not result in correct round-tripping of data. If JSON |
|
|
342 | interoperability is improved in the future, then the goal will be to |
|
|
343 | ensure that decoded JSON data will round-trip encoding and decoding to |
|
|
344 | CBOR intact. |
284 | |
345 | |
285 | |
346 | |
286 | =head1 SECURITY CONSIDERATIONS |
347 | =head1 SECURITY CONSIDERATIONS |
287 | |
348 | |
288 | When you are using CBOR in a protocol, talking to untrusted potentially |
349 | When you are using CBOR in a protocol, talking to untrusted potentially |