Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
6c447de0
Commit
6c447de0
authored
Sep 15, 2018
by
Julian Becker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BSON: Support objects with string members
parent
0c0f2e44
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
100 additions
and
14 deletions
+100
-14
binary_reader.hpp
include/nlohmann/detail/input/binary_reader.hpp
+19
-6
binary_writer.hpp
include/nlohmann/detail/output/binary_writer.hpp
+18
-1
json.hpp
single_include/nlohmann/json.hpp
+37
-7
unit-bson.cpp
test/src/unit-bson.cpp
+26
-0
No files found.
include/nlohmann/detail/input/binary_reader.hpp
View file @
6c447de0
...
@@ -139,7 +139,7 @@ class binary_reader
...
@@ -139,7 +139,7 @@ class binary_reader
/*!
/*!
@return whether array creation completed
@return whether array creation completed
*/
*/
bool
get_bson_str
(
string_t
&
result
)
bool
get_bson_
c
str
(
string_t
&
result
)
{
{
bool
success
=
true
;
bool
success
=
true
;
generate_until
(
std
::
back_inserter
(
result
),
[](
char
c
)
generate_until
(
std
::
back_inserter
(
result
),
[](
char
c
)
...
@@ -148,7 +148,7 @@ class binary_reader
...
@@ -148,7 +148,7 @@ class binary_reader
},
[
this
,
&
success
]
},
[
this
,
&
success
]
{
{
get
();
get
();
if
(
JSON_UNLIKELY
(
unexpect_eof
()))
if
(
JSON_UNLIKELY
(
not
unexpect_eof
()))
{
{
success
=
false
;
success
=
false
;
}
}
...
@@ -172,20 +172,33 @@ class binary_reader
...
@@ -172,20 +172,33 @@ class binary_reader
{
{
switch
(
entry_type
)
switch
(
entry_type
)
{
{
case
0x01
:
case
0x01
:
// double
{
{
string_t
key
;
string_t
key
;
get_bson_str
(
key
);
get_bson_
c
str
(
key
);
sax
->
key
(
key
);
sax
->
key
(
key
);
double
number
;
double
number
;
get_number_little_endian
(
number
);
get_number_little_endian
(
number
);
sax
->
number_float
(
static_cast
<
number_float_t
>
(
number
),
""
);
sax
->
number_float
(
static_cast
<
number_float_t
>
(
number
),
""
);
}
}
break
;
break
;
case
0x08
:
case
0x02
:
// string
{
string_t
key
;
get_bson_cstr
(
key
);
sax
->
key
(
key
);
std
::
int32_t
len
;
string_t
value
;
get_number_little_endian
(
len
);
get_string
(
len
-
1ul
,
value
);
get
();
sax
->
string
(
value
);
}
break
;
case
0x08
:
// boolean
{
{
string_t
key
;
string_t
key
;
get_bson_str
(
key
);
get_bson_
c
str
(
key
);
sax
->
key
(
key
);
sax
->
key
(
key
);
sax
->
boolean
(
static_cast
<
bool
>
(
get
()));
sax
->
boolean
(
static_cast
<
bool
>
(
get
()));
}
}
...
...
include/nlohmann/detail/output/binary_writer.hpp
View file @
6c447de0
...
@@ -688,7 +688,7 @@ class binary_writer
...
@@ -688,7 +688,7 @@ class binary_writer
std
::
size_t
write_bson_double
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
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_character
(
static_cast
<
CharType
>
(
0x01
));
//
double
oa
->
write_characters
(
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
name
.
size
()
+
1u
);
name
.
size
()
+
1u
);
...
@@ -696,6 +696,21 @@ class binary_writer
...
@@ -696,6 +696,21 @@ class binary_writer
return
/*id*/
1ul
+
name
.
size
()
+
1u
+
/*double value*/
8u
;
return
/*id*/
1ul
+
name
.
size
()
+
1u
+
/*double value*/
8u
;
}
}
std
::
size_t
write_bson_string
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
0x02
));
// string (UTF-8)
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
name
.
size
()
+
1u
);
write_number_little_endian
(
static_cast
<
std
::
int32_t
>
(
j
.
m_value
.
string
->
size
()
+
1ul
));
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
j
.
m_value
.
string
->
c_str
()),
j
.
m_value
.
string
->
size
()
+
1
);
return
/*id*/
1ul
+
name
.
size
()
+
1ul
+
sizeof
(
std
::
int32_t
)
+
j
.
m_value
.
string
->
size
()
+
1ul
;
}
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
())
...
@@ -707,6 +722,8 @@ class binary_writer
...
@@ -707,6 +722,8 @@ class binary_writer
return
write_bson_boolean
(
name
,
j
);
return
write_bson_boolean
(
name
,
j
);
case
value_t
:
:
number_float
:
case
value_t
:
:
number_float
:
return
write_bson_double
(
name
,
j
);
return
write_bson_double
(
name
,
j
);
case
value_t
:
:
string
:
return
write_bson_string
(
name
,
j
);
};
};
return
0ul
;
return
0ul
;
...
...
single_include/nlohmann/json.hpp
View file @
6c447de0
...
@@ -6123,7 +6123,7 @@ class binary_reader
...
@@ -6123,7 +6123,7 @@ class binary_reader
/*!
/*!
@return whether array creation completed
@return whether array creation completed
*/
*/
bool
get_bson_str
(
string_t
&
result
)
bool
get_bson_
c
str
(
string_t
&
result
)
{
{
bool
success
=
true
;
bool
success
=
true
;
generate_until
(
std
::
back_inserter
(
result
),
[](
char
c
)
generate_until
(
std
::
back_inserter
(
result
),
[](
char
c
)
...
@@ -6132,7 +6132,7 @@ class binary_reader
...
@@ -6132,7 +6132,7 @@ class binary_reader
},
[
this
,
&
success
]
},
[
this
,
&
success
]
{
{
get
();
get
();
if
(
JSON_UNLIKELY
(
unexpect_eof
()))
if
(
JSON_UNLIKELY
(
not
unexpect_eof
()))
{
{
success
=
false
;
success
=
false
;
}
}
...
@@ -6156,20 +6156,33 @@ class binary_reader
...
@@ -6156,20 +6156,33 @@ class binary_reader
{
{
switch
(
entry_type
)
switch
(
entry_type
)
{
{
case
0x01
:
case
0x01
:
// double
{
{
string_t
key
;
string_t
key
;
get_bson_str
(
key
);
get_bson_
c
str
(
key
);
sax
->
key
(
key
);
sax
->
key
(
key
);
double
number
;
double
number
;
get_number_little_endian
(
number
);
get_number_little_endian
(
number
);
sax
->
number_float
(
static_cast
<
number_float_t
>
(
number
),
""
);
sax
->
number_float
(
static_cast
<
number_float_t
>
(
number
),
""
);
}
}
break
;
break
;
case
0x0
8
:
case
0x0
2
:
// string
{
{
string_t
key
;
string_t
key
;
get_bson_str
(
key
);
get_bson_cstr
(
key
);
sax
->
key
(
key
);
std
::
int32_t
len
;
string_t
value
;
get_number_little_endian
(
len
);
get_string
(
len
-
1ul
,
value
);
get
();
sax
->
string
(
value
);
}
break
;
case
0x08
:
// boolean
{
string_t
key
;
get_bson_cstr
(
key
);
sax
->
key
(
key
);
sax
->
key
(
key
);
sax
->
boolean
(
static_cast
<
bool
>
(
get
()));
sax
->
boolean
(
static_cast
<
bool
>
(
get
()));
}
}
...
@@ -8481,7 +8494,7 @@ class binary_writer
...
@@ -8481,7 +8494,7 @@ class binary_writer
std
::
size_t
write_bson_double
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
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_character
(
static_cast
<
CharType
>
(
0x01
));
//
double
oa
->
write_characters
(
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
name
.
size
()
+
1u
);
name
.
size
()
+
1u
);
...
@@ -8489,6 +8502,21 @@ class binary_writer
...
@@ -8489,6 +8502,21 @@ class binary_writer
return
/*id*/
1ul
+
name
.
size
()
+
1u
+
/*double value*/
8u
;
return
/*id*/
1ul
+
name
.
size
()
+
1u
+
/*double value*/
8u
;
}
}
std
::
size_t
write_bson_string
(
const
typename
BasicJsonType
::
string_t
&
name
,
const
BasicJsonType
&
j
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
0x02
));
// string (UTF-8)
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
name
.
c_str
()),
name
.
size
()
+
1u
);
write_number_little_endian
(
static_cast
<
std
::
int32_t
>
(
j
.
m_value
.
string
->
size
()
+
1ul
));
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
j
.
m_value
.
string
->
c_str
()),
j
.
m_value
.
string
->
size
()
+
1
);
return
/*id*/
1ul
+
name
.
size
()
+
1ul
+
sizeof
(
std
::
int32_t
)
+
j
.
m_value
.
string
->
size
()
+
1ul
;
}
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
())
...
@@ -8500,6 +8528,8 @@ class binary_writer
...
@@ -8500,6 +8528,8 @@ class binary_writer
return
write_bson_boolean
(
name
,
j
);
return
write_bson_boolean
(
name
,
j
);
case
value_t
:
:
number_float
:
case
value_t
:
:
number_float
:
return
write_bson_double
(
name
,
j
);
return
write_bson_double
(
name
,
j
);
case
value_t
:
:
string
:
return
write_bson_string
(
name
,
j
);
};
};
return
0ul
;
return
0ul
;
...
...
test/src/unit-bson.cpp
View file @
6c447de0
...
@@ -183,5 +183,31 @@ TEST_CASE("BSON")
...
@@ -183,5 +183,31 @@ 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 string"
)
{
json
j
=
{
{
"entry"
,
"bsonstr"
}
};
std
::
vector
<
uint8_t
>
expected
=
{
0x18
,
0x00
,
0x00
,
0x00
,
// size (little endian)
0x02
,
/// entry: string (UTF-8)
'e'
,
'n'
,
't'
,
'r'
,
'y'
,
'\x00'
,
0x08
,
0x00
,
0x00
,
0x00
,
'b'
,
's'
,
'o'
,
'n'
,
's'
,
't'
,
'r'
,
'\x00'
,
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