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
617b3cf4
Commit
617b3cf4
authored
Feb 19, 2020
by
Francois Chabot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
templated input adapters
parent
973c52dd
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
59 additions
and
47 deletions
+59
-47
binary_reader.hpp
include/nlohmann/detail/input/binary_reader.hpp
+4
-3
input_adapters.hpp
include/nlohmann/detail/input/input_adapters.hpp
+0
-0
lexer.hpp
include/nlohmann/detail/input/lexer.hpp
+26
-18
parser.hpp
include/nlohmann/detail/input/parser.hpp
+26
-24
json.hpp
include/nlohmann/json.hpp
+0
-0
json.hpp
single_include/nlohmann/json.hpp
+0
-0
unit-class_lexer.cpp
test/src/unit-class_lexer.cpp
+3
-2
No files found.
include/nlohmann/detail/input/binary_reader.hpp
View file @
617b3cf4
...
@@ -31,9 +31,10 @@ namespace detail
...
@@ -31,9 +31,10 @@ namespace detail
/*!
/*!
@brief deserialization of CBOR, MessagePack, and UBJSON values
@brief deserialization of CBOR, MessagePack, and UBJSON values
*/
*/
template
<
typename
BasicJsonType
,
typename
SAX
=
json_sax_dom_parser
<
BasicJsonType
>>
template
<
typename
BasicJsonType
,
typename
SAX
=
json_sax_dom_parser
<
BasicJsonType
>
,
typename
InputAdapterType
=
input_adapter_protocol
>
class
binary_reader
class
binary_reader
{
{
using
input_adapter_ptr_t
=
std
::
shared_ptr
<
InputAdapterType
>
;
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
...
@@ -46,7 +47,7 @@ class binary_reader
...
@@ -46,7 +47,7 @@ class binary_reader
@param[in] adapter input adapter to read from
@param[in] adapter input adapter to read from
*/
*/
explicit
binary_reader
(
input_adapter_t
adapter
)
:
ia
(
std
::
move
(
adapter
))
explicit
binary_reader
(
input_adapter_
ptr_
t
adapter
)
:
ia
(
std
::
move
(
adapter
))
{
{
(
void
)
detail
::
is_sax_static_asserts
<
SAX
,
BasicJsonType
>
{};
(
void
)
detail
::
is_sax_static_asserts
<
SAX
,
BasicJsonType
>
{};
assert
(
ia
);
assert
(
ia
);
...
@@ -1965,7 +1966,7 @@ class binary_reader
...
@@ -1965,7 +1966,7 @@ class binary_reader
private
:
private
:
/// input adapter
/// input adapter
input_adapter_t
ia
=
nullptr
;
input_adapter_
ptr_
t
ia
=
nullptr
;
/// the current character
/// the current character
int
current
=
std
::
char_traits
<
char
>::
eof
();
int
current
=
std
::
char_traits
<
char
>::
eof
();
...
...
include/nlohmann/detail/input/input_adapters.hpp
View file @
617b3cf4
This diff is collapsed.
Click to expand it.
include/nlohmann/detail/input/lexer.hpp
View file @
617b3cf4
...
@@ -22,19 +22,9 @@ namespace detail
...
@@ -22,19 +22,9 @@ namespace detail
// lexer //
// lexer //
///////////
///////////
/*!
@brief lexical analysis
This class organizes the lexical analysis during JSON deserialization.
*/
template
<
typename
BasicJsonType
>
template
<
typename
BasicJsonType
>
class
lexer
class
lexer
_base
{
{
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
;
using
string_t
=
typename
BasicJsonType
::
string_t
;
public
:
public
:
/// token types for the parser
/// token types for the parser
enum
class
token_type
enum
class
token_type
...
@@ -75,9 +65,9 @@ class lexer
...
@@ -75,9 +65,9 @@ class lexer
return
"null literal"
;
return
"null literal"
;
case
token_type
:
:
value_string
:
case
token_type
:
:
value_string
:
return
"string literal"
;
return
"string literal"
;
case
lexer
:
:
token_type
::
value_unsigned
:
case
token_type
:
:
value_unsigned
:
case
lexer
:
:
token_type
::
value_integer
:
case
token_type
:
:
value_integer
:
case
lexer
:
:
token_type
::
value_float
:
case
token_type
:
:
value_float
:
return
"number literal"
;
return
"number literal"
;
case
token_type
:
:
begin_array
:
case
token_type
:
:
begin_array
:
return
"'['"
;
return
"'['"
;
...
@@ -103,15 +93,33 @@ class lexer
...
@@ -103,15 +93,33 @@ class lexer
// LCOV_EXCL_STOP
// LCOV_EXCL_STOP
}
}
}
}
};
/*!
@brief lexical analysis
This class organizes the lexical analysis during JSON deserialization.
*/
template
<
typename
BasicJsonType
,
typename
InputAdapterType
=
input_adapter_protocol
>
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
;
using
string_t
=
typename
BasicJsonType
::
string_t
;
public
:
using
token_type
=
typename
lexer_base
<
BasicJsonType
>::
token_type
;
explicit
lexer
(
detail
::
input_adapte
r_t
&&
adapter
)
explicit
lexer
(
input_adapter_pt
r_t
&&
adapter
)
:
ia
(
std
::
move
(
adapter
)),
decimal_point_char
(
get_decimal_point
())
{}
:
ia
(
std
::
move
(
adapter
)),
decimal_point_char
(
get_decimal_point
())
{}
// delete because of pointer members
// delete because of pointer members
lexer
(
const
lexer
&
)
=
delete
;
lexer
(
const
lexer
&
)
=
delete
;
lexer
(
lexer
&&
)
=
de
lete
;
lexer
(
lexer
&&
)
=
de
fault
;
lexer
&
operator
=
(
lexer
&
)
=
delete
;
lexer
&
operator
=
(
lexer
&
)
=
delete
;
lexer
&
operator
=
(
lexer
&&
)
=
de
lete
;
lexer
&
operator
=
(
lexer
&&
)
=
de
fault
;
~
lexer
()
=
default
;
~
lexer
()
=
default
;
private
:
private
:
...
@@ -1480,7 +1488,7 @@ scan_number_done:
...
@@ -1480,7 +1488,7 @@ scan_number_done:
private
:
private
:
/// input adapter
/// input adapter
detail
::
input_adapte
r_t
ia
=
nullptr
;
input_adapter_pt
r_t
ia
=
nullptr
;
/// the current character
/// the current character
std
::
char_traits
<
char
>::
int_type
current
=
std
::
char_traits
<
char
>::
eof
();
std
::
char_traits
<
char
>::
int_type
current
=
std
::
char_traits
<
char
>::
eof
();
...
...
include/nlohmann/detail/input/parser.hpp
View file @
617b3cf4
...
@@ -24,44 +24,46 @@ namespace detail
...
@@ -24,44 +24,46 @@ namespace detail
// parser //
// parser //
////////////
////////////
enum
class
parse_event_t
:
uint8_t
{
/// the parser read `{` and started to process a JSON object
object_start
,
/// the parser read `}` and finished processing a JSON object
object_end
,
/// the parser read `[` and started to process a JSON array
array_start
,
/// the parser read `]` and finished processing a JSON array
array_end
,
/// the parser read a key of a value in an object
key
,
/// the parser finished reading a JSON value
value
};
template
<
typename
BasicJsonType
>
using
parser_callback_t
=
std
::
function
<
bool
(
int
depth
,
parse_event_t
event
,
BasicJsonType
&
parsed
)
>
;
/*!
/*!
@brief syntax analysis
@brief syntax analysis
This class implements a recursive descent parser.
This class implements a recursive descent parser.
*/
*/
template
<
typename
BasicJsonType
>
template
<
typename
BasicJsonType
,
typename
InputAdapterType
=
input_adapter_protocol
>
class
parser
class
parser
{
{
using
input_adapter_ptr_t
=
std
::
shared_ptr
<
InputAdapterType
>
;
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
using
string_t
=
typename
BasicJsonType
::
string_t
;
using
string_t
=
typename
BasicJsonType
::
string_t
;
using
lexer_t
=
lexer
<
BasicJsonType
>
;
using
lexer_t
=
lexer
<
BasicJsonType
,
InputAdapterType
>
;
using
token_type
=
typename
lexer_t
::
token_type
;
using
token_type
=
typename
lexer_t
::
token_type
;
public
:
public
:
enum
class
parse_event_t
:
uint8_t
{
/// the parser read `{` and started to process a JSON object
object_start
,
/// the parser read `}` and finished processing a JSON object
object_end
,
/// the parser read `[` and started to process a JSON array
array_start
,
/// the parser read `]` and finished processing a JSON array
array_end
,
/// the parser read a key of a value in an object
key
,
/// the parser finished reading a JSON value
value
};
using
parser_callback_t
=
std
::
function
<
bool
(
int
depth
,
parse_event_t
event
,
BasicJsonType
&
parsed
)
>
;
/// a parser reading from an input adapter
/// a parser reading from an input adapter
explicit
parser
(
detail
::
input_adapte
r_t
&&
adapter
,
explicit
parser
(
input_adapter_pt
r_t
&&
adapter
,
const
parser_callback_t
cb
=
nullptr
,
const
parser_callback_t
<
BasicJsonType
>
cb
=
nullptr
,
const
bool
allow_exceptions_
=
true
)
const
bool
allow_exceptions_
=
true
)
:
callback
(
cb
),
m_lexer
(
std
::
move
(
adapter
)),
allow_exceptions
(
allow_exceptions_
)
:
callback
(
cb
),
m_lexer
(
std
::
move
(
adapter
)),
allow_exceptions
(
allow_exceptions_
)
{
{
...
@@ -486,7 +488,7 @@ class parser
...
@@ -486,7 +488,7 @@ class parser
private
:
private
:
/// callback function
/// callback function
const
parser_callback_t
callback
=
nullptr
;
const
parser_callback_t
<
BasicJsonType
>
callback
=
nullptr
;
/// the type of the last read token
/// the type of the last read token
token_type
last_token
=
token_type
::
uninitialized
;
token_type
last_token
=
token_type
::
uninitialized
;
/// the lexer
/// the lexer
...
...
include/nlohmann/json.hpp
View file @
617b3cf4
This diff is collapsed.
Click to expand it.
single_include/nlohmann/json.hpp
View file @
617b3cf4
This diff is collapsed.
Click to expand it.
test/src/unit-class_lexer.cpp
View file @
617b3cf4
...
@@ -37,10 +37,11 @@ using nlohmann::json;
...
@@ -37,10 +37,11 @@ using nlohmann::json;
namespace
namespace
{
{
// shortcut to scan a string literal
// shortcut to scan a string literal
json
::
lexer
::
token_type
scan_string
(
const
char
*
s
);
json
::
lexer
::
token_type
scan_string
(
const
char
*
s
)
json
::
lexer
::
token_type
scan_string
(
const
char
*
s
)
{
{
return
json
::
lexer
(
nlohmann
::
detail
::
input_adapter
(
s
)).
scan
();
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
();
}
}
}
}
...
...
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