Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
d99c230f
Commit
d99c230f
authored
Dec 10, 2016
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✨
implemented indefinite-length CBOR types (#387)
parent
6b84c415
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
0 deletions
+86
-0
src/json.hpp
src/json.hpp
+35
-0
src/json.hpp.re2c
src/json.hpp.re2c
+35
-0
test/src/unit-cbor.cpp
test/src/unit-cbor.cpp
+16
-0
No files found.
src/json.hpp
View file @
d99c230f
...
...
@@ -7043,6 +7043,18 @@ class basic_json
idx
+=
len
+
8
;
// skip 8 size bytes + content bytes
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
}
else
if
(
v
[
current_idx
]
==
0x7f
)
// UTF-8 string (indefinite length)
{
std
::
string
result
;
while
(
v
[
idx
]
!=
0xff
)
{
string_t
s
=
from_cbor_internal
(
v
,
idx
);
result
+=
s
;
}
// skip break byte (0xFF)
idx
+=
1
;
return
result
;
}
else
if
(
v
[
current_idx
]
>=
0x80
and
v
[
current_idx
]
<=
0x97
)
// array
{
basic_json
result
=
value_t
::
array
;
...
...
@@ -7097,6 +7109,17 @@ class basic_json
}
return
result
;
}
else
if
(
v
[
current_idx
]
==
0x9f
)
// array (indefinite length)
{
basic_json
result
=
value_t
::
array
;
while
(
v
[
idx
]
!=
0xff
)
{
result
.
push_back
(
from_cbor_internal
(
v
,
idx
));
}
// skip break byte (0xFF)
idx
+=
1
;
return
result
;
}
else
if
(
v
[
current_idx
]
>=
0xa0
and
v
[
current_idx
]
<=
0xb7
)
// map
{
basic_json
result
=
value_t
::
object
;
...
...
@@ -7156,6 +7179,18 @@ class basic_json
}
return
result
;
}
else
if
(
v
[
current_idx
]
==
0xbf
)
// map (indefinite length)
{
basic_json
result
=
value_t
::
object
;
while
(
v
[
idx
]
!=
0xff
)
{
std
::
string
key
=
from_cbor_internal
(
v
,
idx
);
result
[
key
]
=
from_cbor_internal
(
v
,
idx
);
}
// skip break byte (0xFF)
idx
+=
1
;
return
result
;
}
else
if
(
v
[
current_idx
]
==
0xf4
)
// false
{
return
false
;
...
...
src/json.hpp.re2c
View file @
d99c230f
...
...
@@ -7043,6 +7043,18 @@ class basic_json
idx += len + 8; // skip 8 size bytes + content bytes
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
else if (v[current_idx] == 0x7f) // UTF-8 string (indefinite length)
{
std::string result;
while (v[idx] != 0xff)
{
string_t s = from_cbor_internal(v, idx);
result += s;
}
// skip break byte (0xFF)
idx += 1;
return result;
}
else if (v[current_idx] >= 0x80 and v[current_idx] <= 0x97) // array
{
basic_json result = value_t::array;
...
...
@@ -7097,6 +7109,17 @@ class basic_json
}
return result;
}
else if (v[current_idx] == 0x9f) // array (indefinite length)
{
basic_json result = value_t::array;
while (v[idx] != 0xff)
{
result.push_back(from_cbor_internal(v, idx));
}
// skip break byte (0xFF)
idx += 1;
return result;
}
else if (v[current_idx] >= 0xa0 and v[current_idx] <= 0xb7) // map
{
basic_json result = value_t::object;
...
...
@@ -7156,6 +7179,18 @@ class basic_json
}
return result;
}
else if (v[current_idx] == 0xbf) // map (indefinite length)
{
basic_json result = value_t::object;
while (v[idx] != 0xff)
{
std::string key = from_cbor_internal(v, idx);
result[key] = from_cbor_internal(v, idx);
}
// skip break byte (0xFF)
idx += 1;
return result;
}
else if (v[current_idx] == 0xf4) // false
{
return false;
...
...
test/src/unit-cbor.cpp
View file @
d99c230f
...
...
@@ -1338,6 +1338,9 @@ TEST_CASE("examples from RFC 7049 Appendix A")
CHECK
(
json
::
to_cbor
(
json
::
parse
(
"
\"\\
ud800
\\
udd51
\"
"
))
==
std
::
vector
<
uint8_t
>
({
0x64
,
0xf0
,
0x90
,
0x85
,
0x91
}));
CHECK
(
json
::
parse
(
"
\"\\
ud800
\\
udd51
\"
"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0x64
,
0xf0
,
0x90
,
0x85
,
0x91
})));
// indefinite length strings
CHECK
(
json
::
parse
(
"
\"
streaming
\"
"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0x7f
,
0x65
,
0x73
,
0x74
,
0x72
,
0x65
,
0x61
,
0x64
,
0x6d
,
0x69
,
0x6e
,
0x67
,
0xff
})));
}
SECTION
(
"arrays"
)
...
...
@@ -1353,6 +1356,14 @@ TEST_CASE("examples from RFC 7049 Appendix A")
CHECK
(
json
::
to_cbor
(
json
::
parse
(
"[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]"
))
==
std
::
vector
<
uint8_t
>
({
0x98
,
0x19
,
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
,
0x07
,
0x08
,
0x09
,
0x0a
,
0x0b
,
0x0c
,
0x0d
,
0x0e
,
0x0f
,
0x10
,
0x11
,
0x12
,
0x13
,
0x14
,
0x15
,
0x16
,
0x17
,
0x18
,
0x18
,
0x18
,
0x19
}));
CHECK
(
json
::
parse
(
"[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]"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0x98
,
0x19
,
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
,
0x07
,
0x08
,
0x09
,
0x0a
,
0x0b
,
0x0c
,
0x0d
,
0x0e
,
0x0f
,
0x10
,
0x11
,
0x12
,
0x13
,
0x14
,
0x15
,
0x16
,
0x17
,
0x18
,
0x18
,
0x18
,
0x19
})));
// indefinite length arrays
CHECK
(
json
::
parse
(
"[]"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0x9f
,
0xff
})));
CHECK
(
json
::
parse
(
"[1, [2, 3], [4, 5]] "
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0x9f
,
0x01
,
0x82
,
0x02
,
0x03
,
0x9f
,
0x04
,
0x05
,
0xff
,
0xff
})));
CHECK
(
json
::
parse
(
"[1, [2, 3], [4, 5]]"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0x9f
,
0x01
,
0x82
,
0x02
,
0x03
,
0x82
,
0x04
,
0x05
,
0xff
})));
CHECK
(
json
::
parse
(
"[1, [2, 3], [4, 5]]"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0x83
,
0x01
,
0x82
,
0x02
,
0x03
,
0x9f
,
0x04
,
0x05
,
0xff
})));
CHECK
(
json
::
parse
(
"[1, [2, 3], [4, 5]]"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0x83
,
0x01
,
0x9f
,
0x02
,
0x03
,
0xff
,
0x82
,
0x04
,
0x05
})));
CHECK
(
json
::
parse
(
"[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]"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0x9f
,
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
,
0x07
,
0x08
,
0x09
,
0x0a
,
0x0b
,
0x0c
,
0x0d
,
0x0e
,
0x0f
,
0x10
,
0x11
,
0x12
,
0x13
,
0x14
,
0x15
,
0x16
,
0x17
,
0x18
,
0x18
,
0x18
,
0x19
,
0xff
})));
}
SECTION
(
"objects"
)
...
...
@@ -1368,5 +1379,10 @@ TEST_CASE("examples from RFC 7049 Appendix A")
CHECK
(
json
::
to_cbor
(
json
::
parse
(
"{
\"
a
\"
:
\"
A
\"
,
\"
b
\"
:
\"
B
\"
,
\"
c
\"
:
\"
C
\"
,
\"
d
\"
:
\"
D
\"
,
\"
e
\"
:
\"
E
\"
}"
))
==
std
::
vector
<
uint8_t
>
({
0xa5
,
0x61
,
0x61
,
0x61
,
0x41
,
0x61
,
0x62
,
0x61
,
0x42
,
0x61
,
0x63
,
0x61
,
0x43
,
0x61
,
0x64
,
0x61
,
0x44
,
0x61
,
0x65
,
0x61
,
0x45
}));
CHECK
(
json
::
parse
(
"{
\"
a
\"
:
\"
A
\"
,
\"
b
\"
:
\"
B
\"
,
\"
c
\"
:
\"
C
\"
,
\"
d
\"
:
\"
D
\"
,
\"
e
\"
:
\"
E
\"
}"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0xa5
,
0x61
,
0x61
,
0x61
,
0x41
,
0x61
,
0x62
,
0x61
,
0x42
,
0x61
,
0x63
,
0x61
,
0x43
,
0x61
,
0x64
,
0x61
,
0x44
,
0x61
,
0x65
,
0x61
,
0x45
})));
// indefinite length objects
CHECK
(
json
::
parse
(
"{
\"
a
\"
: 1,
\"
b
\"
: [2, 3]}"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0xbf
,
0x61
,
0x61
,
0x01
,
0x61
,
0x62
,
0x9f
,
0x02
,
0x03
,
0xff
,
0xff
})));
CHECK
(
json
::
parse
(
"[
\"
a
\"
, {
\"
b
\"
:
\"
c
\"
}]"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0x82
,
0x61
,
0x61
,
0xbf
,
0x61
,
0x62
,
0x61
,
0x63
,
0xff
})));
CHECK
(
json
::
parse
(
"{
\"
Fun
\"
: true,
\"
Amt
\"
: -2}"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0xbf
,
0x63
,
0x46
,
0x75
,
0x6e
,
0xf5
,
0x63
,
0x41
,
0x6d
,
0x74
,
0x21
,
0xff
})));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment