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
41673e8f
Commit
41673e8f
authored
Dec 10, 2016
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🐛
fixed CBOR code and added test cases
parent
7e5d6af5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
118 additions
and
10 deletions
+118
-10
src/json.hpp
src/json.hpp
+4
-4
src/json.hpp.re2c
src/json.hpp.re2c
+4
-4
test/src/unit-cbor.cpp
test/src/unit-cbor.cpp
+110
-2
No files found.
src/json.hpp
View file @
41673e8f
...
...
@@ -7085,25 +7085,25 @@ class basic_json
{
idx
+=
1
;
// skip content byte
// must be uint8_t !
return
-
1
-
get_from_vector
<
uint8_t
>
(
v
,
current_idx
);
return
static_cast
<
number_integer_t
>
(
-
1
)
-
get_from_vector
<
uint8_t
>
(
v
,
current_idx
);
}
case
0x39
:
// Negative integer -1-n (two-byte uint16_t follows)
{
idx
+=
2
;
// skip 2 content bytes
return
-
1
-
get_from_vector
<
int16_t
>
(
v
,
current_idx
);
return
static_cast
<
number_integer_t
>
(
-
1
)
-
get_from_vector
<
u
int16_t
>
(
v
,
current_idx
);
}
case
0x3a
:
// Negative integer -1-n (four-byte uint32_t follows)
{
idx
+=
4
;
// skip 4 content bytes
return
-
1
-
get_from_vector
<
int32_t
>
(
v
,
current_idx
);
return
static_cast
<
number_integer_t
>
(
-
1
)
-
get_from_vector
<
u
int32_t
>
(
v
,
current_idx
);
}
case
0x3b
:
// Negative integer -1-n (eight-byte uint64_t follows)
{
idx
+=
8
;
// skip 8 content bytes
return
-
1
-
get_from_vector
<
int64_t
>
(
v
,
current_idx
);
return
static_cast
<
number_integer_t
>
(
-
1
)
-
get_from_vector
<
u
int64_t
>
(
v
,
current_idx
);
}
// UTF-8 string (0x00..0x17 bytes follow)
...
...
src/json.hpp.re2c
View file @
41673e8f
...
...
@@ -7085,25 +7085,25 @@ class basic_json
{
idx += 1; // skip content byte
// must be uint8_t !
return
-1
- get_from_vector<uint8_t>(v, current_idx);
return
static_cast<number_integer_t>(-1)
- get_from_vector<uint8_t>(v, current_idx);
}
case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
{
idx += 2; // skip 2 content bytes
return
-1 - get_from_vector<
int16_t>(v, current_idx);
return
static_cast<number_integer_t>(-1) - get_from_vector<u
int16_t>(v, current_idx);
}
case 0x3a: // Negative integer -1-n (four-byte uint32_t follows)
{
idx += 4; // skip 4 content bytes
return
-1 - get_from_vector<
int32_t>(v, current_idx);
return
static_cast<number_integer_t>(-1) - get_from_vector<u
int32_t>(v, current_idx);
}
case 0x3b: // Negative integer -1-n (eight-byte uint64_t follows)
{
idx += 8; // skip 8 content bytes
return
-1 - get_from_vector<
int64_t>(v, current_idx);
return
static_cast<number_integer_t>(-1) - get_from_vector<u
int64_t>(v, current_idx);
}
// UTF-8 string (0x00..0x17 bytes follow)
...
...
test/src/unit-cbor.cpp
View file @
41673e8f
...
...
@@ -85,9 +85,117 @@ TEST_CASE("CBOR")
{
SECTION
(
"signed"
)
{
SECTION
(
"-
65535..-25
7"
)
SECTION
(
"-
9223372036854775808..-429496729
7"
)
{
for
(
int16_t
i
=
-
65535
;
i
<=
-
257
;
++
i
)
std
::
vector
<
int64_t
>
numbers
;
numbers
.
push_back
(
INT64_MIN
);
numbers
.
push_back
(
-
1000000000000000000
);
numbers
.
push_back
(
-
100000000000000000
);
numbers
.
push_back
(
-
10000000000000000
);
numbers
.
push_back
(
-
1000000000000000
);
numbers
.
push_back
(
-
100000000000000
);
numbers
.
push_back
(
-
10000000000000
);
numbers
.
push_back
(
-
1000000000000
);
numbers
.
push_back
(
-
100000000000
);
numbers
.
push_back
(
-
10000000000
);
numbers
.
push_back
(
-
4294967297
);
for
(
auto
i
:
numbers
)
{
CAPTURE
(
i
);
// create JSON value with integer number
json
j
=
i
;
// check type
CHECK
(
j
.
is_number_integer
());
// create expected byte vector
std
::
vector
<
uint8_t
>
expected
;
expected
.
push_back
(
static_cast
<
uint8_t
>
(
0x3b
));
uint64_t
positive
=
static_cast
<
uint64_t
>
(
-
1
-
i
);
expected
.
push_back
(
static_cast
<
uint8_t
>
((
positive
>>
56
)
&
0xff
));
expected
.
push_back
(
static_cast
<
uint8_t
>
((
positive
>>
48
)
&
0xff
));
expected
.
push_back
(
static_cast
<
uint8_t
>
((
positive
>>
40
)
&
0xff
));
expected
.
push_back
(
static_cast
<
uint8_t
>
((
positive
>>
32
)
&
0xff
));
expected
.
push_back
(
static_cast
<
uint8_t
>
((
positive
>>
24
)
&
0xff
));
expected
.
push_back
(
static_cast
<
uint8_t
>
((
positive
>>
16
)
&
0xff
));
expected
.
push_back
(
static_cast
<
uint8_t
>
((
positive
>>
8
)
&
0xff
));
expected
.
push_back
(
static_cast
<
uint8_t
>
(
positive
&
0xff
));
// compare result + size
const
auto
result
=
json
::
to_cbor
(
j
);
CHECK
(
result
==
expected
);
CHECK
(
result
.
size
()
==
9
);
// check individual bytes
CHECK
(
result
[
0
]
==
0x3b
);
uint64_t
restored
=
static_cast
<
uint64_t
>
((
static_cast
<
uint64_t
>
(
result
[
1
])
<<
070
)
+
(
static_cast
<
uint64_t
>
(
result
[
2
])
<<
060
)
+
(
static_cast
<
uint64_t
>
(
result
[
3
])
<<
050
)
+
(
static_cast
<
uint64_t
>
(
result
[
4
])
<<
040
)
+
(
static_cast
<
uint64_t
>
(
result
[
5
])
<<
030
)
+
(
static_cast
<
uint64_t
>
(
result
[
6
])
<<
020
)
+
(
static_cast
<
uint64_t
>
(
result
[
7
])
<<
010
)
+
static_cast
<
uint64_t
>
(
result
[
8
]));
CHECK
(
restored
==
positive
);
CHECK
(
-
1
-
restored
==
i
);
// roundtrip
CHECK
(
json
::
from_cbor
(
result
)
==
j
);
}
}
SECTION
(
"-4294967296..-65537"
)
{
std
::
vector
<
int64_t
>
numbers
;
numbers
.
push_back
(
-
65537
);
numbers
.
push_back
(
-
100000
);
numbers
.
push_back
(
-
1000000
);
numbers
.
push_back
(
-
10000000
);
numbers
.
push_back
(
-
100000000
);
numbers
.
push_back
(
-
1000000000
);
numbers
.
push_back
(
-
4294967296
);
for
(
auto
i
:
numbers
)
{
CAPTURE
(
i
);
// create JSON value with integer number
json
j
=
i
;
// check type
CHECK
(
j
.
is_number_integer
());
// create expected byte vector
std
::
vector
<
uint8_t
>
expected
;
expected
.
push_back
(
static_cast
<
uint8_t
>
(
0x3a
));
uint32_t
positive
=
static_cast
<
uint32_t
>
(
static_cast
<
uint64_t
>
(
-
1
-
i
)
&
0x00000000ffffffff
);
expected
.
push_back
(
static_cast
<
uint8_t
>
((
positive
>>
24
)
&
0xff
));
expected
.
push_back
(
static_cast
<
uint8_t
>
((
positive
>>
16
)
&
0xff
));
expected
.
push_back
(
static_cast
<
uint8_t
>
((
positive
>>
8
)
&
0xff
));
expected
.
push_back
(
static_cast
<
uint8_t
>
(
positive
&
0xff
));
// compare result + size
const
auto
result
=
json
::
to_cbor
(
j
);
CHECK
(
result
==
expected
);
CHECK
(
result
.
size
()
==
5
);
// check individual bytes
CHECK
(
result
[
0
]
==
0x3a
);
uint32_t
restored
=
static_cast
<
uint32_t
>
((
static_cast
<
uint32_t
>
(
result
[
1
])
<<
030
)
+
(
static_cast
<
uint32_t
>
(
result
[
2
])
<<
020
)
+
(
static_cast
<
uint32_t
>
(
result
[
3
])
<<
010
)
+
static_cast
<
uint32_t
>
(
result
[
4
]));
CHECK
(
restored
==
positive
);
CHECK
(
-
1ll
-
restored
==
i
);
// roundtrip
CHECK
(
json
::
from_cbor
(
result
)
==
j
);
}
}
SECTION
(
"-65536..-257"
)
{
for
(
int32_t
i
=
-
65536
;
i
<=
-
257
;
++
i
)
{
CAPTURE
(
i
);
...
...
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