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
5ce7d6bd
Commit
5ce7d6bd
authored
7 years ago
by
Julian Becker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BSON: support objects with objects as members
parent
83b427ad
develop
bon8
coverity_scan
feature/optional
gcc_warning_flags
icpc
issue2228
issue2615
issue2932
issue3232_use_catch
master
release/3.10.2
release/3.10.3
release/3.10.4
release/3.10.5
string_view
string_view2
update_doctest
3.6.1
v3.10.5
v3.10.4
v3.10.3
v3.10.2
v3.10.1
v3.10.0
v3.9.1
v3.9.0
v3.8.0
v3.7.3
v3.7.2
v3.7.1
v3.7.0
v3.6.1
v3.6.0
v3.5.0
v3.4.0
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
4 deletions
+77
-4
include/nlohmann/detail/input/binary_reader.hpp
include/nlohmann/detail/input/binary_reader.hpp
+8
-1
include/nlohmann/detail/output/binary_writer.hpp
include/nlohmann/detail/output/binary_writer.hpp
+16
-1
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+24
-2
test/src/unit-bson.cpp
test/src/unit-bson.cpp
+29
-0
No files found.
include/nlohmann/detail/input/binary_reader.hpp
View file @
5ce7d6bd
...
@@ -231,10 +231,17 @@ class binary_reader
...
@@ -231,10 +231,17 @@ class binary_reader
sax
->
null
();
sax
->
null
();
}
}
break
;
break
;
case
0x03
:
// object
{
string_t
key
;
get_bson_cstr
(
key
);
sax
->
key
(
key
);
parse_bson_internal
();
}
break
;
}
}
}
}
get
();
const
auto
result
=
sax
->
end_object
();
const
auto
result
=
sax
->
end_object
();
return
result
;
return
result
;
...
...
This diff is collapsed.
Click to expand it.
include/nlohmann/detail/output/binary_writer.hpp
View file @
5ce7d6bd
...
@@ -775,6 +775,18 @@ class binary_writer
...
@@ -775,6 +775,18 @@ class binary_writer
}
}
}
}
std
::
size_t
write_bson_object_internal
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
0x03
));
// object
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
name
.
size
()
+
1u
);
auto
const
embedded_document_size
=
write_bson_object
(
j
);
return
/*id*/
1ul
+
name
.
size
()
+
1ul
+
embedded_document_size
;
}
std
::
size_t
write_bson_object_entry
(
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
)
{
{
switch
(
j
.
type
())
switch
(
j
.
type
())
...
@@ -782,6 +794,8 @@ class binary_writer
...
@@ -782,6 +794,8 @@ class binary_writer
default:
default:
JSON_THROW
(
type_error
::
create
(
317
,
"JSON value cannot be serialized to requested format"
));
JSON_THROW
(
type_error
::
create
(
317
,
"JSON value cannot be serialized to requested format"
));
break
;
break
;
case
value_t
:
:
object
:
return
write_bson_object_internal
(
name
,
j
);
case
value_t
:
:
boolean
:
case
value_t
:
:
boolean
:
return
write_bson_boolean
(
name
,
j
);
return
write_bson_boolean
(
name
,
j
);
case
value_t
:
:
number_float
:
case
value_t
:
:
number_float
:
...
@@ -803,7 +817,7 @@ class binary_writer
...
@@ -803,7 +817,7 @@ class binary_writer
@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
*/
*/
void
write_bson_object
(
const
BasicJsonType
&
j
)
std
::
size_t
write_bson_object
(
const
BasicJsonType
&
j
)
{
{
assert
(
j
.
type
()
==
value_t
::
object
);
assert
(
j
.
type
()
==
value_t
::
object
);
auto
document_size_offset
=
oa
->
reserve_characters
(
4ul
);
auto
document_size_offset
=
oa
->
reserve_characters
(
4ul
);
...
@@ -816,6 +830,7 @@ class binary_writer
...
@@ -816,6 +830,7 @@ class binary_writer
oa
->
write_character
(
static_cast
<
CharType
>
(
0x00
));
oa
->
write_character
(
static_cast
<
CharType
>
(
0x00
));
write_number_little_endian_at
(
document_size_offset
,
document_size
);
write_number_little_endian_at
(
document_size_offset
,
document_size
);
return
document_size
;
}
}
/*!
/*!
...
...
This diff is collapsed.
Click to expand it.
single_include/nlohmann/json.hpp
View file @
5ce7d6bd
...
@@ -6215,10 +6215,17 @@ class binary_reader
...
@@ -6215,10 +6215,17 @@ class binary_reader
sax
->
null
();
sax
->
null
();
}
}
break
;
break
;
case
0x03
:
// object
{
string_t
key
;
get_bson_cstr
(
key
);
sax
->
key
(
key
);
parse_bson_internal
();
}
break
;
}
}
}
}
get
();
const
auto
result
=
sax
->
end_object
();
const
auto
result
=
sax
->
end_object
();
return
result
;
return
result
;
...
@@ -8609,6 +8616,18 @@ class binary_writer
...
@@ -8609,6 +8616,18 @@ class binary_writer
}
}
}
}
std
::
size_t
write_bson_object_internal
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
0x03
));
// object
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
name
.
size
()
+
1u
);
auto
const
embedded_document_size
=
write_bson_object
(
j
);
return
/*id*/
1ul
+
name
.
size
()
+
1ul
+
embedded_document_size
;
}
std
::
size_t
write_bson_object_entry
(
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
)
{
{
switch
(
j
.
type
())
switch
(
j
.
type
())
...
@@ -8616,6 +8635,8 @@ class binary_writer
...
@@ -8616,6 +8635,8 @@ class binary_writer
default:
default:
JSON_THROW
(
type_error
::
create
(
317
,
"JSON value cannot be serialized to requested format"
));
JSON_THROW
(
type_error
::
create
(
317
,
"JSON value cannot be serialized to requested format"
));
break
;
break
;
case
value_t
:
:
object
:
return
write_bson_object_internal
(
name
,
j
);
case
value_t
:
:
boolean
:
case
value_t
:
:
boolean
:
return
write_bson_boolean
(
name
,
j
);
return
write_bson_boolean
(
name
,
j
);
case
value_t
:
:
number_float
:
case
value_t
:
:
number_float
:
...
@@ -8637,7 +8658,7 @@ class binary_writer
...
@@ -8637,7 +8658,7 @@ class binary_writer
@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
*/
*/
void
write_bson_object
(
const
BasicJsonType
&
j
)
std
::
size_t
write_bson_object
(
const
BasicJsonType
&
j
)
{
{
assert
(
j
.
type
()
==
value_t
::
object
);
assert
(
j
.
type
()
==
value_t
::
object
);
auto
document_size_offset
=
oa
->
reserve_characters
(
4ul
);
auto
document_size_offset
=
oa
->
reserve_characters
(
4ul
);
...
@@ -8650,6 +8671,7 @@ class binary_writer
...
@@ -8650,6 +8671,7 @@ class binary_writer
oa
->
write_character
(
static_cast
<
CharType
>
(
0x00
));
oa
->
write_character
(
static_cast
<
CharType
>
(
0x00
));
write_number_little_endian_at
(
document_size_offset
,
document_size
);
write_number_little_endian_at
(
document_size_offset
,
document_size
);
return
document_size
;
}
}
/*!
/*!
...
...
This diff is collapsed.
Click to expand it.
test/src/unit-bson.cpp
View file @
5ce7d6bd
...
@@ -351,5 +351,34 @@ TEST_CASE("BSON")
...
@@ -351,5 +351,34 @@ TEST_CASE("BSON")
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 object member"
)
{
// directly encoding uint64 is not supported in bson (only for timestamp values)
json
j
=
{
{
"entry"
,
json
::
object
()
}
};
std
::
vector
<
uint8_t
>
expected
=
{
0x11
,
0x00
,
0x00
,
0x00
,
// size (little endian)
0x03
,
/// entry: embedded document
'e'
,
'n'
,
't'
,
'r'
,
'y'
,
'\x00'
,
0x05
,
0x00
,
0x00
,
0x00
,
// size (little endian)
// no entries
0x00
,
// end marker (embedded document)
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
);
}
}
}
}
}
This diff is collapsed.
Click to expand it.
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