1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
### jsoncons data model
The jsoncons data model supports the familiar JSON types - nulls,
booleans, numbers, strings, arrays, objects - plus byte strings.
Type|Description
----|-----------
null|
bool|
uint64|
int64|
half|Half precision floating point
double|Double precision floating point
string|
byte string|
array|
object|
In addition, jsoncons supports semantic tagging of values, allows it to preserve
these type semantics when parsing JSON-like data formats such as CBOR that have them.
Tag|Description
---|-----------
undefined |
datetime |
epoch_second |
epoch_milli |
epoch_nano |
bigint |
bigdec |
float128 |
bigfloat |
base16 |
base64 |
base64url |
uri |
clamped |
multi_dim_row_major |
multi_dim_column_major |
ext |
id |
regex |
code |
### Examples
#### Mappings between CBOR and jsoncons data items
CBOR data item|CBOR tag | jsoncons data item|jsoncons tag
---------------|------------------------------------------------| --------------|------------------
null |  | null |
undefined |  | null | undefined
true or false |  | bool |
unsigned or negative integer |  | int64 |
unsigned or negative integer | 1 (epoch-based date/time) | int64 | seconds
unsigned integer |  | uint64 |
unsigned integer | 1 (epoch-based date/time) | uint64 | seconds
half-precision float, float, or double |  | half |
float or double |  | double |
double | 1 (epoch-based date/time) | double | seconds
string |  | string |
byte string | 2 (positive bignum) or 2 (negative bignum) | string | bigint
array | 4 (decimal fraction) | string | bigdec
array | 5 (bigfloat) | string | bigfloat
string | 0 (date/time string) | string | datetime
string | 32 (uri) | string | uri
string | 33 (base64url) | string | base64url
string | 34 (base64) | string | base64
byte string |  | byte_string |
byte string | 21 (Expected conversion to base64url encoding) | byte_string | base64url
byte string | 22 (Expected conversion to base64 encoding) | byte_string | base64
byte string | 23 (Expected conversion to base16 encoding) | byte_string | base16
array |  | array |
map |  | object |
#### json value to CBOR item
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/cbor/cbor.hpp>
using namespace jsoncons;
int main()
{
json j(json_array_arg);
j.emplace_back("foo");
std::vector<uint8_t> bstr = {'b','a','r'};
j.emplace_back(byte_string_arg, bstr);
j.emplace_back("-18446744073709551617", semantic_tag::bigint);
j.emplace_back("273.15", semantic_tag::bigdec);
j.emplace_back("2018-10-19 12:41:07-07:00", semantic_tag::datetime);
j.emplace_back(1431027667, semantic_tag::epoch_second);
j.emplace_back(-1431027667, semantic_tag::epoch_second);
j.emplace_back(1431027667.5, semantic_tag::epoch_second);
std::cout << "(1)\n" << pretty_print(j) << "\n\n";
std::vector<uint8_t> bytes;
cbor::encode_cbor(j, bytes);
std::cout << "(2)\n" << byte_string_view(bytes) << "\n\n";
/*
88 -- Array of length 8
63 -- String value of length 3
666f6f -- "foo"
43 -- Byte string value of length 3
626172 -- 'b''a''r'
c3 -- Tag 3 (negative bignum)
49 Byte string value of length 9
010000000000000000 -- Bytes content
c4 - Tag 4 (decimal fraction)
82 -- Array of length 2
21 -- -2
19 6ab3 -- 27315
c0 -- Tag 0 (date-time)
78 19 -- Length (25)
323031382d31302d31392031323a34313a30372d30373a3030 -- "2018-10-19 12:41:07-07:00"
c1 -- Tag 1 (epoch time)
1a -- uint32_t
554bbfd3 -- 1431027667
c1
3a
554bbfd2
c1
fb
41d552eff4e00000
*/
}
```
Output
```
(1)
[
"foo",
"YmFy",
"-18446744073709551617",
"273.15",
"2018-10-19 12:41:07-07:00",
1431027667,
-1431027667,
1431027667.5
]
(2)
88,63,66,6f,6f,43,62,61,72,c3,49,01,00,00,00,00,00,00,00,00,c4,82,21,19,6a,b3,c0,78,19,32,30,31,38,2d,31,30,2d,31,39,20,31,32,3a,34,31,3a,30,37,2d,30,37,3a,30,30,c1,1a,55,4b,bf,d3,c1,3a,55,4b,bf,d2,c1,fb,41,d5,52,ef,f4,e0,00,00
```
#### CBOR item to json value
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/cbor/cbor.hpp>
using namespace jsoncons;
void main()
{
std::vector<uint8_t> bytes;
cbor::cbor_bytes_encoder encoder(bytes);
encoder.begin_array(); // indefinite length outer array
encoder.string_value("foo");
encoder.byte_string_value(byte_string{'b','a','r'});
encoder.string_value("-18446744073709551617", semantic_tag::bigint);
encoder.string_value("273.15", semantic_tag::bigdec);
encoder.string_value("2018-10-19 12:41:07-07:00", semantic_tag::datetime) ;
encoder.int64_value(1431027667, semantic_tag::epoch_second);
encoder.int64_value(-1431027667, semantic_tag::epoch_second);
encoder.double_value(1431027667.5, semantic_tag::epoch_second);
encoder.end_array();
encoder.flush();
std::cout << "(1)\n" << byte_string_view(bytes) << "\n\n";
/*
9f -- Start indefinite length array
63 -- String value of length 3
666f6f -- "foo"
43 -- Byte string value of length 3
626172 -- 'b''a''r'
c3 -- Tag 3 (negative bignum)
49 Byte string value of length 9
010000000000000000 -- Bytes content
c4 - Tag 4 (decimal fraction)
82 -- Array of length 2
21 -- -2
19 6ab3 -- 27315
c0 -- Tag 0 (date-time)
78 19 -- Length (25)
323031382d31302d31392031323a34313a30372d30373a3030 -- "2018-10-19 12:41:07-07:00"
c1 -- Tag 1 (epoch time)
1a -- uint32_t
554bbfd3 -- 1431027667
c1
3a
554bbfd2
c1
fb
41d552eff4e00000
ff -- "break"
*/
json j = cbor::decode_cbor<json>(bytes);
std::cout << "(2)\n" << pretty_print(j) << "\n\n";
```
Output:
```
(1)
9f,63,66,6f,6f,43,62,61,72,c3,49,01,00,00,00,00,00,00,00,00,c4,82,21,19,6a,b3,c0,78,19,32,30,31,38,2d,31,30,2d,31,39,20,31,32,3a,34,31,3a,30,37,2d,30,37,3a,30,30,c1,1a,55,4b,bf,d3,c1,3a,55,4b,bf,d2,c1,fb,41,d5,52,ef,f4,e0,00,00,ff
(2)
[
"foo",
"YmFy",
"-18446744073709551617",
"273.15",
"2018-10-19 12:41:07-07:00",
1431027667,
-1431027667,
1431027667.5
]
```
|