… | |
… | |
8 | $perl_value = decode_cbor $binary_cbor_data; |
8 | $perl_value = decode_cbor $binary_cbor_data; |
9 | |
9 | |
10 | # OO-interface |
10 | # OO-interface |
11 | |
11 | |
12 | $coder = CBOR::XS->new; |
12 | $coder = CBOR::XS->new; |
13 | #TODO |
13 | $binary_cbor_data = $coder->encode ($perl_value); |
|
|
14 | $perl_value = $coder->decode ($binary_cbor_data); |
|
|
15 | |
|
|
16 | # prefix decoding |
|
|
17 | |
|
|
18 | my $many_cbor_strings = ...; |
|
|
19 | while (length $many_cbor_strings) { |
|
|
20 | my ($data, $length) = $cbor->decode_prefix ($many_cbor_strings); |
|
|
21 | # data was decoded |
|
|
22 | substr $many_cbor_strings, 0, $length, ""; # remove decoded cbor string |
|
|
23 | } |
14 | |
24 | |
15 | DESCRIPTION |
25 | DESCRIPTION |
16 | WARNING! THIS IS A PRE-ALPHA RELEASE! IT WILL CRASH, CORRUPT YOUR DATA |
26 | WARNING! This module is very new, and not very well tested (that's up to |
17 | AND EAT YOUR CHILDREN! (Actually, apart from being untested and a bit |
27 | you to do). Furthermore, details of the implementation might change |
18 | feature-limited, it might already be useful). |
28 | freely before version 1.0. And lastly, the object serialisation protocol |
|
|
29 | depends on a pending IANA assignment, and until that assignment is |
|
|
30 | official, this implementation is not interoperable with other |
|
|
31 | implementations (even future versions of this module) until the |
|
|
32 | assignment is done. |
|
|
33 | |
|
|
34 | You are still invited to try out CBOR, and this module. |
19 | |
35 | |
20 | This module converts Perl data structures to the Concise Binary Object |
36 | This module converts Perl data structures to the Concise Binary Object |
21 | Representation (CBOR) and vice versa. CBOR is a fast binary |
37 | Representation (CBOR) and vice versa. CBOR is a fast binary |
22 | serialisation format that aims to use a superset of the JSON data model, |
38 | serialisation format that aims to use a superset of the JSON data model, |
23 | i.e. when you can represent something in JSON, you should be able to |
39 | i.e. when you can represent something in JSON, you should be able to |
24 | represent it in CBOR. |
40 | represent it in CBOR. |
25 | |
41 | |
26 | This makes it a faster and more compact binary alternative to JSON. |
42 | In short, CBOR is a faster and very compact binary alternative to JSON, |
|
|
43 | with the added ability of supporting serialisation of Perl objects. |
|
|
44 | (JSON often compresses better than CBOR though, so if you plan to |
|
|
45 | compress the data later you might want to compare both formats first). |
27 | |
46 | |
28 | The primary goal of this module is to be *correct* and the secondary |
47 | The primary goal of this module is to be *correct* and the secondary |
29 | goal is to be *fast*. To reach the latter goal it was written in C. |
48 | goal is to be *fast*. To reach the latter goal it was written in C. |
30 | |
49 | |
31 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
50 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
… | |
… | |
145 | arrays, maps |
164 | arrays, maps |
146 | CBOR arrays and CBOR maps will be converted into references to a |
165 | CBOR arrays and CBOR maps will be converted into references to a |
147 | Perl array or hash, respectively. The keys of the map will be |
166 | Perl array or hash, respectively. The keys of the map will be |
148 | stringified during this process. |
167 | stringified during this process. |
149 | |
168 | |
150 | true, false |
169 | null |
151 | These CBOR values become "CBOR::XS::true" and "CBOR::XS::false", |
170 | CBOR null becomes "undef" in Perl. |
|
|
171 | |
|
|
172 | true, false, undefined |
|
|
173 | These CBOR values become "Types:Serialiser::true", |
|
|
174 | "Types:Serialiser::false" and "Types::Serialiser::error", |
152 | respectively. They are overloaded to act almost exactly like the |
175 | respectively. They are overloaded to act almost exactly like the |
153 | numbers 1 and 0. You can check whether a scalar is a CBOR boolean by |
176 | numbers 1 and 0 (for true and false) or to throw an exception on |
154 | using the "CBOR::XS::is_bool" function. |
177 | access (for error). See the Types::Serialiser manpage for details. |
155 | |
178 | |
156 | null, undefined |
179 | CBOR tag 256 (perl object) |
157 | CBOR null and undefined values becomes "undef" in Perl (in the |
180 | The tag value 256 (TODO: pending iana registration) will be used to |
158 | future, Undefined may raise an exception or something else). |
181 | deserialise a Perl object serialised with "FREEZE". See OBJECT |
|
|
182 | SERIALISATION, below, for details. |
159 | |
183 | |
160 | tags |
184 | CBOR tag 55799 (magic header) |
161 | Tagged items consists of a numeric tag and another CBOR value. The |
|
|
162 | tag 55799 is ignored (this tag implements the magic header). |
185 | The tag 55799 is ignored (this tag implements the magic header). |
163 | |
186 | |
164 | All other tags are currently converted into a CBOR::XS::Tagged |
187 | other CBOR tags |
|
|
188 | Tagged items consists of a numeric tag and another CBOR value. Tags |
|
|
189 | not handled internally are currently converted into a |
165 | object, which is simply a blessed array reference consistsing of the |
190 | CBOR::XS::Tagged object, which is simply a blessed array reference |
166 | numeric tag value followed by the (decoded) BOR value. |
191 | consisting of the numeric tag value followed by the (decoded) CBOR |
|
|
192 | value. |
|
|
193 | |
|
|
194 | In the future, support for user-supplied conversions might get |
|
|
195 | added. |
167 | |
196 | |
168 | anything else |
197 | anything else |
169 | Anything else (e.g. unsupported simple values) will raise a decoding |
198 | Anything else (e.g. unsupported simple values) will raise a decoding |
170 | error. |
199 | error. |
171 | |
200 | |
… | |
… | |
191 | and 1, which get turned into false and true in CBOR. |
220 | and 1, which get turned into false and true in CBOR. |
192 | |
221 | |
193 | CBOR::XS::Tagged objects |
222 | CBOR::XS::Tagged objects |
194 | Objects of this type must be arrays consisting of a single "[tag, |
223 | Objects of this type must be arrays consisting of a single "[tag, |
195 | value]" pair. The (numerical) tag will be encoded as a CBOR tag, the |
224 | value]" pair. The (numerical) tag will be encoded as a CBOR tag, the |
196 | value will be encoded as appropriate for the value. |
225 | value will be encoded as appropriate for the value. You cna use |
|
|
226 | "CBOR::XS::tag" to create such objects. |
197 | |
227 | |
198 | CBOR::XS::true, CBOR::XS::false |
228 | Types::Serialiser::true, Types::Serialiser::false, |
|
|
229 | Types::Serialiser::error |
199 | These special values become CBOR true and CBOR false values, |
230 | These special values become CBOR true, CBOR false and CBOR undefined |
200 | respectively. You can also use "\1" and "\0" directly if you want. |
231 | values, respectively. You can also use "\1", "\0" and "\undef" |
|
|
232 | directly if you want. |
201 | |
233 | |
202 | blessed objects |
234 | other blessed objects |
203 | Other blessed objects currently need to have a "TO_CBOR" method. It |
235 | Other blessed objects are serialised via "TO_CBOR" or "FREEZE". See |
204 | will be called on every object that is being serialised, and must |
236 | "OBJECT SERIALISATION", below, for details. |
205 | return something that can be encoded in CBOR. |
|
|
206 | |
237 | |
207 | simple scalars |
238 | simple scalars |
208 | TODO Simple Perl scalars (any scalar that is not a reference) are |
239 | TODO Simple Perl scalars (any scalar that is not a reference) are |
209 | the most difficult objects to encode: CBOR::XS will encode undefined |
240 | the most difficult objects to encode: CBOR::XS will encode undefined |
210 | scalars as CBOR null values, scalars that have last been used in a |
241 | scalars as CBOR null values, scalars that have last been used in a |
… | |
… | |
245 | IEEE single format if possible without loss of precision, otherwise |
276 | IEEE single format if possible without loss of precision, otherwise |
246 | the IEEE double format will be used. Perls that use formats other |
277 | the IEEE double format will be used. Perls that use formats other |
247 | than IEEE double to represent numerical values are supported, but |
278 | than IEEE double to represent numerical values are supported, but |
248 | might suffer loss of precision. |
279 | might suffer loss of precision. |
249 | |
280 | |
|
|
281 | OBJECT SERIALISATION |
|
|
282 | This module knows two way to serialise a Perl object: The CBOR-specific |
|
|
283 | way, and the generic way. |
|
|
284 | |
|
|
285 | Whenever the encoder encounters a Perl object that it cnanot serialise |
|
|
286 | directly (most of them), it will first look up the "TO_CBOR" method on |
|
|
287 | it. |
|
|
288 | |
|
|
289 | If it has a "TO_CBOR" method, it will call it with the object as only |
|
|
290 | argument, and expects exactly one return value, which it will then |
|
|
291 | substitute and encode it in the place of the object. |
|
|
292 | |
|
|
293 | Otherwise, it will look up the "FREEZE" method. If it exists, it will |
|
|
294 | call it with the object as first argument, and the constant string |
|
|
295 | "CBOR" as the second argument, to distinguish it from other serialisers. |
|
|
296 | |
|
|
297 | The "FREEZE" method can return any number of values (i.e. zero or more). |
|
|
298 | These will be encoded as CBOR perl object, together with the classname. |
|
|
299 | |
|
|
300 | If an object supports neither "TO_CBOR" nor "FREEZE", encoding will fail |
|
|
301 | with an error. |
|
|
302 | |
|
|
303 | Objects encoded via "TO_CBOR" cannot be automatically decoded, but |
|
|
304 | objects encoded via "FREEZE" can be decoded using the following |
|
|
305 | protocol: |
|
|
306 | |
|
|
307 | When an encoded CBOR perl object is encountered by the decoder, it will |
|
|
308 | look up the "THAW" method, by using the stored classname, and will fail |
|
|
309 | if the method cannot be found. |
|
|
310 | |
|
|
311 | After the lookup it will call the "THAW" method with the stored |
|
|
312 | classname as first argument, the constant string "CBOR" as second |
|
|
313 | argument, and all values returned by "FREEZE" as remaining arguments. |
|
|
314 | |
|
|
315 | EXAMPLES |
|
|
316 | Here is an example "TO_CBOR" method: |
|
|
317 | |
|
|
318 | sub My::Object::TO_CBOR { |
|
|
319 | my ($obj) = @_; |
|
|
320 | |
|
|
321 | ["this is a serialised My::Object object", $obj->{id}] |
|
|
322 | } |
|
|
323 | |
|
|
324 | When a "My::Object" is encoded to CBOR, it will instead encode a simple |
|
|
325 | array with two members: a string, and the "object id". Decoding this |
|
|
326 | CBOR string will yield a normal perl array reference in place of the |
|
|
327 | object. |
|
|
328 | |
|
|
329 | A more useful and practical example would be a serialisation method for |
|
|
330 | the URI module. CBOR has a custom tag value for URIs, namely 32: |
|
|
331 | |
|
|
332 | sub URI::TO_CBOR { |
|
|
333 | my ($self) = @_; |
|
|
334 | my $uri = "$self"; # stringify uri |
|
|
335 | utf8::upgrade $uri; # make sure it will be encoded as UTF-8 string |
|
|
336 | CBOR::XS::tagged 32, "$_[0]" |
|
|
337 | } |
|
|
338 | |
|
|
339 | This will encode URIs as a UTF-8 string with tag 32, which indicates an |
|
|
340 | URI. |
|
|
341 | |
|
|
342 | Decoding such an URI will not (currently) give you an URI object, but |
|
|
343 | instead a CBOR::XS::Tagged object with tag number 32 and the string - |
|
|
344 | exactly what was returned by "TO_CBOR". |
|
|
345 | |
|
|
346 | To serialise an object so it can automatically be deserialised, you need |
|
|
347 | to use "FREEZE" and "THAW". To take the URI module as example, this |
|
|
348 | would be a possible implementation: |
|
|
349 | |
|
|
350 | sub URI::FREEZE { |
|
|
351 | my ($self, $serialiser) = @_; |
|
|
352 | "$self" # encode url string |
|
|
353 | } |
|
|
354 | |
|
|
355 | sub URI::THAW { |
|
|
356 | my ($class, $serialiser, $uri) = @_; |
|
|
357 | |
|
|
358 | $class->new ($uri) |
|
|
359 | } |
|
|
360 | |
|
|
361 | Unlike "TO_CBOR", multiple values can be returned by "FREEZE". For |
|
|
362 | example, a "FREEZE" method that returns "type", "id" and "variant" |
|
|
363 | values would cause an invocation of "THAW" with 5 arguments: |
|
|
364 | |
|
|
365 | sub My::Object::FREEZE { |
|
|
366 | my ($self, $serialiser) = @_; |
|
|
367 | |
|
|
368 | ($self->{type}, $self->{id}, $self->{variant}) |
|
|
369 | } |
|
|
370 | |
|
|
371 | sub My::Object::THAW { |
|
|
372 | my ($class, $serialiser, $type, $id, $variant) = @_; |
|
|
373 | |
|
|
374 | $class-<new (type => $type, id => $id, variant => $variant) |
|
|
375 | } |
|
|
376 | |
250 | MAGIC HEADER |
377 | MAGIC HEADER |
251 | There is no way to distinguish CBOR from other formats programmatically. |
378 | There is no way to distinguish CBOR from other formats programmatically. |
252 | To make it easier to distinguish CBOR from other formats, the CBOR |
379 | To make it easier to distinguish CBOR from other formats, the CBOR |
253 | specification has a special "magic string" that can be prepended to any |
380 | specification has a special "magic string" that can be prepended to any |
254 | CBOR string without changing it's meaning. |
381 | CBOR string without changing it's meaning. |
255 | |
382 | |
256 | This string is available as $CBOR::XS::MAGIC. This module does not |
383 | This string is available as $CBOR::XS::MAGIC. This module does not |
257 | prepend this string tot he CBOR data it generates, but it will ignroe it |
384 | prepend this string tot he CBOR data it generates, but it will ignroe it |
258 | if present, so users can prepend this string as a "file type" indicator |
385 | if present, so users can prepend this string as a "file type" indicator |
259 | as required. |
386 | as required. |
260 | |
387 | |
|
|
388 | THE CBOR::XS::Tagged CLASS |
|
|
389 | CBOR has the concept of tagged values - any CBOR value can be tagged |
|
|
390 | with a numeric 64 bit number, which are centrally administered. |
|
|
391 | |
|
|
392 | "CBOR::XS" handles a few tags internally when en- or decoding. You can |
|
|
393 | also create tags yourself by encoding "CBOR::XS::Tagged" objects, and |
|
|
394 | the decoder will create "CBOR::XS::Tagged" objects itself when it hits |
|
|
395 | an unknown tag. |
|
|
396 | |
|
|
397 | These objects are simply blessed array references - the first member of |
|
|
398 | the array being the numerical tag, the second being the value. |
|
|
399 | |
|
|
400 | You can interact with "CBOR::XS::Tagged" objects in the following ways: |
|
|
401 | |
|
|
402 | $tagged = CBOR::XS::tag $tag, $value |
|
|
403 | This function(!) creates a new "CBOR::XS::Tagged" object using the |
|
|
404 | given $tag (0..2**64-1) to tag the given $value (which can be any |
|
|
405 | Perl value that can be encoded in CBOR, including serialisable Perl |
|
|
406 | objects and "CBOR::XS::Tagged" objects). |
|
|
407 | |
|
|
408 | $tagged->[0] |
|
|
409 | $tagged->[0] = $new_tag |
|
|
410 | $tag = $tagged->tag |
|
|
411 | $new_tag = $tagged->tag ($new_tag) |
|
|
412 | Access/mutate the tag. |
|
|
413 | |
|
|
414 | $tagged->[1] |
|
|
415 | $tagged->[1] = $new_value |
|
|
416 | $value = $tagged->value |
|
|
417 | $new_value = $tagged->value ($new_value) |
|
|
418 | Access/mutate the tagged value. |
|
|
419 | |
|
|
420 | EXAMPLES |
|
|
421 | Here are some examples of "CBOR::XS::Tagged" uses to tag objects. |
|
|
422 | |
|
|
423 | You can look up CBOR tag value and emanings in the IANA registry at |
|
|
424 | <http://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>. |
|
|
425 | |
|
|
426 | Prepend a magic header ($CBOR::XS::MAGIC): |
|
|
427 | |
|
|
428 | my $cbor = encode_cbor CBOR::XS::tag 55799, $value; |
|
|
429 | # same as: |
|
|
430 | my $cbor = $CBOR::XS::MAGIC . encode_cbor $value; |
|
|
431 | |
|
|
432 | Serialise some URIs and a regex in an array: |
|
|
433 | |
|
|
434 | my $cbor = encode_cbor [ |
|
|
435 | (CBOR::XS::tag 32, "http://www.nethype.de/"), |
|
|
436 | (CBOR::XS::tag 32, "http://software.schmorp.de/"), |
|
|
437 | (CBOR::XS::tag 35, "^[Pp][Ee][Rr][lL]\$"), |
|
|
438 | ]; |
|
|
439 | |
|
|
440 | Wrap CBOR data in CBOR: |
|
|
441 | |
|
|
442 | my $cbor_cbor = encode_cbor |
|
|
443 | CBOR::XS::tag 24, |
|
|
444 | encode_cbor [1, 2, 3]; |
|
|
445 | |
261 | CBOR and JSON |
446 | CBOR and JSON |
262 | CBOR is supposed to implement a superset of the JSON data model, and is, |
447 | CBOR is supposed to implement a superset of the JSON data model, and is, |
263 | with some coercion, able to represent all JSON texts (something that |
448 | with some coercion, able to represent all JSON texts (something that |
264 | other "binary JSON" formats such as BSON generally do not support). |
449 | other "binary JSON" formats such as BSON generally do not support). |
265 | |
450 | |
266 | CBOR implements some extra hints and support for JSON interoperability, |
451 | CBOR implements some extra hints and support for JSON interoperability, |
… | |
… | |
340 | |
525 | |
341 | SEE ALSO |
526 | SEE ALSO |
342 | The JSON and JSON::XS modules that do similar, but human-readable, |
527 | The JSON and JSON::XS modules that do similar, but human-readable, |
343 | serialisation. |
528 | serialisation. |
344 | |
529 | |
|
|
530 | The Types::Serialiser module provides the data model for true, false and |
|
|
531 | error values. |
|
|
532 | |
345 | AUTHOR |
533 | AUTHOR |
346 | Marc Lehmann <schmorp@schmorp.de> |
534 | Marc Lehmann <schmorp@schmorp.de> |
347 | http://home.schmorp.de/ |
535 | http://home.schmorp.de/ |
348 | |
536 | |