@@ -5,36 +5,15 @@ and has no external dependencies.
55
66[ cbor ] : https://cbor.io/
77
8- ## Getting started
9-
10- To get started you need to import ` CborEncoder ` and ` CborDecoder ` classes like
11- this:
12-
13- ``` ts
14- import {CborEncoder } from ' json-joy/es2020/json-pack/cbor/CborEncoder' ;
15- import {CborDecoder } from ' json-joy/es2020/json-pack/cbor/CborDecoder' ;
16- ```
17-
18- The ` CborDecoder ` implements full decoding feature set including advanced
19- features like value skipping and decoding one level at-a-time. Those features
20- are not necessary for most use cases, to save on bundle size you can import
21- the "base" decoder instead:
8+ ## Basic Usage
229
2310``` ts
24- import {CborDecoderBase } from ' json-joy/es2020/json-pack/cbor/CborDecoderBase' ;
25- ```
26-
27- The base decoder implements all CBOR decoding features except for the advanced
28- shallow decoding features, like skipping, one level at-a-time decoding.
29-
30- ## Usage
11+ import {CborEncoder , CborDecoder } from ' @jsonjoy.com/json-pack/lib/cbor' ;
3112
32- Encode a JavaScript POJO to CBOR:
33-
34- ``` ts
3513const encoder = new CborEncoder ();
14+ const decoder = new CborDecoder ();
3615
37- const pojo = {
16+ const data = {
3817 id: 123 ,
3918 foo: ' bar' ,
4019 tags: [' a' , ' b' , ' c' ],
@@ -47,39 +26,34 @@ const pojo = {
4726 },
4827};
4928
50- const encoded = encoder .encode (pojo );
51- console .log (encoded );
52- // Uint8Array(53) [
53- // 164, 98, 105, 100, 24, 123, 99, 102, 111, 111,
54- // 99, 98, 97, 114, 100, 116, 97, 103, 115, 131,
55- // 97, 97, 97, 98, 97, 99, 120, 6, 110, 101,
56- // 115, 116, 101, 100, 163, 97, 97, 1, 97, 98,
57- // 2, 120, 6, 108, 101, 118, 101, 108, 50, 161,
58- // 97, 99, 3
59- // ]
29+ const encoded = encoder .encode (data );
30+ const decoded = decoder .decode (encoded );
31+
32+ console .log (decoded ); // Original data structure
6033```
6134
62- Decode CBOR back to JavaScript POJO:
35+ ## Advanced Usage
36+
37+ To get started you need to import ` CborEncoder ` and ` CborDecoder ` classes like
38+ this:
6339
6440``` ts
65- const decoderBase = new CborDecoderBase ();
66- const decoded = decoderBase .read (encoded );
41+ import {CborEncoder } from ' @jsonjoy.com/json-pack/lib/cbor/CborEncoder' ;
42+ import {CborDecoder } from ' @jsonjoy.com/json-pack/lib/cbor/CborDecoder' ;
43+ ```
44+
45+ The ` CborDecoder ` implements full decoding feature set including advanced
46+ features like value skipping and decoding one level at-a-time. Those features
47+ are not necessary for most use cases, to save on bundle size you can import
48+ the "base" decoder instead:
6749
68- console .log (decoded );
69- // {
70- // id: 123,
71- // foo: 'bar',
72- // tags: ['a', 'b', 'c'],
73- // nested: {
74- // a: 1,
75- // b: 2,
76- // level2: {
77- // c: 3,
78- // }
79- // },
80- // }
50+ ``` ts
51+ import {CborDecoderBase } from ' @jsonjoy.com/json-pack/lib/cbor/CborDecoderBase' ;
8152```
8253
54+ The base decoder implements all CBOR decoding features except for the advanced
55+ shallow decoding features, like skipping, one level at-a-time decoding.
56+
8357## Implementation details
8458
8559- Map keys are treated as strings.
0 commit comments