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
0c0f2e44
Commit
0c0f2e44
authored
Sep 15, 2018
by
Julian Becker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BSON: support doubles
parent
9a0dddc5
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
118 additions
and
34 deletions
+118
-34
include/nlohmann/detail/input/binary_reader.hpp
include/nlohmann/detail/input/binary_reader.hpp
+7
-5
include/nlohmann/detail/output/binary_writer.hpp
include/nlohmann/detail/output/binary_writer.hpp
+27
-2
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+37
-4
test/src/unit-bson.cpp
test/src/unit-bson.cpp
+47
-23
No files found.
include/nlohmann/detail/input/binary_reader.hpp
View file @
0c0f2e44
...
@@ -137,8 +137,6 @@ class binary_reader
...
@@ -137,8 +137,6 @@ class binary_reader
}
}
/*!
/*!
@param[in] len the length of the array or std::size_t(-1) for an
array of indefinite size
@return whether array creation completed
@return whether array creation completed
*/
*/
bool
get_bson_str
(
string_t
&
result
)
bool
get_bson_str
(
string_t
&
result
)
...
@@ -179,15 +177,19 @@ class binary_reader
...
@@ -179,15 +177,19 @@ class binary_reader
string_t
key
;
string_t
key
;
get_bson_str
(
key
);
get_bson_str
(
key
);
sax
->
key
(
key
);
sax
->
key
(
key
);
sax
->
boolean
(
static_cast
<
bool
>
(
get
()));
double
number
;
}
break
;
get_number_little_endian
(
number
);
sax
->
number_float
(
static_cast
<
number_float_t
>
(
number
),
""
);
}
break
;
case
0x08
:
case
0x08
:
{
{
string_t
key
;
string_t
key
;
get_bson_str
(
key
);
get_bson_str
(
key
);
sax
->
key
(
key
);
sax
->
key
(
key
);
sax
->
boolean
(
static_cast
<
bool
>
(
get
()));
sax
->
boolean
(
static_cast
<
bool
>
(
get
()));
}
break
;
}
break
;
}
}
}
}
...
...
include/nlohmann/detail/output/binary_writer.hpp
View file @
0c0f2e44
...
@@ -676,8 +676,7 @@ class binary_writer
...
@@ -676,8 +676,7 @@ class binary_writer
}
}
}
}
std
::
size_t
write_bson_boolean
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
std
::
size_t
write_bson_object_entry
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
{
oa
->
write_character
(
static_cast
<
CharType
>
(
0x08
));
// boolean
oa
->
write_character
(
static_cast
<
CharType
>
(
0x08
));
// boolean
oa
->
write_characters
(
oa
->
write_characters
(
...
@@ -687,6 +686,32 @@ class binary_writer
...
@@ -687,6 +686,32 @@ class binary_writer
return
/*id*/
1ul
+
name
.
size
()
+
1u
+
/*boolean value*/
1u
;
return
/*id*/
1ul
+
name
.
size
()
+
1u
+
/*boolean value*/
1u
;
}
}
std
::
size_t
write_bson_double
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
0x01
));
// boolean
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
name
.
size
()
+
1u
);
write_number_little_endian
(
j
.
m_value
.
number_float
);
return
/*id*/
1ul
+
name
.
size
()
+
1u
+
/*double value*/
8u
;
}
std
::
size_t
write_bson_object_entry
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
switch
(
j
.
type
())
{
default:
JSON_THROW
(
type_error
::
create
(
317
,
"JSON value cannot be serialized to requested format"
));
break
;
case
value_t
:
:
boolean
:
return
write_bson_boolean
(
name
,
j
);
case
value_t
:
:
number_float
:
return
write_bson_double
(
name
,
j
);
};
return
0ul
;
}
/*!
/*!
@param[in] j JSON value to serialize
@param[in] j JSON value to serialize
@pre j.type() == value_t::object
@pre j.type() == value_t::object
...
...
single_include/nlohmann/json.hpp
View file @
0c0f2e44
...
@@ -6121,8 +6121,6 @@ class binary_reader
...
@@ -6121,8 +6121,6 @@ class binary_reader
}
}
/*!
/*!
@param[in] len the length of the array or std::size_t(-1) for an
array of indefinite size
@return whether array creation completed
@return whether array creation completed
*/
*/
bool
get_bson_str
(
string_t
&
result
)
bool
get_bson_str
(
string_t
&
result
)
...
@@ -6158,6 +6156,16 @@ class binary_reader
...
@@ -6158,6 +6156,16 @@ class binary_reader
{
{
switch
(
entry_type
)
switch
(
entry_type
)
{
{
case
0x01
:
{
string_t
key
;
get_bson_str
(
key
);
sax
->
key
(
key
);
double
number
;
get_number_little_endian
(
number
);
sax
->
number_float
(
static_cast
<
number_float_t
>
(
number
),
""
);
}
break
;
case
0x08
:
case
0x08
:
{
{
string_t
key
;
string_t
key
;
...
@@ -8461,8 +8469,7 @@ class binary_writer
...
@@ -8461,8 +8469,7 @@ class binary_writer
}
}
}
}
std
::
size_t
write_bson_boolean
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
std
::
size_t
write_bson_object_entry
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
{
oa
->
write_character
(
static_cast
<
CharType
>
(
0x08
));
// boolean
oa
->
write_character
(
static_cast
<
CharType
>
(
0x08
));
// boolean
oa
->
write_characters
(
oa
->
write_characters
(
...
@@ -8472,6 +8479,32 @@ class binary_writer
...
@@ -8472,6 +8479,32 @@ class binary_writer
return
/*id*/
1ul
+
name
.
size
()
+
1u
+
/*boolean value*/
1u
;
return
/*id*/
1ul
+
name
.
size
()
+
1u
+
/*boolean value*/
1u
;
}
}
std
::
size_t
write_bson_double
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
0x01
));
// boolean
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
name
.
size
()
+
1u
);
write_number_little_endian
(
j
.
m_value
.
number_float
);
return
/*id*/
1ul
+
name
.
size
()
+
1u
+
/*double value*/
8u
;
}
std
::
size_t
write_bson_object_entry
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
switch
(
j
.
type
())
{
default:
JSON_THROW
(
type_error
::
create
(
317
,
"JSON value cannot be serialized to requested format"
));
break
;
case
value_t
:
:
boolean
:
return
write_bson_boolean
(
name
,
j
);
case
value_t
:
:
number_float
:
return
write_bson_double
(
name
,
j
);
};
return
0ul
;
}
/*!
/*!
@param[in] j JSON value to serialize
@param[in] j JSON value to serialize
@pre j.type() == value_t::object
@pre j.type() == value_t::object
...
...
test/src/unit-bson.cpp
View file @
0c0f2e44
...
@@ -136,28 +136,52 @@ TEST_CASE("BSON")
...
@@ -136,28 +136,52 @@ TEST_CASE("BSON")
CHECK
(
json
::
from_bson
(
result
,
true
,
false
)
==
j
);
CHECK
(
json
::
from_bson
(
result
,
true
,
false
)
==
j
);
}
}
// SECTION("non-empty object with double")
SECTION
(
"non-empty object with bool"
)
// {
{
// json j =
json
j
=
// {
{
// { "entry", true }
{
"entry"
,
false
}
// };
};
// std::vector<uint8_t> expected =
std
::
vector
<
uint8_t
>
expected
=
// {
{
// 0x14, 0x00, 0x00, 0x00, // size (little endian)
0x0D
,
0x00
,
0x00
,
0x00
,
// size (little endian)
// 0x01, /// entry: double
0x08
,
// entry: boolean
// 'e', 'n', 't', 'r', 'y', '\x00',
'e'
,
'n'
,
't'
,
'r'
,
'y'
,
'\x00'
,
// 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x10, 0x40,
0x00
,
// value = false
// 0x00 // end marker
0x00
// end marker
// };
};
// const auto result = json::to_bson(j);
const
auto
result
=
json
::
to_bson
(
j
);
// CHECK(result == expected);
CHECK
(
result
==
expected
);
// // roundtrip
// roundtrip
// //CHECK(json::from_bson(result) == j);
CHECK
(
json
::
from_bson
(
result
)
==
j
);
// //CHECK(json::from_bson(result, true, false) == j);
CHECK
(
json
::
from_bson
(
result
,
true
,
false
)
==
j
);
// }
}
SECTION
(
"non-empty object with double"
)
{
json
j
=
{
{
"entry"
,
4.2
}
};
std
::
vector
<
uint8_t
>
expected
=
{
0x14
,
0x00
,
0x00
,
0x00
,
// size (little endian)
0x01
,
/// entry: double
'e'
,
'n'
,
't'
,
'r'
,
'y'
,
'\x00'
,
0xcd
,
0xcc
,
0xcc
,
0xcc
,
0xcc
,
0xcc
,
0x10
,
0x40
,
0x00
// end marker
};
const
auto
result
=
json
::
to_bson
(
j
);
CHECK
(
result
==
expected
);
// roundtrip
CHECK
(
json
::
from_bson
(
result
)
==
j
);
CHECK
(
json
::
from_bson
(
result
,
true
,
false
)
==
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