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
cf485c29
Commit
cf485c29
authored
Sep 15, 2018
by
Julian Becker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BSON: Support for arrays
parent
120d1d77
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
154 additions
and
21 deletions
+154
-21
include/nlohmann/detail/input/binary_reader.hpp
include/nlohmann/detail/input/binary_reader.hpp
+39
-10
include/nlohmann/detail/output/binary_writer.hpp
include/nlohmann/detail/output/binary_writer.hpp
+24
-0
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+63
-10
test/src/unit-bson.cpp
test/src/unit-bson.cpp
+28
-1
No files found.
include/nlohmann/detail/input/binary_reader.hpp
View file @
cf485c29
...
...
@@ -157,17 +157,8 @@ class binary_reader
return
success
;
}
bool
parse_bson_internal
()
void
parse_bson_entries
()
{
std
::
int32_t
documentSize
;
get_number_little_endian
(
documentSize
);
if
(
not
JSON_UNLIKELY
(
sax
->
start_object
(
-
1
)))
{
return
false
;
}
while
(
auto
entry_type
=
get
())
{
switch
(
entry_type
)
...
...
@@ -239,8 +230,46 @@ class binary_reader
parse_bson_internal
();
}
break
;
case
0x04
:
// array
{
string_t
key
;
get_bson_cstr
(
key
);
sax
->
key
(
key
);
parse_bson_array
();
}
break
;
}
}
}
bool
parse_bson_array
()
{
std
::
int32_t
documentSize
;
get_number_little_endian
(
documentSize
);
if
(
not
JSON_UNLIKELY
(
sax
->
start_array
(
-
1
)))
{
return
false
;
}
parse_bson_entries
();
const
auto
result
=
sax
->
end_array
();
return
result
;
}
bool
parse_bson_internal
()
{
std
::
int32_t
documentSize
;
get_number_little_endian
(
documentSize
);
if
(
not
JSON_UNLIKELY
(
sax
->
start_object
(
-
1
)))
{
return
false
;
}
parse_bson_entries
();
const
auto
result
=
sax
->
end_object
();
...
...
include/nlohmann/detail/output/binary_writer.hpp
View file @
cf485c29
...
...
@@ -787,6 +787,28 @@ class binary_writer
return
/*id*/
1ul
+
name
.
size
()
+
1ul
+
embedded_document_size
;
}
std
::
size_t
write_bson_array
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
0x04
));
// object
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
name
.
size
()
+
1u
);
auto
document_size_offset
=
oa
->
reserve_characters
(
4ul
);
std
::
int32_t
embedded_document_size
=
5ul
;
for
(
const
auto
&
el
:
*
j
.
m_value
.
array
)
{
embedded_document_size
+=
write_bson_object_entry
(
""
,
el
);
}
oa
->
write_character
(
static_cast
<
CharType
>
(
0x00
));
write_number_little_endian_at
(
document_size_offset
,
embedded_document_size
);
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
)
{
switch
(
j
.
type
())
...
...
@@ -796,6 +818,8 @@ class binary_writer
break
;
case
value_t
:
:
object
:
return
write_bson_object_internal
(
name
,
j
);
case
value_t
:
:
array
:
return
write_bson_array
(
name
,
j
);
case
value_t
:
:
boolean
:
return
write_bson_boolean
(
name
,
j
);
case
value_t
:
:
number_float
:
...
...
single_include/nlohmann/json.hpp
View file @
cf485c29
...
...
@@ -6141,17 +6141,8 @@ class binary_reader
return
success
;
}
bool
parse_bson_internal
()
{
std
::
int32_t
documentSize
;
get_number_little_endian
(
documentSize
);
if
(
not
JSON_UNLIKELY
(
sax
->
start_object
(
-
1
)))
void
parse_bson_entries
()
{
return
false
;
}
while
(
auto
entry_type
=
get
())
{
switch
(
entry_type
)
...
...
@@ -6223,9 +6214,47 @@ class binary_reader
parse_bson_internal
();
}
break
;
case
0x04
:
// array
{
string_t
key
;
get_bson_cstr
(
key
);
sax
->
key
(
key
);
parse_bson_array
();
}
break
;
}
}
}
bool
parse_bson_array
()
{
std
::
int32_t
documentSize
;
get_number_little_endian
(
documentSize
);
if
(
not
JSON_UNLIKELY
(
sax
->
start_array
(
-
1
)))
{
return
false
;
}
parse_bson_entries
();
const
auto
result
=
sax
->
end_array
();
return
result
;
}
bool
parse_bson_internal
()
{
std
::
int32_t
documentSize
;
get_number_little_endian
(
documentSize
);
if
(
not
JSON_UNLIKELY
(
sax
->
start_object
(
-
1
)))
{
return
false
;
}
parse_bson_entries
();
const
auto
result
=
sax
->
end_object
();
return
result
;
...
...
@@ -8628,6 +8657,28 @@ class binary_writer
return
/*id*/
1ul
+
name
.
size
()
+
1ul
+
embedded_document_size
;
}
std
::
size_t
write_bson_array
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
0x04
));
// object
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
name
.
size
()
+
1u
);
auto
document_size_offset
=
oa
->
reserve_characters
(
4ul
);
std
::
int32_t
embedded_document_size
=
5ul
;
for
(
const
auto
&
el
:
*
j
.
m_value
.
array
)
{
embedded_document_size
+=
write_bson_object_entry
(
""
,
el
);
}
oa
->
write_character
(
static_cast
<
CharType
>
(
0x00
));
write_number_little_endian_at
(
document_size_offset
,
embedded_document_size
);
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
)
{
switch
(
j
.
type
())
...
...
@@ -8637,6 +8688,8 @@ class binary_writer
break
;
case
value_t
:
:
object
:
return
write_bson_object_internal
(
name
,
j
);
case
value_t
:
:
array
:
return
write_bson_array
(
name
,
j
);
case
value_t
:
:
boolean
:
return
write_bson_boolean
(
name
,
j
);
case
value_t
:
:
number_float
:
...
...
test/src/unit-bson.cpp
View file @
cf485c29
...
...
@@ -354,7 +354,6 @@ TEST_CASE("BSON")
SECTION
(
"non-empty object with object member"
)
{
// directly encoding uint64 is not supported in bson (only for timestamp values)
json
j
=
{
{
"entry"
,
json
::
object
()
}
...
...
@@ -381,6 +380,34 @@ TEST_CASE("BSON")
CHECK
(
json
::
from_bson
(
result
,
true
,
false
)
==
j
);
}
SECTION
(
"non-empty object with array member"
)
{
json
j
=
{
{
"entry"
,
json
::
array
()
}
};
std
::
vector
<
uint8_t
>
expected
=
{
0x11
,
0x00
,
0x00
,
0x00
,
// size (little endian)
0x04
,
/// 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
);
}
SECTION
(
"Some more complex document"
)
{
// directly encoding uint64 is not supported in bson (only for timestamp values)
...
...
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