… | |
… | |
44 | about 20% smaller than the same data encoded as (compact) JSON or |
44 | about 20% smaller than the same data encoded as (compact) JSON or |
45 | Storable. |
45 | Storable. |
46 | |
46 | |
47 | In addition to the core CBOR data format, this module implements a |
47 | In addition to the core CBOR data format, this module implements a |
48 | number of extensions, to support cyclic and shared data structures (see |
48 | number of extensions, to support cyclic and shared data structures (see |
49 | "allow_sharing"), string deduplication (see "pack_strings") and scalar |
49 | "allow_sharing" and "allow_cycles"), string deduplication (see |
50 | references (always enabled). |
50 | "pack_strings") and scalar references (always enabled). |
51 | |
51 | |
52 | The primary goal of this module is to be *correct* and the secondary |
52 | The primary goal of this module is to be *correct* and the secondary |
53 | goal is to be *fast*. To reach the latter goal it was written in C. |
53 | goal is to be *fast*. To reach the latter goal it was written in C. |
54 | |
54 | |
55 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
55 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
… | |
… | |
141 | instead will emit a reference to the earlier value. |
141 | instead will emit a reference to the earlier value. |
142 | |
142 | |
143 | This means that such values will only be encoded once, and will not |
143 | This means that such values will only be encoded once, and will not |
144 | result in a deep cloning of the value on decode, in decoders |
144 | result in a deep cloning of the value on decode, in decoders |
145 | supporting the value sharing extension. This also makes it possible |
145 | supporting the value sharing extension. This also makes it possible |
146 | to encode cyclic data structures. |
146 | to encode cyclic data structures (which need "allow_cycles" to ne |
|
|
147 | enabled to be decoded by this module). |
147 | |
148 | |
148 | It is recommended to leave it off unless you know your communication |
149 | It is recommended to leave it off unless you know your communication |
149 | partner supports the value sharing extensions to CBOR |
150 | partner supports the value sharing extensions to CBOR |
150 | (<http://cbor.schmorp.de/value-sharing>), as without decoder |
151 | (<http://cbor.schmorp.de/value-sharing>), as without decoder |
151 | support, the resulting data structure might be unusable. |
152 | support, the resulting data structure might be unusable. |
152 | |
153 | |
153 | Detecting shared values incurs a runtime overhead when values are |
154 | Detecting shared values incurs a runtime overhead when values are |
154 | encoded that have a reference counter large than one, and might |
155 | encoded that have a reference counter large than one, and might |
155 | unnecessarily increase the encoded size, as potentially shared |
156 | unnecessarily increase the encoded size, as potentially shared |
156 | values are encode as sharable whether or not they are actually |
157 | values are encode as shareable whether or not they are actually |
157 | shared. |
158 | shared. |
158 | |
159 | |
159 | At the moment, only targets of references can be shared (e.g. |
160 | At the moment, only targets of references can be shared (e.g. |
160 | scalars, arrays or hashes pointed to by a reference). Weirder |
161 | scalars, arrays or hashes pointed to by a reference). Weirder |
161 | constructs, such as an array with multiple "copies" of the *same* |
162 | constructs, such as an array with multiple "copies" of the *same* |
… | |
… | |
165 | If $enable is false (the default), then "encode" will encode shared |
166 | If $enable is false (the default), then "encode" will encode shared |
166 | data structures repeatedly, unsharing them in the process. Cyclic |
167 | data structures repeatedly, unsharing them in the process. Cyclic |
167 | data structures cannot be encoded in this mode. |
168 | data structures cannot be encoded in this mode. |
168 | |
169 | |
169 | This option does not affect "decode" in any way - shared values and |
170 | This option does not affect "decode" in any way - shared values and |
|
|
171 | references will always be decoded properly if present. |
|
|
172 | |
|
|
173 | $cbor = $cbor->allow_cycles ([$enable]) |
|
|
174 | $enabled = $cbor->get_allow_cycles |
|
|
175 | If $enable is true (or missing), then "decode" will happily decode |
|
|
176 | self-referential (cyclic) data structures. By default these will not |
|
|
177 | be decoded, as they need manual cleanup to avoid memory leaks, so |
|
|
178 | code that isn't prepared for this will not leak memory. |
|
|
179 | |
|
|
180 | If $enable is false (the default), then "decode" will throw an error |
|
|
181 | when it encounters a self-referential/cyclic data structure. |
|
|
182 | |
|
|
183 | This option does not affect "encode" in any way - shared values and |
170 | references will always be decoded properly if present. |
184 | references will always be decoded properly if present. |
171 | |
185 | |
172 | $cbor = $cbor->pack_strings ([$enable]) |
186 | $cbor = $cbor->pack_strings ([$enable]) |
173 | $enabled = $cbor->get_pack_strings |
187 | $enabled = $cbor->get_pack_strings |
174 | If $enable is true (or missing), then "encode" will try not to |
188 | If $enable is true (or missing), then "encode" will try not to |
… | |
… | |
396 | the IEEE double format will be used. Perls that use formats other |
410 | the IEEE double format will be used. Perls that use formats other |
397 | than IEEE double to represent numerical values are supported, but |
411 | than IEEE double to represent numerical values are supported, but |
398 | might suffer loss of precision. |
412 | might suffer loss of precision. |
399 | |
413 | |
400 | OBJECT SERIALISATION |
414 | OBJECT SERIALISATION |
|
|
415 | This module implements both a CBOR-specific and the generic |
|
|
416 | Types::Serialier object serialisation protocol. The following |
|
|
417 | subsections explain both methods. |
|
|
418 | |
|
|
419 | ENCODING |
401 | This module knows two way to serialise a Perl object: The CBOR-specific |
420 | This module knows two way to serialise a Perl object: The CBOR-specific |
402 | way, and the generic way. |
421 | way, and the generic way. |
403 | |
422 | |
404 | Whenever the encoder encounters a Perl object that it cnanot serialise |
423 | Whenever the encoder encounters a Perl object that it cannot serialise |
405 | directly (most of them), it will first look up the "TO_CBOR" method on |
424 | directly (most of them), it will first look up the "TO_CBOR" method on |
406 | it. |
425 | it. |
407 | |
426 | |
408 | If it has a "TO_CBOR" method, it will call it with the object as only |
427 | If it has a "TO_CBOR" method, it will call it with the object as only |
409 | argument, and expects exactly one return value, which it will then |
428 | argument, and expects exactly one return value, which it will then |
… | |
… | |
414 | "CBOR" as the second argument, to distinguish it from other serialisers. |
433 | "CBOR" as the second argument, to distinguish it from other serialisers. |
415 | |
434 | |
416 | The "FREEZE" method can return any number of values (i.e. zero or more). |
435 | The "FREEZE" method can return any number of values (i.e. zero or more). |
417 | These will be encoded as CBOR perl object, together with the classname. |
436 | These will be encoded as CBOR perl object, together with the classname. |
418 | |
437 | |
|
|
438 | These methods *MUST NOT* change the data structure that is being |
|
|
439 | serialised. Failure to comply to this can result in memory corruption - |
|
|
440 | and worse. |
|
|
441 | |
419 | If an object supports neither "TO_CBOR" nor "FREEZE", encoding will fail |
442 | If an object supports neither "TO_CBOR" nor "FREEZE", encoding will fail |
420 | with an error. |
443 | with an error. |
421 | |
444 | |
|
|
445 | DECODING |
422 | Objects encoded via "TO_CBOR" cannot be automatically decoded, but |
446 | Objects encoded via "TO_CBOR" cannot (normally) be automatically |
423 | objects encoded via "FREEZE" can be decoded using the following |
447 | decoded, but objects encoded via "FREEZE" can be decoded using the |
424 | protocol: |
448 | following protocol: |
425 | |
449 | |
426 | When an encoded CBOR perl object is encountered by the decoder, it will |
450 | When an encoded CBOR perl object is encountered by the decoder, it will |
427 | look up the "THAW" method, by using the stored classname, and will fail |
451 | look up the "THAW" method, by using the stored classname, and will fail |
428 | if the method cannot be found. |
452 | if the method cannot be found. |
429 | |
453 | |
… | |
… | |
584 | 26 (perl-object, <http://cbor.schmorp.de/perl-object>) |
608 | 26 (perl-object, <http://cbor.schmorp.de/perl-object>) |
585 | These tags are automatically created (and decoded) for serialisable |
609 | These tags are automatically created (and decoded) for serialisable |
586 | objects using the "FREEZE/THAW" methods (the Types::Serialier object |
610 | objects using the "FREEZE/THAW" methods (the Types::Serialier object |
587 | serialisation protocol). See "OBJECT SERIALISATION" for details. |
611 | serialisation protocol). See "OBJECT SERIALISATION" for details. |
588 | |
612 | |
589 | 28, 29 (sharable, sharedref, L <http://cbor.schmorp.de/value-sharing>) |
613 | 28, 29 (shareable, sharedref, L <http://cbor.schmorp.de/value-sharing>) |
590 | These tags are automatically decoded when encountered, resulting in |
614 | These tags are automatically decoded when encountered (and they do |
|
|
615 | not result in a cyclic data structure, see "allow_cycles"), |
591 | shared values in the decoded object. They are only encoded, however, |
616 | resulting in shared values in the decoded object. They are only |
592 | when "allow_sharable" is enabled. |
617 | encoded, however, when "allow_sharing" is enabled. |
|
|
618 | |
|
|
619 | Not all shared values can be successfully decoded: values that |
|
|
620 | reference themselves will *currently* decode as "undef" (this is not |
|
|
621 | the same as a reference pointing to itself, which will be |
|
|
622 | represented as a value that contains an indirect reference to itself |
|
|
623 | - these will be decoded properly). |
|
|
624 | |
|
|
625 | Note that considerably more shared value data structures can be |
|
|
626 | decoded than will be encoded - currently, only values pointed to by |
|
|
627 | references will be shared, others will not. While non-reference |
|
|
628 | shared values can be generated in Perl with some effort, they were |
|
|
629 | considered too unimportant to be supported in the encoder. The |
|
|
630 | decoder, however, will decode these values as shared values. |
593 | |
631 | |
594 | 256, 25 (stringref-namespace, stringref, L |
632 | 256, 25 (stringref-namespace, stringref, L |
595 | <http://cbor.schmorp.de/stringref>) |
633 | <http://cbor.schmorp.de/stringref>) |
596 | These tags are automatically decoded when encountered. They are only |
634 | These tags are automatically decoded when encountered. They are only |
597 | encoded, however, when "pack_strings" is enabled. |
635 | encoded, however, when "pack_strings" is enabled. |
… | |
… | |
703 | uses long double to represent floating point values, they might not be |
741 | uses long double to represent floating point values, they might not be |
704 | encoded properly. Half precision types are accepted, but not encoded. |
742 | encoded properly. Half precision types are accepted, but not encoded. |
705 | |
743 | |
706 | Strict mode and canonical mode are not implemented. |
744 | Strict mode and canonical mode are not implemented. |
707 | |
745 | |
|
|
746 | LIMITATIONS ON PERLS WITHOUT 64-BIT INTEGER SUPPORT |
|
|
747 | On perls that were built without 64 bit integer support (these are rare |
|
|
748 | nowadays, even on 32 bit architectures), support for any kind of 64 bit |
|
|
749 | integer in CBOR is very limited - most likely, these 64 bit values will |
|
|
750 | be truncated, corrupted, or otherwise not decoded correctly. This also |
|
|
751 | includes string, array and map sizes that are stored as 64 bit integers. |
|
|
752 | |
708 | THREADS |
753 | THREADS |
709 | This module is *not* guaranteed to be thread safe and there are no plans |
754 | This module is *not* guaranteed to be thread safe and there are no plans |
710 | to change this until Perl gets thread support (as opposed to the |
755 | to change this until Perl gets thread support (as opposed to the |
711 | horribly slow so-called "threads" which are simply slow and bloated |
756 | horribly slow so-called "threads" which are simply slow and bloated |
712 | process simulations - use fork, it's *much* faster, cheaper, better). |
757 | process simulations - use fork, it's *much* faster, cheaper, better). |