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
06ccd43a
Unverified
Commit
06ccd43a
authored
Sep 10, 2019
by
Niels Lohmann
Committed by
GitHub
Sep 10, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1722 from t-b/fix-int64-min-issue
Fix int64 min issue
parents
a6bd798b
8067c3ca
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
78 additions
and
2 deletions
+78
-2
include/nlohmann/detail/output/serializer.hpp
include/nlohmann/detail/output/serializer.hpp
+27
-1
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+27
-1
test/src/unit-regression.cpp
test/src/unit-regression.cpp
+7
-0
test/src/unit-serialization.cpp
test/src/unit-serialization.cpp
+17
-0
No files found.
include/nlohmann/detail/output/serializer.hpp
View file @
06ccd43a
...
...
@@ -630,7 +630,7 @@ class serializer
if
(
is_negative
)
{
*
buffer_ptr
=
'-'
;
abs_value
=
static_cast
<
number_unsigned_t
>
(
std
::
abs
(
static_cast
<
std
::
intmax_t
>
(
x
))
);
abs_value
=
remove_sign
(
x
);
// account one more byte for the minus sign
n_chars
=
1
+
count_digits
(
abs_value
);
...
...
@@ -811,6 +811,32 @@ class serializer
return
state
;
}
/*
* Overload to make the compiler happy while it is instantiating
* dump_integer for number_unsigned_t.
* Must never be called.
*/
number_unsigned_t
remove_sign
(
number_unsigned_t
x
)
{
assert
(
false
);
// LCOV_EXCL_LINE
return
x
;
// LCOV_EXCL_LINE
}
/*
* Helper function for dump_integer
*
* This function takes a negative signed integer and returns its absolute
* value as unsigned integer. The plus/minus shuffling is necessary as we can
* not directly remove the sign of an arbitrary signed integer as the
* absolute values of INT_MIN and INT_MAX are usually not the same. See
* #1708 for details.
*/
inline
number_unsigned_t
remove_sign
(
number_integer_t
x
)
noexcept
{
assert
(
x
<
0
and
x
<
(
std
::
numeric_limits
<
number_integer_t
>::
max
)());
return
static_cast
<
number_unsigned_t
>
(
-
(
x
+
1
))
+
1
;
}
private:
/// the output of the serializer
output_adapter_t
<
char
>
o
=
nullptr
;
...
...
single_include/nlohmann/json.hpp
View file @
06ccd43a
...
...
@@ -14267,7 +14267,7 @@ class serializer
if
(
is_negative
)
{
*
buffer_ptr
=
'-'
;
abs_value
=
static_cast
<
number_unsigned_t
>
(
std
::
abs
(
static_cast
<
std
::
intmax_t
>
(
x
))
);
abs_value
=
remove_sign
(
x
);
// account one more byte for the minus sign
n_chars
=
1
+
count_digits
(
abs_value
);
...
...
@@ -14448,6 +14448,32 @@ class serializer
return
state
;
}
/*
* Overload to make the compiler happy while it is instantiating
* dump_integer for number_unsigned_t.
* Must never be called.
*/
number_unsigned_t
remove_sign
(
number_unsigned_t
x
)
{
assert
(
false
);
// LCOV_EXCL_LINE
return
x
;
// LCOV_EXCL_LINE
}
/*
* Helper function for dump_integer
*
* This function takes a negative signed integer and returns its absolute
* value as unsigned integer. The plus/minus shuffling is necessary as we can
* not directly remove the sign of an arbitrary signed integer as the
* absolute values of INT_MIN and INT_MAX are usually not the same. See
* #1708 for details.
*/
inline
number_unsigned_t
remove_sign
(
number_integer_t
x
)
noexcept
{
assert
(
x
<
0
and
x
<
(
std
::
numeric_limits
<
number_integer_t
>::
max
)());
return
static_cast
<
number_unsigned_t
>
(
-
(
x
+
1
))
+
1
;
}
private:
/// the output of the serializer
output_adapter_t
<
char
>
o
=
nullptr
;
...
...
test/src/unit-regression.cpp
View file @
06ccd43a
...
...
@@ -1802,6 +1802,13 @@ TEST_CASE("regression tests")
json
j
=
json
::
parse
(
"[-9223372036854775808]"
);
CHECK
(
j
.
dump
()
==
"[-9223372036854775808]"
);
}
SECTION
(
"issue #1708 - minimum value of int64_t can be outputted"
)
{
constexpr
auto
smallest
=
(
std
::
numeric_limits
<
int64_t
>::
min
)();
json
j
=
smallest
;
CHECK
(
j
.
dump
()
==
std
::
to_string
(
smallest
));
}
}
#if not defined(JSON_NOEXCEPTION)
...
...
test/src/unit-serialization.cpp
View file @
06ccd43a
...
...
@@ -189,3 +189,20 @@ TEST_CASE("serialization")
test
(
"[3,
\"
false
\"
,false]"
,
"[3,
\\\"
false
\\\"
,false]"
);
}
}
TEST_CASE_TEMPLATE
(
"serialization for extreme integer values"
,
T
,
int32_t
,
uint32_t
,
int64_t
,
uint64_t
)
{
SECTION
(
"minimum"
)
{
constexpr
auto
minimum
=
(
std
::
numeric_limits
<
T
>::
min
)();
json
j
=
minimum
;
CHECK
(
j
.
dump
()
==
std
::
to_string
(
minimum
));
}
SECTION
(
"maximum"
)
{
constexpr
auto
maximum
=
(
std
::
numeric_limits
<
T
>::
max
)();
json
j
=
maximum
;
CHECK
(
j
.
dump
()
==
std
::
to_string
(
maximum
));
}
}
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