… | |
… | |
39 | 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 |
40 | represent it in CBOR. |
40 | represent it in CBOR. |
41 | |
41 | |
42 | In short, CBOR is a faster and very 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. |
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). |
|
|
46 | |
|
|
47 | To give you a general idea about speed, with texts in the megabyte |
|
|
48 | range, "CBOR::XS" usually encodes roughly twice as fast as Storable or |
|
|
49 | JSON::XS and decodes about 15%-30% faster than those. The shorter the |
|
|
50 | data, the worse Storable performs in comparison. |
|
|
51 | |
|
|
52 | As for compactness, "CBOR::XS" encoded data structures are usually about |
|
|
53 | 20% smaller than the same data encoded as (compact) JSON or Storable. |
44 | |
54 | |
45 | The primary goal of this module is to be *correct* and the secondary |
55 | The primary goal of this module is to be *correct* and the secondary |
46 | goal is to be *fast*. To reach the latter goal it was written in C. |
56 | goal is to be *fast*. To reach the latter goal it was written in C. |
47 | |
57 | |
48 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
58 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
… | |
… | |
174 | numbers 1 and 0 (for true and false) or to throw an exception on |
184 | numbers 1 and 0 (for true and false) or to throw an exception on |
175 | access (for error). See the Types::Serialiser manpage for details. |
185 | access (for error). See the Types::Serialiser manpage for details. |
176 | |
186 | |
177 | CBOR tag 256 (perl object) |
187 | CBOR tag 256 (perl object) |
178 | The tag value 256 (TODO: pending iana registration) will be used to |
188 | The tag value 256 (TODO: pending iana registration) will be used to |
179 | deserialise a Perl object serialised with "FREEZE". See "OBJECT |
189 | deserialise a Perl object serialised with "FREEZE". See OBJECT |
180 | SERIALISATION", below, for details. |
190 | SERIALISATION, below, for details. |
181 | |
191 | |
182 | CBOR tag 55799 (magic header) |
192 | CBOR tag 55799 (magic header) |
183 | The tag 55799 is ignored (this tag implements the magic header). |
193 | The tag 55799 is ignored (this tag implements the magic header). |
184 | |
194 | |
185 | other CBOR tags |
195 | other CBOR tags |
… | |
… | |
218 | and 1, which get turned into false and true in CBOR. |
228 | and 1, which get turned into false and true in CBOR. |
219 | |
229 | |
220 | CBOR::XS::Tagged objects |
230 | CBOR::XS::Tagged objects |
221 | Objects of this type must be arrays consisting of a single "[tag, |
231 | Objects of this type must be arrays consisting of a single "[tag, |
222 | value]" pair. The (numerical) tag will be encoded as a CBOR tag, the |
232 | value]" pair. The (numerical) tag will be encoded as a CBOR tag, the |
223 | value will be encoded as appropriate for the value. |
233 | value will be encoded as appropriate for the value. You cna use |
|
|
234 | "CBOR::XS::tag" to create such objects. |
224 | |
235 | |
225 | Types::Serialiser::true, Types::Serialiser::false, |
236 | Types::Serialiser::true, Types::Serialiser::false, |
226 | Types::Serialiser::error |
237 | Types::Serialiser::error |
227 | These special values become CBOR true, CBOR false and CBOR undefined |
238 | These special values become CBOR true, CBOR false and CBOR undefined |
228 | values, respectively. You can also use "\1", "\0" and "\undef" |
239 | values, respectively. You can also use "\1", "\0" and "\undef" |
… | |
… | |
379 | |
390 | |
380 | This string is available as $CBOR::XS::MAGIC. This module does not |
391 | This string is available as $CBOR::XS::MAGIC. This module does not |
381 | prepend this string tot he CBOR data it generates, but it will ignroe it |
392 | prepend this string tot he CBOR data it generates, but it will ignroe it |
382 | if present, so users can prepend this string as a "file type" indicator |
393 | if present, so users can prepend this string as a "file type" indicator |
383 | as required. |
394 | as required. |
|
|
395 | |
|
|
396 | THE CBOR::XS::Tagged CLASS |
|
|
397 | CBOR has the concept of tagged values - any CBOR value can be tagged |
|
|
398 | with a numeric 64 bit number, which are centrally administered. |
|
|
399 | |
|
|
400 | "CBOR::XS" handles a few tags internally when en- or decoding. You can |
|
|
401 | also create tags yourself by encoding "CBOR::XS::Tagged" objects, and |
|
|
402 | the decoder will create "CBOR::XS::Tagged" objects itself when it hits |
|
|
403 | an unknown tag. |
|
|
404 | |
|
|
405 | These objects are simply blessed array references - the first member of |
|
|
406 | the array being the numerical tag, the second being the value. |
|
|
407 | |
|
|
408 | You can interact with "CBOR::XS::Tagged" objects in the following ways: |
|
|
409 | |
|
|
410 | $tagged = CBOR::XS::tag $tag, $value |
|
|
411 | This function(!) creates a new "CBOR::XS::Tagged" object using the |
|
|
412 | given $tag (0..2**64-1) to tag the given $value (which can be any |
|
|
413 | Perl value that can be encoded in CBOR, including serialisable Perl |
|
|
414 | objects and "CBOR::XS::Tagged" objects). |
|
|
415 | |
|
|
416 | $tagged->[0] |
|
|
417 | $tagged->[0] = $new_tag |
|
|
418 | $tag = $tagged->tag |
|
|
419 | $new_tag = $tagged->tag ($new_tag) |
|
|
420 | Access/mutate the tag. |
|
|
421 | |
|
|
422 | $tagged->[1] |
|
|
423 | $tagged->[1] = $new_value |
|
|
424 | $value = $tagged->value |
|
|
425 | $new_value = $tagged->value ($new_value) |
|
|
426 | Access/mutate the tagged value. |
|
|
427 | |
|
|
428 | EXAMPLES |
|
|
429 | Here are some examples of "CBOR::XS::Tagged" uses to tag objects. |
|
|
430 | |
|
|
431 | You can look up CBOR tag value and emanings in the IANA registry at |
|
|
432 | <http://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>. |
|
|
433 | |
|
|
434 | Prepend a magic header ($CBOR::XS::MAGIC): |
|
|
435 | |
|
|
436 | my $cbor = encode_cbor CBOR::XS::tag 55799, $value; |
|
|
437 | # same as: |
|
|
438 | my $cbor = $CBOR::XS::MAGIC . encode_cbor $value; |
|
|
439 | |
|
|
440 | Serialise some URIs and a regex in an array: |
|
|
441 | |
|
|
442 | my $cbor = encode_cbor [ |
|
|
443 | (CBOR::XS::tag 32, "http://www.nethype.de/"), |
|
|
444 | (CBOR::XS::tag 32, "http://software.schmorp.de/"), |
|
|
445 | (CBOR::XS::tag 35, "^[Pp][Ee][Rr][lL]\$"), |
|
|
446 | ]; |
|
|
447 | |
|
|
448 | Wrap CBOR data in CBOR: |
|
|
449 | |
|
|
450 | my $cbor_cbor = encode_cbor |
|
|
451 | CBOR::XS::tag 24, |
|
|
452 | encode_cbor [1, 2, 3]; |
384 | |
453 | |
385 | CBOR and JSON |
454 | CBOR and JSON |
386 | CBOR is supposed to implement a superset of the JSON data model, and is, |
455 | CBOR is supposed to implement a superset of the JSON data model, and is, |
387 | with some coercion, able to represent all JSON texts (something that |
456 | with some coercion, able to represent all JSON texts (something that |
388 | other "binary JSON" formats such as BSON generally do not support). |
457 | other "binary JSON" formats such as BSON generally do not support). |