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
44fe284f
Commit
44fe284f
authored
May 13, 2020
by
chenguoping
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enhace to_cbor() to support +/-Infinity, NaN, and single-precision float
parent
a414e359
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
6 deletions
+58
-6
include/nlohmann/detail/output/binary_writer.hpp
include/nlohmann/detail/output/binary_writer.hpp
+28
-2
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+1
-1
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+29
-3
No files found.
include/nlohmann/detail/output/binary_writer.hpp
View file @
44fe284f
...
...
@@ -6,6 +6,7 @@
#include <cstring> // memcpy
#include <limits> // numeric_limits
#include <string> // string
#include <cmath> // isnan, isinf
#include <nlohmann/detail/input/binary_reader.hpp>
#include <nlohmann/detail/macro_scope.hpp>
...
...
@@ -177,8 +178,33 @@ class binary_writer
case
value_t
:
:
number_float
:
{
oa
->
write_character
(
get_cbor_float_prefix
(
j
.
m_value
.
number_float
));
write_number
(
j
.
m_value
.
number_float
);
if
(
std
::
isnan
(
j
.
m_value
.
number_float
))
{
// NaN is 0xf97e00 in CBOR
oa
->
write_character
(
to_char_type
(
0xF9
));
oa
->
write_character
(
to_char_type
(
0x7E
));
oa
->
write_character
(
to_char_type
(
0x00
));
}
else
if
(
std
::
isinf
(
j
.
m_value
.
number_float
))
{
// Infinity is 0xf97c00, -Infinity is 0xf9fc00
oa
->
write_character
(
to_char_type
(
0xf9
));
oa
->
write_character
(
j
.
m_value
.
number_float
>
0
?
to_char_type
(
0x7C
)
:
to_char_type
(
0xFC
));
oa
->
write_character
(
to_char_type
(
0x00
));
}
else
{
if
(
static_cast
<
double
>
(
static_cast
<
float
>
(
j
.
m_value
.
number_float
))
==
j
.
m_value
.
number_float
)
{
oa
->
write_character
(
get_cbor_float_prefix
(
static_cast
<
float
>
(
j
.
m_value
.
number_float
)));
write_number
(
static_cast
<
float
>
(
j
.
m_value
.
number_float
));
}
else
{
oa
->
write_character
(
get_cbor_float_prefix
(
j
.
m_value
.
number_float
));
write_number
(
j
.
m_value
.
number_float
);
}
}
break
;
}
...
...
include/nlohmann/json.hpp
View file @
44fe284f
...
...
@@ -7059,7 +7059,7 @@ class basic_json
- break (0xFF)
@param[in] j JSON value to serialize
@return
MessagePack
serialization as byte vector
@return
CBOR
serialization as byte vector
@complexity Linear in the size of the JSON value @a j.
...
...
single_include/nlohmann/json.hpp
View file @
44fe284f
...
...
@@ -11828,6 +11828,7 @@ class json_ref
#include <cstring> // memcpy
#include <limits> // numeric_limits
#include <string> // string
#include <cmath> // isnan, isinf
// #include <nlohmann/detail/input/binary_reader.hpp>
...
...
@@ -12126,8 +12127,33 @@ class binary_writer
case
value_t
:
:
number_float
:
{
oa
->
write_character
(
get_cbor_float_prefix
(
j
.
m_value
.
number_float
));
write_number
(
j
.
m_value
.
number_float
);
if
(
std
::
isnan
(
j
.
m_value
.
number_float
))
{
// NaN is 0xf97e00 in CBOR
oa
->
write_character
(
to_char_type
(
0xF9
));
oa
->
write_character
(
to_char_type
(
0x7E
));
oa
->
write_character
(
to_char_type
(
0x00
));
}
else
if
(
std
::
isinf
(
j
.
m_value
.
number_float
))
{
// Infinity is 0xf97c00, -Infinity is 0xf9fc00
oa
->
write_character
(
to_char_type
(
0xf9
));
oa
->
write_character
(
j
.
m_value
.
number_float
>
0
?
to_char_type
(
0x7C
)
:
to_char_type
(
0xFC
));
oa
->
write_character
(
to_char_type
(
0x00
));
}
else
{
if
(
static_cast
<
double
>
(
static_cast
<
float
>
(
j
.
m_value
.
number_float
))
==
j
.
m_value
.
number_float
)
{
oa
->
write_character
(
get_cbor_float_prefix
(
static_cast
<
float
>
(
j
.
m_value
.
number_float
)));
write_number
(
static_cast
<
float
>
(
j
.
m_value
.
number_float
));
}
else
{
oa
->
write_character
(
get_cbor_float_prefix
(
j
.
m_value
.
number_float
));
write_number
(
j
.
m_value
.
number_float
);
}
}
break
;
}
...
...
@@ -22507,7 +22533,7 @@ class basic_json
- break (0xFF)
@param[in] j JSON value to serialize
@return
MessagePack
serialization as byte vector
@return
CBOR
serialization as byte vector
@complexity Linear in the size of the JSON value @a j.
...
...
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