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
2e2cf02c
Commit
2e2cf02c
authored
Feb 19, 2020
by
Francois Chabot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
duck-typed object input adapters
parent
a0c4fc94
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
39 additions
and
42 deletions
+39
-42
binary_reader.hpp
include/nlohmann/detail/input/binary_reader.hpp
+4
-6
input_adapters.hpp
include/nlohmann/detail/input/input_adapters.hpp
+0
-0
lexer.hpp
include/nlohmann/detail/input/lexer.hpp
+4
-6
parser.hpp
include/nlohmann/detail/input/parser.hpp
+2
-3
binary_writer.hpp
include/nlohmann/detail/output/binary_writer.hpp
+1
-1
json.hpp
include/nlohmann/json.hpp
+26
-23
json.hpp
single_include/nlohmann/json.hpp
+0
-0
unit-class_lexer.cpp
test/src/unit-class_lexer.cpp
+2
-3
No files found.
include/nlohmann/detail/input/binary_reader.hpp
View file @
2e2cf02c
...
...
@@ -31,10 +31,9 @@ namespace detail
/*!
@brief deserialization of CBOR, MessagePack, and UBJSON values
*/
template
<
typename
BasicJsonType
,
typename
SAX
=
json_sax_dom_parser
<
BasicJsonType
>
,
typename
InputAdapterType
=
input_adapter_protocol
>
template
<
typename
BasicJsonType
,
typename
InputAdapterType
,
typename
SAX
=
json_sax_dom_parser
<
BasicJsonType
>
>
class
binary_reader
{
using
input_adapter_ptr_t
=
std
::
shared_ptr
<
InputAdapterType
>
;
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
...
...
@@ -47,10 +46,9 @@ class binary_reader
@param[in] adapter input adapter to read from
*/
explicit
binary_reader
(
input_adapter_ptr_t
adapter
)
:
ia
(
std
::
move
(
adapter
))
explicit
binary_reader
(
InputAdapterType
&&
adapter
)
:
ia
(
std
::
move
(
adapter
))
{
(
void
)
detail
::
is_sax_static_asserts
<
SAX
,
BasicJsonType
>
{};
assert
(
ia
);
}
// make class move-only
...
...
@@ -1810,7 +1808,7 @@ class binary_reader
int
get
()
{
++
chars_read
;
return
current
=
ia
->
get_character
();
return
current
=
ia
.
get_character
();
}
/*!
...
...
@@ -1966,7 +1964,7 @@ class binary_reader
private
:
/// input adapter
input_adapter_ptr_t
ia
=
nullptr
;
InputAdapterType
ia
;
/// the current character
int
current
=
std
::
char_traits
<
char
>::
eof
();
...
...
include/nlohmann/detail/input/input_adapters.hpp
View file @
2e2cf02c
This diff is collapsed.
Click to expand it.
include/nlohmann/detail/input/lexer.hpp
View file @
2e2cf02c
...
...
@@ -99,11 +99,9 @@ class lexer_base
This class organizes the lexical analysis during JSON deserialization.
*/
template
<
typename
BasicJsonType
,
typename
InputAdapterType
=
input_adapter_protocol
>
template
<
typename
BasicJsonType
,
typename
InputAdapterType
>
class
lexer
:
public
lexer_base
<
BasicJsonType
>
{
using
input_adapter_ptr_t
=
std
::
shared_ptr
<
InputAdapterType
>
;
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
...
...
@@ -112,7 +110,7 @@ class lexer : public lexer_base<BasicJsonType>
public
:
using
token_type
=
typename
lexer_base
<
BasicJsonType
>::
token_type
;
explicit
lexer
(
input_adapter_ptr_t
&&
adapter
)
explicit
lexer
(
InputAdapterType
&&
adapter
)
:
ia
(
std
::
move
(
adapter
)),
decimal_point_char
(
get_decimal_point
())
{}
// delete because of pointer members
...
...
@@ -1264,7 +1262,7 @@ scan_number_done:
}
else
{
current
=
ia
->
get_character
();
current
=
ia
.
get_character
();
}
if
(
JSON_HEDLEY_LIKELY
(
current
!=
std
::
char_traits
<
char
>::
eof
()))
...
...
@@ -1488,7 +1486,7 @@ scan_number_done:
private
:
/// input adapter
input_adapter_ptr_t
ia
=
nullptr
;
InputAdapterType
ia
;
/// the current character
std
::
char_traits
<
char
>::
int_type
current
=
std
::
char_traits
<
char
>::
eof
();
...
...
include/nlohmann/detail/input/parser.hpp
View file @
2e2cf02c
...
...
@@ -49,10 +49,9 @@ using parser_callback_t =
This class implements a recursive descent parser.
*/
template
<
typename
BasicJsonType
,
typename
InputAdapterType
=
input_adapter_protocol
>
template
<
typename
BasicJsonType
,
typename
InputAdapterType
>
class
parser
{
using
input_adapter_ptr_t
=
std
::
shared_ptr
<
InputAdapterType
>
;
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
...
...
@@ -62,7 +61,7 @@ class parser
public
:
/// a parser reading from an input adapter
explicit
parser
(
input_adapter_ptr_t
&&
adapter
,
explicit
parser
(
InputAdapterType
&&
adapter
,
const
parser_callback_t
<
BasicJsonType
>
cb
=
nullptr
,
const
bool
allow_exceptions_
=
true
)
:
callback
(
cb
),
m_lexer
(
std
::
move
(
adapter
)),
allow_exceptions
(
allow_exceptions_
)
...
...
include/nlohmann/detail/output/binary_writer.hpp
View file @
2e2cf02c
...
...
@@ -1326,7 +1326,7 @@ class binary_writer
private
:
/// whether we can assume little endianess
const
bool
is_little_endian
=
binary_reader
<
BasicJsonType
>::
little_endianess
();
const
bool
is_little_endian
=
binary_reader
<
BasicJsonType
,
detail
::
input_buffer_adapter
>::
little_endianess
();
/// the output
output_adapter_t
<
CharType
>
oa
=
nullptr
;
...
...
include/nlohmann/json.hpp
View file @
2e2cf02c
...
...
@@ -175,7 +175,7 @@ class basic_json
friend
class
::
nlohmann
::
detail
::
iter_impl
;
template
<
typename
BasicJsonType
,
typename
CharType
>
friend
class
::
nlohmann
::
detail
::
binary_writer
;
template
<
typename
BasicJsonType
,
typename
SAX
,
typename
InputType
>
template
<
typename
BasicJsonType
,
typename
InputType
,
typename
SAX
>
friend
class
::
nlohmann
::
detail
::
binary_reader
;
template
<
typename
BasicJsonType
>
friend
class
::
nlohmann
::
detail
::
json_sax_dom_parser
;
...
...
@@ -190,7 +190,7 @@ class basic_json
template
<
typename
InputAdapterType
>
static
::
nlohmann
::
detail
::
parser
<
basic_json
,
InputAdapterType
>
parser
(
std
::
shared_ptr
<
InputAdapterType
>
adapter
,
InputAdapterType
adapter
,
detail
::
parser_callback_t
<
basic_json
>
cb
=
nullptr
,
bool
allow_exceptions
=
true
)
...
...
@@ -210,7 +210,8 @@ class basic_json
template
<
typename
CharType
>
using
output_adapter_t
=
::
nlohmann
::
detail
::
output_adapter_t
<
CharType
>
;
using
binary_reader
=
::
nlohmann
::
detail
::
binary_reader
<
basic_json
>
;
template
<
typename
InputType
>
using
binary_reader
=
::
nlohmann
::
detail
::
binary_reader
<
basic_json
,
InputType
>
;
template
<
typename
CharType
>
using
binary_writer
=
::
nlohmann
::
detail
::
binary_writer
<
basic_json
,
CharType
>
;
using
serializer
=
::
nlohmann
::
detail
::
serializer
<
basic_json
>
;
...
...
@@ -6303,11 +6304,10 @@ class basic_json
{
assert
(
sax
);
auto
input_adapter
=
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
));
using
adapter_type
=
typename
decltype
(
input_adapter
)
::
element_type
;
auto
ia
=
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
));
return
format
==
input_format_t
::
json
?
parser
(
std
::
move
(
i
nput_adapter
)).
sax_parse
(
sax
,
strict
)
:
detail
::
binary_reader
<
basic_json
,
SAX
,
adapter_type
>
(
std
::
move
(
input_adapter
)).
sax_parse
(
format
,
sax
,
strict
);
?
parser
(
std
::
move
(
i
a
)).
sax_parse
(
sax
,
strict
)
:
detail
::
binary_reader
<
basic_json
,
decltype
(
ia
),
SAX
>
(
std
::
move
(
ia
)).
sax_parse
(
format
,
sax
,
strict
);
}
template
<
typename
SAX
>
...
...
@@ -6318,11 +6318,10 @@ class basic_json
{
assert
(
sax
);
auto
input_adapter
=
i
.
get
();
using
adapter_type
=
typename
decltype
(
input_adapter
)
::
element_type
;
auto
ia
=
i
.
get
();
return
format
==
input_format_t
::
json
?
parser
(
std
::
move
(
i
nput_adapter
)).
sax_parse
(
sax
,
strict
)
:
detail
::
binary_reader
<
basic_json
,
SAX
,
adapter_type
>
(
std
::
move
(
input_adapter
)).
sax_parse
(
format
,
sax
,
strict
);
?
parser
(
std
::
move
(
i
a
)).
sax_parse
(
sax
,
strict
)
:
detail
::
binary_reader
<
basic_json
,
decltype
(
ia
),
SAX
>
(
std
::
move
(
ia
)).
sax_parse
(
format
,
sax
,
strict
);
}
...
...
@@ -7026,7 +7025,8 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
))).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
auto
ia
=
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
));
const
bool
res
=
binary_reader
<
decltype
(
ia
)
>
(
std
::
move
(
ia
)).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
...
...
@@ -7042,7 +7042,7 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
span_input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)).
get
()).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
<
detail
::
input_buffer_adapter
>
(
detail
::
span_input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)).
get
()).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
...
...
@@ -7053,7 +7053,7 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
i
.
get
()).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
<
detail
::
input_buffer_adapter
>
(
i
.
get
()).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
...
...
@@ -7147,7 +7147,8 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
))).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
auto
ia
=
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
));
const
bool
res
=
binary_reader
<
decltype
(
ia
)
>
(
std
::
move
(
ia
)).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
...
...
@@ -7163,7 +7164,7 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
span_input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)).
get
()).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
<
detail
::
input_buffer_adapter
>
(
detail
::
span_input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)).
get
()).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
...
...
@@ -7175,7 +7176,7 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
i
.
get
()).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
<
detail
::
input_buffer_adapter
>
(
i
.
get
()).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
...
...
@@ -7249,7 +7250,8 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
))).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
auto
ia
=
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
));
const
bool
res
=
binary_reader
<
decltype
(
ia
)
>
(
std
::
move
(
ia
)).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
...
...
@@ -7265,7 +7267,7 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
span_input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)).
get
()).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
<
detail
::
input_buffer_adapter
>
(
detail
::
span_input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)).
get
()).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
...
...
@@ -7276,7 +7278,7 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
i
.
get
()).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
<
detail
::
input_buffer_adapter
>
(
i
.
get
()).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
...
...
@@ -7349,7 +7351,8 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
))).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
auto
ia
=
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
));
const
bool
res
=
binary_reader
<
decltype
(
ia
)
>
(
std
::
move
(
ia
)).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
...
...
@@ -7365,7 +7368,7 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
span_input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)).
get
()).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
<
detail
::
input_buffer_adapter
>
(
detail
::
span_input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)).
get
()).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
...
...
@@ -7376,7 +7379,7 @@ class basic_json
{
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
i
.
get
()).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
<
detail
::
input_buffer_adapter
>
(
i
.
get
()).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
/// @}
...
...
single_include/nlohmann/json.hpp
View file @
2e2cf02c
This diff is collapsed.
Click to expand it.
test/src/unit-class_lexer.cpp
View file @
2e2cf02c
...
...
@@ -39,9 +39,8 @@ namespace
// shortcut to scan a string literal
json
::
lexer
::
token_type
scan_string
(
const
char
*
s
)
{
auto
input_adapter
=
nlohmann
::
detail
::
input_adapter
(
s
);
using
input_adapter_type
=
typename
decltype
(
input_adapter
)
::
element_type
;
return
nlohmann
::
detail
::
lexer
<
json
,
input_adapter_type
>
(
std
::
move
(
input_adapter
)).
scan
();
auto
ia
=
nlohmann
::
detail
::
input_adapter
(
s
);
return
nlohmann
::
detail
::
lexer
<
json
,
decltype
(
ia
)
>
(
std
::
move
(
ia
)).
scan
();
}
}
...
...
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