… | |
… | |
12 | $coder = CBOR::XS->new; |
12 | $coder = CBOR::XS->new; |
13 | #TODO |
13 | #TODO |
14 | |
14 | |
15 | DESCRIPTION |
15 | DESCRIPTION |
16 | WARNING! THIS IS A PRE-ALPHA RELEASE! IT WILL CRASH, CORRUPT YOUR DATA |
16 | WARNING! THIS IS A PRE-ALPHA RELEASE! IT WILL CRASH, CORRUPT YOUR DATA |
17 | AND EAT YOUR CHILDREN! |
17 | AND EAT YOUR CHILDREN! (Actually, apart from being untested and a bit |
|
|
18 | feature-limited, it might already be useful). |
18 | |
19 | |
19 | This module converts Perl data structures to CBOR and vice versa. Its |
20 | This module converts Perl data structures to the Concise Binary Object |
20 | primary goal is to be *correct* and its secondary goal is to be *fast*. |
21 | Representation (CBOR) and vice versa. CBOR is a fast binary |
|
|
22 | 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 |
|
|
24 | represent it in CBOR. |
|
|
25 | |
|
|
26 | This makes it a faster and more compact binary alternative to JSON. |
|
|
27 | |
|
|
28 | The primary goal of this module is to be *correct* and the secondary |
21 | To reach the latter goal it was written in C. |
29 | goal is to be *fast*. To reach the latter goal it was written in C. |
22 | |
30 | |
23 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
31 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
24 | vice versa. |
32 | vice versa. |
25 | |
33 | |
26 | FUNCTIONAL INTERFACE |
34 | FUNCTIONAL INTERFACE |
… | |
… | |
118 | For the more enlightened: note that in the following descriptions, |
126 | For the more enlightened: note that in the following descriptions, |
119 | lowercase *perl* refers to the Perl interpreter, while uppercase *Perl* |
127 | lowercase *perl* refers to the Perl interpreter, while uppercase *Perl* |
120 | refers to the abstract Perl language itself. |
128 | refers to the abstract Perl language itself. |
121 | |
129 | |
122 | CBOR -> PERL |
130 | CBOR -> PERL |
|
|
131 | integers |
|
|
132 | CBOR integers become (numeric) perl scalars. On perls without 64 bit |
|
|
133 | support, 64 bit integers will be truncated or otherwise corrupted. |
|
|
134 | |
|
|
135 | byte strings |
|
|
136 | Byte strings will become octet strings in Perl (the byte values |
|
|
137 | 0..255 will simply become characters of the same value in Perl). |
|
|
138 | |
|
|
139 | UTF-8 strings |
|
|
140 | UTF-8 strings in CBOR will be decoded, i.e. the UTF-8 octets will be |
|
|
141 | decoded into proper Unicode code points. At the moment, the validity |
|
|
142 | of the UTF-8 octets will not be validated - corrupt input will |
|
|
143 | result in corrupted Perl strings. |
|
|
144 | |
|
|
145 | arrays, maps |
|
|
146 | 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 |
|
|
148 | stringified during this process. |
|
|
149 | |
123 | True, False |
150 | true, false |
124 | These CBOR values become "CBOR::XS::true" and "CBOR::XS::false", |
151 | These CBOR values become "CBOR::XS::true" and "CBOR::XS::false", |
125 | respectively. They are overloaded to act almost exactly like the |
152 | respectively. They are overloaded to act almost exactly like the |
126 | numbers 1 and 0. You can check whether a scalar is a CBOR boolean by |
153 | numbers 1 and 0. You can check whether a scalar is a CBOR boolean by |
127 | using the "CBOR::XS::is_bool" function. |
154 | using the "CBOR::XS::is_bool" function. |
128 | |
155 | |
129 | Null, Undefined |
156 | null, undefined |
130 | CBOR Null and Undefined values becomes "undef" in Perl (in the |
157 | CBOR null and undefined values becomes "undef" in Perl (in the |
131 | future, Undefined may raise an exception). |
158 | future, Undefined may raise an exception or something else). |
|
|
159 | |
|
|
160 | tags |
|
|
161 | Tagged items consists of a numeric tag and another CBOR value. The |
|
|
162 | tag 55799 is ignored (this tag implements the magic header). |
|
|
163 | |
|
|
164 | All other tags are currently converted into a CBOR::XS::Tagged |
|
|
165 | object, which is simply a blessed array reference consistsing of the |
|
|
166 | numeric tag value followed by the (decoded) BOR value. |
|
|
167 | |
|
|
168 | anything else |
|
|
169 | Anything else (e.g. unsupported simple values) will raise a decoding |
|
|
170 | error. |
132 | |
171 | |
133 | PERL -> CBOR |
172 | PERL -> CBOR |
134 | The mapping from Perl to CBOR is slightly more difficult, as Perl is a |
173 | The mapping from Perl to CBOR is slightly more difficult, as Perl is a |
135 | truly typeless language, so we can only guess which CBOR type is meant |
174 | truly typeless language, so we can only guess which CBOR type is meant |
136 | by a Perl value. |
175 | by a Perl value. |
… | |
… | |
138 | hash references |
177 | hash references |
139 | Perl hash references become CBOR maps. As there is no inherent |
178 | Perl hash references become CBOR maps. As there is no inherent |
140 | ordering in hash keys (or CBOR maps), they will usually be encoded |
179 | ordering in hash keys (or CBOR maps), they will usually be encoded |
141 | in a pseudo-random order. |
180 | in a pseudo-random order. |
142 | |
181 | |
|
|
182 | Currently, tied hashes will use the indefinite-length format, while |
|
|
183 | normal hashes will use the fixed-length format. |
|
|
184 | |
143 | array references |
185 | array references |
144 | Perl array references become CBOR arrays. |
186 | Perl array references become fixed-length CBOR arrays. |
145 | |
187 | |
146 | other references |
188 | other references |
147 | Other unblessed references are generally not allowed and will cause |
189 | Other unblessed references are generally not allowed and will cause |
148 | an exception to be thrown, except for references to the integers 0 |
190 | an exception to be thrown, except for references to the integers 0 |
149 | and 1, which get turned into "False" and "True" in CBOR. |
191 | and 1, which get turned into false and true in CBOR. |
|
|
192 | |
|
|
193 | CBOR::XS::Tagged objects |
|
|
194 | 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 |
|
|
196 | value will be encoded as appropriate for the value. |
150 | |
197 | |
151 | CBOR::XS::true, CBOR::XS::false |
198 | CBOR::XS::true, CBOR::XS::false |
152 | These special values become CBOR True and CBOR False values, |
199 | These special values become CBOR true and CBOR false values, |
153 | respectively. You can also use "\1" and "\0" directly if you want. |
200 | respectively. You can also use "\1" and "\0" directly if you want. |
154 | |
201 | |
155 | blessed objects |
202 | blessed objects |
156 | Blessed objects are not directly representable in CBOR. TODO See the |
203 | Other blessed objects currently need to have a "TO_CBOR" method. It |
157 | "allow_blessed" and "convert_blessed" methods on various options on |
204 | will be called on every object that is being serialised, and must |
158 | how to deal with this: basically, you can choose between throwing an |
205 | return something that can be encoded in CBOR. |
159 | exception, encoding the reference as if it weren't blessed, or |
|
|
160 | provide your own serialiser method. |
|
|
161 | |
206 | |
162 | simple scalars |
207 | simple scalars |
163 | TODO Simple Perl scalars (any scalar that is not a reference) are |
208 | TODO Simple Perl scalars (any scalar that is not a reference) are |
164 | the most difficult objects to encode: CBOR::XS will encode undefined |
209 | the most difficult objects to encode: CBOR::XS will encode undefined |
165 | scalars as CBOR "Null" values, scalars that have last been used in a |
210 | scalars as CBOR null values, scalars that have last been used in a |
166 | string context before encoding as CBOR strings, and anything else as |
211 | string context before encoding as CBOR strings, and anything else as |
167 | number value: |
212 | number value: |
168 | |
213 | |
169 | # dump as number |
214 | # dump as number |
170 | encode_cbor [2] # yields [2] |
215 | encode_cbor [2] # yields [2] |
… | |
… | |
193 | |
238 | |
194 | You can not currently force the type in other, less obscure, ways. |
239 | You can not currently force the type in other, less obscure, ways. |
195 | Tell me if you need this capability (but don't forget to explain why |
240 | Tell me if you need this capability (but don't forget to explain why |
196 | it's needed :). |
241 | it's needed :). |
197 | |
242 | |
198 | Note that numerical precision has the same meaning as under Perl (so |
243 | Perl values that seem to be integers generally use the shortest |
199 | binary to decimal conversion follows the same rules as in Perl, |
244 | possible representation. Floating-point values will use either the |
200 | which can differ to other languages). Also, your perl interpreter |
245 | IEEE single format if possible without loss of precision, otherwise |
201 | might expose extensions to the floating point numbers of your |
246 | the IEEE double format will be used. Perls that use formats other |
202 | platform, such as infinities or NaN's - these cannot be represented |
247 | than IEEE double to represent numerical values are supported, but |
203 | in CBOR, and it is an error to pass those in. |
248 | might suffer loss of precision. |
204 | |
249 | |
205 | MAGIC HEADER |
250 | MAGIC HEADER |
206 | There is no way to distinguish CBOR from other formats programmatically. |
251 | There is no way to distinguish CBOR from other formats programmatically. |
207 | To make it easier to distinguish CBOR from other formats, the CBOR |
252 | To make it easier to distinguish CBOR from other formats, the CBOR |
208 | specification has a special "magic string" that can be prepended to any |
253 | specification has a special "magic string" that can be prepended to any |
… | |
… | |
212 | prepend this string tot he CBOR data it generates, but it will ignroe it |
257 | prepend this string tot he CBOR data it generates, but it will ignroe it |
213 | if present, so users can prepend this string as a "file type" indicator |
258 | if present, so users can prepend this string as a "file type" indicator |
214 | as required. |
259 | as required. |
215 | |
260 | |
216 | CBOR and JSON |
261 | CBOR and JSON |
217 | TODO |
262 | 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 |
|
|
264 | other "binary JSON" formats such as BSON generally do not support). |
|
|
265 | |
|
|
266 | CBOR implements some extra hints and support for JSON interoperability, |
|
|
267 | and the spec offers further guidance for conversion between CBOR and |
|
|
268 | JSON. None of this is currently implemented in CBOR, and the guidelines |
|
|
269 | in the spec do not result in correct round-tripping of data. If JSON |
|
|
270 | interoperability is improved in the future, then the goal will be to |
|
|
271 | ensure that decoded JSON data will round-trip encoding and decoding to |
|
|
272 | CBOR intact. |
218 | |
273 | |
219 | SECURITY CONSIDERATIONS |
274 | SECURITY CONSIDERATIONS |
220 | When you are using CBOR in a protocol, talking to untrusted potentially |
275 | When you are using CBOR in a protocol, talking to untrusted potentially |
221 | hostile creatures requires relatively few measures. |
276 | hostile creatures requires relatively few measures. |
222 | |
277 | |