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
b38ecb5c
Unverified
Commit
b38ecb5c
authored
Jul 08, 2017
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
simplified binary write
Also added some comments and improved the documentation.
parent
b3ac36db
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
26 deletions
+26
-26
src/json.hpp
src/json.hpp
+26
-26
No files found.
src/json.hpp
View file @
b38ecb5c
...
@@ -9897,22 +9897,23 @@ class basic_json
...
@@ -9897,22 +9897,23 @@ class basic_json
/*
/*
@brief read a number from the input
@brief read a number from the input
@tparam
T
the type of the number
@tparam
NumberType
the type of the number
@return number of type @a
T
@return number of type @a
NumberType
@note This function needs to respect the system's endianess, because
@note This function needs to respect the system's endianess, because
bytes in CBOR and MessagePack are stored in network order (big
bytes in CBOR and MessagePack are stored in network order (big
endian) and therefore need reordering on little endian systems.
endian) and therefore need reordering on little endian systems.
@throw parse_error.110 if input has less than `sizeof(T)` bytes
@throw parse_error.110 if input has less than `sizeof(NumberType)`
bytes
*/
*/
template
<
typename
T
>
template
<
typename
NumberType
>
T
get_number
()
NumberType
get_number
()
{
{
// step 1: read input into array with system's byte order
// step 1: read input into array with system's byte order
std
::
array
<
uint8_t
,
sizeof
(
T
)
>
vec
;
std
::
array
<
uint8_t
,
sizeof
(
NumberType
)
>
vec
;
for
(
size_t
i
=
0
;
i
<
sizeof
(
T
);
++
i
)
for
(
size_t
i
=
0
;
i
<
sizeof
(
NumberType
);
++
i
)
{
{
get
();
get
();
check_eof
();
check_eof
();
...
@@ -9920,7 +9921,7 @@ class basic_json
...
@@ -9920,7 +9921,7 @@ class basic_json
// reverse byte order prior to conversion if necessary
// reverse byte order prior to conversion if necessary
if
(
is_little_endian
)
if
(
is_little_endian
)
{
{
vec
[
sizeof
(
T
)
-
i
-
1
]
=
static_cast
<
uint8_t
>
(
current
);
vec
[
sizeof
(
NumberType
)
-
i
-
1
]
=
static_cast
<
uint8_t
>
(
current
);
}
}
else
else
{
{
...
@@ -9929,8 +9930,8 @@ class basic_json
...
@@ -9929,8 +9930,8 @@ class basic_json
}
}
// step 2: convert array into number of type T and return
// step 2: convert array into number of type T and return
T
result
;
NumberType
result
;
std
::
memcpy
(
&
result
,
vec
.
data
(),
sizeof
(
T
));
std
::
memcpy
(
&
result
,
vec
.
data
(),
sizeof
(
NumberType
));
return
result
;
return
result
;
}
}
...
@@ -9939,6 +9940,10 @@ class basic_json
...
@@ -9939,6 +9940,10 @@ class basic_json
@param[in] len number of bytes to read
@param[in] len number of bytes to read
@note We can not reserve @a len bytes for the result, because @a len
may be too large. Usually, @ref check_eof() detects the end of
the input before we run out of string memory.
@return string created by reading @a len bytes
@return string created by reading @a len bytes
@throw parse_error.110 if input has less than @a len bytes
@throw parse_error.110 if input has less than @a len bytes
...
@@ -10658,33 +10663,28 @@ class basic_json
...
@@ -10658,33 +10663,28 @@ class basic_json
/*
/*
@brief write a number to output input
@brief write a number to output input
@param[in] n number of type @a
T
@param[in] n number of type @a
NumberType
@tparam
T
the type of the number
@tparam
NumberType
the type of the number
@note This function needs to respect the system's endianess, because
@note This function needs to respect the system's endianess, because
bytes in CBOR and MessagePack are stored in network order (big
bytes in CBOR and MessagePack are stored in network order (big
endian) and therefore need reordering on little endian systems.
endian) and therefore need reordering on little endian systems.
*/
*/
template
<
typename
T
>
template
<
typename
NumberType
>
void
write_number
(
T
n
)
void
write_number
(
NumberType
n
)
{
{
// step 1: write number to array of length
T
// step 1: write number to array of length
NumberType
std
::
array
<
uint8_t
,
sizeof
(
T
)
>
vec
;
std
::
array
<
uint8_t
,
sizeof
(
NumberType
)
>
vec
;
std
::
memcpy
(
vec
.
data
(),
&
n
,
sizeof
(
T
));
std
::
memcpy
(
vec
.
data
(),
&
n
,
sizeof
(
NumberType
));
// step 2: write array to output (with possible reordering)
// step 2: write array to output (with possible reordering)
for
(
size_t
i
=
0
;
i
<
sizeof
(
T
);
++
i
)
{
// reverse byte order prior to conversion if necessary
if
(
is_little_endian
)
if
(
is_little_endian
)
{
{
oa
->
write_character
(
vec
[
sizeof
(
T
)
-
i
-
1
]);
// reverse byte order prior to conversion if necessary
}
std
::
reverse
(
vec
.
begin
(),
vec
.
end
());
else
{
oa
->
write_character
(
vec
[
i
]);
// LCOV_EXCL_LINE
}
}
}
oa
->
write_characters
(
vec
.
data
(),
sizeof
(
NumberType
));
}
}
private:
private:
...
...
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