ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.pm
(Generate patch)

Comparing CBOR-XS/XS.pm (file contents):
Revision 1.1 by root, Fri Oct 25 23:09:45 2013 UTC vs.
Revision 1.4 by root, Sat Oct 26 22:25:47 2013 UTC

32 32
33package CBOR::XS; 33package CBOR::XS;
34 34
35use common::sense; 35use common::sense;
36 36
37our $VERSION = 0.01; 37our $VERSION = 0.02;
38our @ISA = qw(Exporter); 38our @ISA = qw(Exporter);
39 39
40our @EXPORT = qw(encode_cbor decode_cbor); 40our @EXPORT = qw(encode_cbor decode_cbor);
41 41
42use Exporter; 42use Exporter;
43use XSLoader; 43use XSLoader;
44
45our $MAGIC = "\xd9\xd9\xf7";
44 46
45=head1 FUNCTIONAL INTERFACE 47=head1 FUNCTIONAL INTERFACE
46 48
47The following convenience methods are provided by this module. They are 49The following convenience methods are provided by this module. They are
48exported by default: 50exported by default:
161 163
162=head2 CBOR -> PERL 164=head2 CBOR -> PERL
163 165
164=over 4 166=over 4
165 167
168=item integers
169
170CBOR integers become (numeric) perl scalars. On perls without 64 bit
171support, 64 bit integers will be truncated or otherwise corrupted.
172
173=item byte strings
174
175Byte strings will become octet strings in Perl (the byte values 0..255
176will simply become characters of the same value in Perl).
177
178=item UTF-8 strings
179
180UTF-8 strings in CBOR will be decoded, i.e. the UTF-8 octets will be
181decoded into proper Unicode code points. At the moment, the validity of
182the UTF-8 octets will not be validated - corrupt input will result in
183corrupted Perl strings.
184
185=item arrays, maps
186
187CBOR arrays and CBOR maps will be converted into references to a Perl
188array or hash, respectively. The keys of the map will be stringified
189during this process.
190
166=item True, False 191=item true, false
167 192
168These CBOR values become C<CBOR::XS::true> and C<CBOR::XS::false>, 193These CBOR values become C<CBOR::XS::true> and C<CBOR::XS::false>,
169respectively. They are overloaded to act almost exactly like the numbers 194respectively. They are overloaded to act almost exactly like the numbers
170C<1> and C<0>. You can check whether a scalar is a CBOR boolean by using 195C<1> and C<0>. You can check whether a scalar is a CBOR boolean by using
171the C<CBOR::XS::is_bool> function. 196the C<CBOR::XS::is_bool> function.
172 197
173=item null 198=item null, undefined
174 199
175A CBOR Null value becomes C<undef> in Perl. 200CBOR null and undefined values becomes C<undef> in Perl (in the future,
201Undefined may raise an exception or something else).
202
203=item tags
204
205Tagged items consists of a numeric tag and another CBOR value. The tag
20655799 is ignored (this tag implements the magic header).
207
208All other tags are currently converted into a L<CBOR::XS::Tagged> object,
209which is simply a blessed array reference consistsing of the numeric tag
210value followed by the (decoded) BOR value.
211
212=item anything else
213
214Anything else (e.g. unsupported simple values) will raise a decoding
215error.
176 216
177=back 217=back
178 218
179 219
180=head2 PERL -> CBOR 220=head2 PERL -> CBOR
185 225
186=over 4 226=over 4
187 227
188=item hash references 228=item hash references
189 229
190Perl hash references become CBOR maps. As there is no inherent ordering 230Perl hash references become CBOR maps. As there is no inherent ordering in
191in hash keys (or CBOR maps), they will usually be encoded in a 231hash keys (or CBOR maps), they will usually be encoded in a pseudo-random
192pseudo-random order. 232order.
233
234Currently, tied hashes will use the indefinite-length format, while normal
235hashes will use the fixed-length format.
193 236
194=item array references 237=item array references
195 238
196Perl array references become CBOR arrays. 239Perl array references become fixed-length CBOR arrays.
197 240
198=item other references 241=item other references
199 242
200Other unblessed references are generally not allowed and will cause an 243Other unblessed references are generally not allowed and will cause an
201exception to be thrown, except for references to the integers C<0> and 244exception to be thrown, except for references to the integers C<0> and
202C<1>, which get turned into C<False> and C<True> in CBOR. 245C<1>, which get turned into false and true in CBOR.
246
247=item CBOR::XS::Tagged objects
248
249Objects of this type must be arrays consisting of a single C<[tag, value]>
250pair. The (numerical) tag will be encoded as a CBOR tag, the value will be
251encoded as appropriate for the value.
203 252
204=item CBOR::XS::true, CBOR::XS::false 253=item CBOR::XS::true, CBOR::XS::false
205 254
206These special values become CBOR True and CBOR False values, 255These special values become CBOR true and CBOR false values,
207respectively. You can also use C<\1> and C<\0> directly if you want. 256respectively. You can also use C<\1> and C<\0> directly if you want.
208 257
209=item blessed objects 258=item blessed objects
210 259
211Blessed objects are not directly representable in CBOR. TODO 260Other blessed objects currently need to have a C<TO_CBOR> method. It
212See the 261will be called on every object that is being serialised, and must return
213C<allow_blessed> and C<convert_blessed> methods on various options on 262something that can be encoded in CBOR.
214how to deal with this: basically, you can choose between throwing an
215exception, encoding the reference as if it weren't blessed, or provide
216your own serialiser method.
217 263
218=item simple scalars 264=item simple scalars
219 265
220TODO 266TODO
221Simple Perl scalars (any scalar that is not a reference) are the most 267Simple Perl scalars (any scalar that is not a reference) are the most
222difficult objects to encode: CBOR::XS will encode undefined scalars as 268difficult objects to encode: CBOR::XS will encode undefined scalars as
223CBOR C<Null> values, scalars that have last been used in a string context 269CBOR null values, scalars that have last been used in a string context
224before encoding as CBOR strings, and anything else as number value: 270before encoding as CBOR strings, and anything else as number value:
225 271
226 # dump as number 272 # dump as number
227 encode_cbor [2] # yields [2] 273 encode_cbor [2] # yields [2]
228 encode_cbor [-3.0e17] # yields [-3e+17] 274 encode_cbor [-3.0e17] # yields [-3e+17]
250 296
251You can not currently force the type in other, less obscure, ways. Tell me 297You can not currently force the type in other, less obscure, ways. Tell me
252if you need this capability (but don't forget to explain why it's needed 298if you need this capability (but don't forget to explain why it's needed
253:). 299:).
254 300
255Note that numerical precision has the same meaning as under Perl (so 301Perl values that seem to be integers generally use the shortest possible
256binary to decimal conversion follows the same rules as in Perl, which 302representation. Floating-point values will use either the IEEE single
257can differ to other languages). Also, your perl interpreter might expose 303format if possible without loss of precision, otherwise the IEEE double
258extensions to the floating point numbers of your platform, such as 304format will be used. Perls that use formats other than IEEE double to
259infinities or NaN's - these cannot be represented in CBOR, and it is an 305represent numerical values are supported, but might suffer loss of
260error to pass those in. 306precision.
261 307
262=back 308=back
263 309
264 310
311=head2 MAGIC HEADER
312
313There is no way to distinguish CBOR from other formats
314programmatically. To make it easier to distinguish CBOR from other
315formats, the CBOR specification has a special "magic string" that can be
316prepended to any CBOR string without changing it's meaning.
317
318This string is available as C<$CBOR::XS::MAGIC>. This module does not
319prepend this string tot he CBOR data it generates, but it will ignroe it
320if present, so users can prepend this string as a "file type" indicator as
321required.
322
323
265=head2 CBOR and JSON 324=head2 CBOR and JSON
266 325
267TODO 326CBOR is supposed to implement a superset of the JSON data model, and is,
327with some coercion, able to represent all JSON texts (something that other
328"binary JSON" formats such as BSON generally do not support).
329
330CBOR implements some extra hints and support for JSON interoperability,
331and the spec offers further guidance for conversion between CBOR and
332JSON. None of this is currently implemented in CBOR, and the guidelines
333in the spec do not result in correct round-tripping of data. If JSON
334interoperability is improved in the future, then the goal will be to
335ensure that decoded JSON data will round-trip encoding and decoding to
336CBOR intact.
268 337
269 338
270=head1 SECURITY CONSIDERATIONS 339=head1 SECURITY CONSIDERATIONS
271 340
272When you are using CBOR in a protocol, talking to untrusted potentially 341When you are using CBOR in a protocol, talking to untrusted potentially

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines