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
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
567 additions
and
254 deletions
+567
-254
binary_reader.hpp
include/nlohmann/detail/input/binary_reader.hpp
+4
-3
input_adapters.hpp
include/nlohmann/detail/input/input_adapters.hpp
+102
-53
lexer.hpp
include/nlohmann/detail/input/lexer.hpp
+26
-18
parser.hpp
include/nlohmann/detail/input/parser.hpp
+24
-22
json.hpp
include/nlohmann/json.hpp
+126
-30
json.hpp
single_include/nlohmann/json.hpp
+282
-126
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
...
@@ -45,14 +45,11 @@ struct input_adapter_protocol
...
@@ -45,14 +45,11 @@ struct input_adapter_protocol
virtual
~
input_adapter_protocol
()
=
default
;
virtual
~
input_adapter_protocol
()
=
default
;
};
};
/// a type to simplify interfaces
using
input_adapter_t
=
std
::
shared_ptr
<
input_adapter_protocol
>
;
/*!
/*!
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
buffer. This adapter is a very low level adapter.
buffer. This adapter is a very low level adapter.
*/
*/
class
file_input_adapter
:
public
input_adapter_protocol
class
file_input_adapter
final
:
public
input_adapter_protocol
{
{
public
:
public
:
JSON_HEDLEY_NON_NULL
(
2
)
JSON_HEDLEY_NON_NULL
(
2
)
...
@@ -87,7 +84,7 @@ characters following those used in parsing the JSON input. Clears the
...
@@ -87,7 +84,7 @@ characters following those used in parsing the JSON input. Clears the
std::istream flags; any input errors (e.g., EOF) will be detected by the first
std::istream flags; any input errors (e.g., EOF) will be detected by the first
subsequent call for input from the std::istream.
subsequent call for input from the std::istream.
*/
*/
class
input_stream_adapter
:
public
input_adapter_protocol
class
input_stream_adapter
final
:
public
input_adapter_protocol
{
{
public
:
public
:
~
input_stream_adapter
()
override
~
input_stream_adapter
()
override
...
@@ -128,7 +125,7 @@ class input_stream_adapter : public input_adapter_protocol
...
@@ -128,7 +125,7 @@ class input_stream_adapter : public input_adapter_protocol
};
};
/// input adapter for buffer input
/// input adapter for buffer input
class
input_buffer_adapter
:
public
input_adapter_protocol
class
input_buffer_adapter
final
:
public
input_adapter_protocol
{
{
public
:
public
:
input_buffer_adapter
(
const
char
*
b
,
const
std
::
size_t
l
)
noexcept
input_buffer_adapter
(
const
char
*
b
,
const
std
::
size_t
l
)
noexcept
...
@@ -285,7 +282,7 @@ struct wide_string_input_helper<WideStringType, 2>
...
@@ -285,7 +282,7 @@ struct wide_string_input_helper<WideStringType, 2>
};
};
template
<
typename
WideStringType
>
template
<
typename
WideStringType
>
class
wide_string_input_adapter
:
public
input_adapter_protocol
class
wide_string_input_adapter
final
:
public
input_adapter_protocol
{
{
public
:
public
:
explicit
wide_string_input_adapter
(
const
WideStringType
&
w
)
noexcept
explicit
wide_string_input_adapter
(
const
WideStringType
&
w
)
noexcept
...
@@ -331,60 +328,50 @@ class wide_string_input_adapter : public input_adapter_protocol
...
@@ -331,60 +328,50 @@ class wide_string_input_adapter : public input_adapter_protocol
std
::
size_t
utf8_bytes_filled
=
0
;
std
::
size_t
utf8_bytes_filled
=
0
;
};
};
class
input_adapter
inline
std
::
shared_ptr
<
file_input_adapter
>
input_adapter
(
std
::
FILE
*
file
)
{
{
public
:
return
std
::
make_shared
<
file_input_adapter
>
(
file
);
// native support
}
JSON_HEDLEY_NON_NULL
(
2
)
input_adapter
(
std
::
FILE
*
file
)
:
ia
(
std
::
make_shared
<
file_input_adapter
>
(
file
))
{}
/// input adapter for input stream
input_adapter
(
std
::
istream
&
i
)
:
ia
(
std
::
make_shared
<
input_stream_adapter
>
(
i
))
{}
/// input adapter for input stream
input_adapter
(
std
::
istream
&&
i
)
:
ia
(
std
::
make_shared
<
input_stream_adapter
>
(
i
))
{}
input_adapter
(
const
std
::
wstring
&
ws
)
inline
std
::
shared_ptr
<
input_stream_adapter
>
input_adapter
(
std
::
istream
&
stream
)
:
ia
(
std
::
make_shared
<
wide_string_input_adapter
<
std
::
wstring
>>
(
ws
))
{}
{
return
std
::
make_shared
<
input_stream_adapter
>
(
stream
);
input_adapter
(
const
std
::
u16string
&
ws
)
}
:
ia
(
std
::
make_shared
<
wide_string_input_adapter
<
std
::
u16string
>>
(
ws
))
{}
input_adapter
(
const
std
::
u32string
&
ws
)
inline
std
::
shared_ptr
<
input_stream_adapter
>
input_adapter
(
std
::
istream
&&
stream
)
:
ia
(
std
::
make_shared
<
wide_string_input_adapter
<
std
::
u32string
>>
(
ws
))
{}
{
return
std
::
make_shared
<
input_stream_adapter
>
(
stream
);
}
/// input adapter for buffer
template
<
typename
CharT
,
template
<
typename
CharT
,
typename
std
::
enable_if
<
typename
std
::
enable_if
<
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
int
>::
type
=
0
>
int
>::
type
=
0
>
input_adapter
(
CharT
b
,
std
::
size_t
l
)
std
::
shared_ptr
<
input_buffer_adapter
>
input_adapter
(
CharT
b
,
std
::
size_t
l
)
:
ia
(
std
::
make_shared
<
input_buffer_adapter
>
(
reinterpret_cast
<
const
char
*>
(
b
),
l
))
{}
{
return
std
::
make_shared
<
input_buffer_adapter
>
(
reinterpret_cast
<
const
char
*>
(
b
),
l
);
// derived support
}
/// input adapter for string literal
template
<
typename
CharT
,
template
<
typename
CharT
,
typename
std
::
enable_if
<
typename
std
::
enable_if
<
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
int
>::
type
=
0
>
int
>::
type
=
0
>
input_adapter
(
CharT
b
)
std
::
shared_ptr
<
input_buffer_adapter
>
input_adapter
(
CharT
b
)
:
input_adapter
(
reinterpret_cast
<
const
char
*>
(
b
),
{
std
::
strlen
(
reinterpret_cast
<
const
char
*>
(
b
)))
{}
return
input_adapter
(
reinterpret_cast
<
const
char
*>
(
b
),
std
::
strlen
(
reinterpret_cast
<
const
char
*>
(
b
)));
}
/// input adapter for iterator range with contiguous storage
template
<
class
IteratorType
,
template
<
class
IteratorType
,
typename
std
::
enable_if
<
typename
std
::
enable_if
<
std
::
is_same
<
typename
iterator_traits
<
IteratorType
>::
iterator_category
,
std
::
random_access_iterator_tag
>::
value
,
std
::
is_same
<
typename
iterator_traits
<
IteratorType
>::
iterator_category
,
std
::
random_access_iterator_tag
>::
value
,
int
>::
type
=
0
>
int
>::
type
=
0
>
input_adapter
(
IteratorType
first
,
IteratorType
last
)
std
::
shared_ptr
<
input_buffer_adapter
>
input_adapter
(
IteratorType
first
,
IteratorType
last
)
{
{
#ifndef NDEBUG
#ifndef NDEBUG
// assertion to check that the iterator range is indeed contiguous,
// assertion to check that the iterator range is indeed contiguous,
// see https://stackoverflow.com/a/35008842/266378 for more discussion
// see https://stackoverflow.com/a/35008842/266378 for more discussion
...
@@ -407,36 +394,98 @@ class input_adapter
...
@@ -407,36 +394,98 @@ class input_adapter
if
(
JSON_HEDLEY_LIKELY
(
len
>
0
))
if
(
JSON_HEDLEY_LIKELY
(
len
>
0
))
{
{
// there is at least one element: use the address of first
// there is at least one element: use the address of first
ia
=
std
::
make_shared
<
input_buffer_adapter
>
(
reinterpret_cast
<
const
char
*>
(
&
(
*
first
)),
len
);
return
std
::
make_shared
<
input_buffer_adapter
>
(
reinterpret_cast
<
const
char
*>
(
&
(
*
first
)),
len
);
}
}
else
else
{
{
// the address of first cannot be used: use nullptr
// the address of first cannot be used: use nullptr
ia
=
std
::
make_shared
<
input_buffer_adapter
>
(
nullptr
,
len
);
return
std
::
make_shared
<
input_buffer_adapter
>
(
nullptr
,
len
);
}
}
}
}
inline
std
::
shared_ptr
<
wide_string_input_adapter
<
std
::
wstring
>>
input_adapter
(
const
std
::
wstring
&
ws
)
{
return
std
::
make_shared
<
wide_string_input_adapter
<
std
::
wstring
>>
(
ws
);
}
inline
std
::
shared_ptr
<
wide_string_input_adapter
<
std
::
u16string
>>
input_adapter
(
const
std
::
u16string
&
ws
)
{
return
std
::
make_shared
<
wide_string_input_adapter
<
std
::
u16string
>>
(
ws
);
}
inline
std
::
shared_ptr
<
wide_string_input_adapter
<
std
::
u32string
>>
input_adapter
(
const
std
::
u32string
&
ws
)
{
return
std
::
make_shared
<
wide_string_input_adapter
<
std
::
u32string
>>
(
ws
);
}
template
<
class
ContiguousContainer
,
typename
std
::
enable_if
<
not
std
::
is_pointer
<
ContiguousContainer
>::
value
and
std
::
is_base_of
<
std
::
random_access_iterator_tag
,
typename
iterator_traits
<
decltype
(
std
::
begin
(
std
::
declval
<
ContiguousContainer
const
>
()))
>::
iterator_category
>::
value
,
int
>::
type
=
0
>
std
::
shared_ptr
<
input_buffer_adapter
>
input_adapter
(
const
ContiguousContainer
&
c
)
{
return
input_adapter
(
std
::
begin
(
c
),
std
::
end
(
c
));
}
template
<
class
T
,
std
::
size_t
N
>
std
::
shared_ptr
<
input_buffer_adapter
>
input_adapter
(
T
(
&
array
)[
N
])
{
return
input_adapter
(
std
::
begin
(
array
),
std
::
end
(
array
));
}
// This class only handles inputs of input_buffer_adapter type.
// It's required so that expressions like {ptr, len} can be implicitely casted
// to the correct adapter.
class
span_input_adapter
{
public
:
template
<
typename
CharT
,
typename
std
::
enable_if
<
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
int
>::
type
=
0
>
span_input_adapter
(
CharT
b
,
std
::
size_t
l
)
:
ia
(
std
::
make_shared
<
input_buffer_adapter
>
(
reinterpret_cast
<
const
char
*>
(
b
),
l
))
{}
template
<
typename
CharT
,
typename
std
::
enable_if
<
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
int
>::
type
=
0
>
span_input_adapter
(
CharT
b
)
:
span_input_adapter
(
reinterpret_cast
<
const
char
*>
(
b
),
std
::
strlen
(
reinterpret_cast
<
const
char
*>
(
b
)))
{}
template
<
class
IteratorType
,
typename
std
::
enable_if
<
std
::
is_same
<
typename
iterator_traits
<
IteratorType
>::
iterator_category
,
std
::
random_access_iterator_tag
>::
value
,
int
>::
type
=
0
>
span_input_adapter
(
IteratorType
first
,
IteratorType
last
)
:
ia
(
input_adapter
(
first
,
last
))
{}
/// input adapter for array
template
<
class
T
,
std
::
size_t
N
>
template
<
class
T
,
std
::
size_t
N
>
input_adapter
(
T
(
&
array
)[
N
])
span_
input_adapter
(
T
(
&
array
)[
N
])
:
input_adapter
(
std
::
begin
(
array
),
std
::
end
(
array
))
{}
:
span_
input_adapter
(
std
::
begin
(
array
),
std
::
end
(
array
))
{}
/// input adapter for contiguous container
/// input adapter for contiguous container
template
<
class
ContiguousContainer
,
typename
template
<
class
ContiguousContainer
,
typename
std
::
enable_if
<
not
std
::
is_pointer
<
ContiguousContainer
>::
value
and
std
::
enable_if
<
not
std
::
is_pointer
<
ContiguousContainer
>::
value
and
std
::
is_base_of
<
std
::
random_access_iterator_tag
,
typename
iterator_traits
<
decltype
(
std
::
begin
(
std
::
declval
<
ContiguousContainer
const
>
()))
>::
iterator_category
>::
value
,
std
::
is_base_of
<
std
::
random_access_iterator_tag
,
typename
iterator_traits
<
decltype
(
std
::
begin
(
std
::
declval
<
ContiguousContainer
const
>
()))
>::
iterator_category
>::
value
,
int
>::
type
=
0
>
int
>::
type
=
0
>
input_adapter
(
const
ContiguousContainer
&
c
)
span_
input_adapter
(
const
ContiguousContainer
&
c
)
:
input_adapter
(
std
::
begin
(
c
),
std
::
end
(
c
))
{}
:
span_
input_adapter
(
std
::
begin
(
c
),
std
::
end
(
c
))
{}
operator
input_adapter_
t
()
std
::
shared_ptr
<
input_buffer_adapter
>
ge
t
()
{
{
return
ia
;
return
ia
;
}
}
private
:
private
:
/// the actual adapter
std
::
shared_ptr
<
input_buffer_adapter
>
ia
=
nullptr
;
input_adapter_t
ia
=
nullptr
;
};
};
}
// namespace detail
}
// namespace detail
}
// namespace nlohmann
}
// namespace nlohmann
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,24 +24,8 @@ namespace detail
...
@@ -24,24 +24,8 @@ namespace detail
// parser //
// parser //
////////////
////////////
/*!
enum
class
parse_event_t
:
uint8_t
@brief syntax analysis
This class implements a recursive descent parser.
*/
template
<
typename
BasicJsonType
>
class
parser
{
{
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
;
using
lexer_t
=
lexer
<
BasicJsonType
>
;
using
token_type
=
typename
lexer_t
::
token_type
;
public
:
enum
class
parse_event_t
:
uint8_t
{
/// the parser read `{` and started to process a JSON object
/// the parser read `{` and started to process a JSON object
object_start
,
object_start
,
/// the parser read `}` and finished processing a JSON object
/// the parser read `}` and finished processing a JSON object
...
@@ -54,14 +38,32 @@ class parser
...
@@ -54,14 +38,32 @@ class parser
key
,
key
,
/// the parser finished reading a JSON value
/// the parser finished reading a JSON value
value
value
};
};
using
parser_callback_t
=
template
<
typename
BasicJsonType
>
using
parser_callback_t
=
std
::
function
<
bool
(
int
depth
,
parse_event_t
event
,
BasicJsonType
&
parsed
)
>
;
std
::
function
<
bool
(
int
depth
,
parse_event_t
event
,
BasicJsonType
&
parsed
)
>
;
/*!
@brief syntax analysis
This class implements a recursive descent parser.
*/
template
<
typename
BasicJsonType
,
typename
InputAdapterType
=
input_adapter_protocol
>
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
;
using
string_t
=
typename
BasicJsonType
::
string_t
;
using
lexer_t
=
lexer
<
BasicJsonType
,
InputAdapterType
>
;
using
token_type
=
typename
lexer_t
::
token_type
;
public
:
/// 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
...
@@ -167,7 +167,9 @@ class basic_json
...
@@ -167,7 +167,9 @@ class basic_json
private
:
private
:
template
<
detail
::
value_t
>
friend
struct
detail
::
external_constructor
;
template
<
detail
::
value_t
>
friend
struct
detail
::
external_constructor
;
friend
::
nlohmann
::
json_pointer
<
basic_json
>
;
friend
::
nlohmann
::
json_pointer
<
basic_json
>
;
friend
::
nlohmann
::
detail
::
parser
<
basic_json
>
;
template
<
typename
BasicJsonType
,
typename
InputType
>
friend
class
::
nlohmann
::
detail
::
parser
;
friend
::
nlohmann
::
detail
::
serializer
<
basic_json
>
;
friend
::
nlohmann
::
detail
::
serializer
<
basic_json
>
;
template
<
typename
BasicJsonType
>
template
<
typename
BasicJsonType
>
friend
class
::
nlohmann
::
detail
::
iter_impl
;
friend
class
::
nlohmann
::
detail
::
iter_impl
;
...
@@ -184,8 +186,17 @@ class basic_json
...
@@ -184,8 +186,17 @@ class basic_json
using
basic_json_t
=
NLOHMANN_BASIC_JSON_TPL
;
using
basic_json_t
=
NLOHMANN_BASIC_JSON_TPL
;
// convenience aliases for types residing in namespace detail;
// convenience aliases for types residing in namespace detail;
using
lexer
=
::
nlohmann
::
detail
::
lexer
<
basic_json
>
;
using
lexer
=
::
nlohmann
::
detail
::
lexer_base
<
basic_json
>
;
using
parser
=
::
nlohmann
::
detail
::
parser
<
basic_json
>
;
template
<
typename
InputAdapterType
>
static
::
nlohmann
::
detail
::
parser
<
basic_json
,
InputAdapterType
>
parser
(
std
::
shared_ptr
<
InputAdapterType
>
adapter
,
detail
::
parser_callback_t
<
basic_json
>
cb
=
nullptr
,
bool
allow_exceptions
=
true
)
{
return
::
nlohmann
::
detail
::
parser
<
basic_json
,
InputAdapterType
>
(
std
::
move
(
adapter
),
std
::
move
(
cb
),
allow_exceptions
);
}
using
primitive_iterator_t
=
::
nlohmann
::
detail
::
primitive_iterator_t
;
using
primitive_iterator_t
=
::
nlohmann
::
detail
::
primitive_iterator_t
;
template
<
typename
BasicJsonType
>
template
<
typename
BasicJsonType
>
...
@@ -1115,7 +1126,7 @@ class basic_json
...
@@ -1115,7 +1126,7 @@ class basic_json
@sa @ref parser_callback_t for more information and examples
@sa @ref parser_callback_t for more information and examples
*/
*/
using
parse_event_t
=
typename
parser
::
parse_event_t
;
using
parse_event_t
=
detail
::
parse_event_t
;
/*!
/*!
@brief per-element parser callback type
@brief per-element parser callback type
...
@@ -1166,7 +1177,7 @@ class basic_json
...
@@ -1166,7 +1177,7 @@ class basic_json
@since version 1.0.0
@since version 1.0.0
*/
*/
using
parser_callback_t
=
typename
parser
::
parser_callback_t
;
using
parser_callback_t
=
detail
::
parser_callback_t
<
basic_json
>
;
//////////////////
//////////////////
// constructors //
// constructors //
...
@@ -6198,21 +6209,39 @@ class basic_json
...
@@ -6198,21 +6209,39 @@ class basic_json
@since version 2.0.3 (contiguous containers)
@since version 2.0.3 (contiguous containers)
*/
*/
template
<
typename
InputType
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
parse
(
detail
::
input_adapter
&&
i
,
static
basic_json
parse
(
InputType
&&
i
,
const
parser_callback_t
cb
=
nullptr
,
const
parser_callback_t
cb
=
nullptr
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
)
{
{
basic_json
result
;
basic_json
result
;
parser
(
i
,
cb
,
allow_exceptions
).
parse
(
true
,
result
);
parser
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
))
,
cb
,
allow_exceptions
).
parse
(
true
,
result
);
return
result
;
return
result
;
}
}
static
bool
accept
(
detail
::
input_adapter
&&
i
)
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
parse
(
detail
::
span_input_adapter
&&
i
,
const
parser_callback_t
cb
=
nullptr
,
const
bool
allow_exceptions
=
true
)
{
basic_json
result
;
parser
(
i
.
get
(),
cb
,
allow_exceptions
).
parse
(
true
,
result
);
return
result
;
}
template
<
typename
InputType
>
static
bool
accept
(
InputType
&&
i
)
{
{
return
parser
(
i
).
accept
(
true
);
return
parser
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
))
).
accept
(
true
);
}
}
static
bool
accept
(
detail
::
span_input_adapter
&&
i
)
{
return
parser
(
i
.
get
()).
accept
(
true
);
}
/*!
/*!
@brief generate SAX events
@brief generate SAX events
...
@@ -6266,18 +6295,37 @@ class basic_json
...
@@ -6266,18 +6295,37 @@ class basic_json
@since version 3.2.0
@since version 3.2.0
*/
*/
template
<
typename
SAX
,
typename
InputType
>
JSON_HEDLEY_NON_NULL
(
2
)
static
bool
sax_parse
(
InputType
&&
i
,
SAX
*
sax
,
input_format_t
format
=
input_format_t
::
json
,
const
bool
strict
=
true
)
{
assert
(
sax
);
auto
input_adapter
=
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
));
using
adapter_type
=
typename
decltype
(
input_adapter
)
::
element_type
;
return
format
==
input_format_t
::
json
?
parser
(
std
::
move
(
input_adapter
)).
sax_parse
(
sax
,
strict
)
:
detail
::
binary_reader
<
basic_json
,
SAX
,
adapter_type
>
(
std
::
move
(
input_adapter
)).
sax_parse
(
format
,
sax
,
strict
);
}
template
<
typename
SAX
>
template
<
typename
SAX
>
JSON_HEDLEY_NON_NULL
(
2
)
JSON_HEDLEY_NON_NULL
(
2
)
static
bool
sax_parse
(
detail
::
input_adapter
&&
i
,
SAX
*
sax
,
static
bool
sax_parse
(
detail
::
span_
input_adapter
&&
i
,
SAX
*
sax
,
input_format_t
format
=
input_format_t
::
json
,
input_format_t
format
=
input_format_t
::
json
,
const
bool
strict
=
true
)
const
bool
strict
=
true
)
{
{
assert
(
sax
);
assert
(
sax
);
auto
input_adapter
=
i
.
get
();
using
adapter_type
=
typename
decltype
(
input_adapter
)
::
element_type
;
return
format
==
input_format_t
::
json
return
format
==
input_format_t
::
json
?
parser
(
std
::
move
(
i
)).
sax_parse
(
sax
,
strict
)
?
parser
(
std
::
move
(
i
nput_adapter
)).
sax_parse
(
sax
,
strict
)
:
detail
::
binary_reader
<
basic_json
,
SAX
>
(
std
::
move
(
i
)).
sax_parse
(
format
,
sax
,
strict
);
:
detail
::
binary_reader
<
basic_json
,
SAX
,
adapter_type
>
(
std
::
move
(
input_adapter
)).
sax_parse
(
format
,
sax
,
strict
);
}
}
/*!
/*!
@brief deserialize from an iterator range with contiguous storage
@brief deserialize from an iterator range with contiguous storage
...
@@ -6970,14 +7018,15 @@ class basic_json
...
@@ -6970,14 +7018,15 @@ class basic_json
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
since 3.2.0
since 3.2.0
*/
*/
template
<
typename
InputType
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_cbor
(
detail
::
input_adapter
&&
i
,
static
basic_json
from_cbor
(
InputType
&&
i
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
)
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
i
)).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
)
)).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
...
@@ -6985,7 +7034,7 @@ class basic_json
...
@@ -6985,7 +7034,7 @@ class basic_json
@copydoc from_cbor(detail::input_adapter&&, const bool, const bool)
@copydoc from_cbor(detail::input_adapter&&, const bool, const bool)
*/
*/
template
<
typename
A1
,
typename
A2
,
template
<
typename
A1
,
typename
A2
,
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
span_
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_cbor
(
A1
&&
a1
,
A2
&&
a2
,
static
basic_json
from_cbor
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
...
@@ -6993,7 +7042,18 @@ class basic_json
...
@@ -6993,7 +7042,18 @@ class basic_json
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
))).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_cbor
(
detail
::
span_input_adapter
&&
i
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
...
@@ -7079,14 +7139,15 @@ class basic_json
...
@@ -7079,14 +7139,15 @@ class basic_json
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
since 3.2.0
since 3.2.0
*/
*/
template
<
typename
InputType
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_msgpack
(
detail
::
input_adapter
&&
i
,
static
basic_json
from_msgpack
(
InputType
&&
i
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
)
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
i
)).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
)
)).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
...
@@ -7094,7 +7155,7 @@ class basic_json
...
@@ -7094,7 +7155,7 @@ class basic_json
@copydoc from_msgpack(detail::input_adapter&&, const bool, const bool)
@copydoc from_msgpack(detail::input_adapter&&, const bool, const bool)
*/
*/
template
<
typename
A1
,
typename
A2
,
template
<
typename
A1
,
typename
A2
,
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
span_
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_msgpack
(
A1
&&
a1
,
A2
&&
a2
,
static
basic_json
from_msgpack
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
...
@@ -7102,10 +7163,23 @@ class basic_json
...
@@ -7102,10 +7163,23 @@ class basic_json
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)
)).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_msgpack
(
detail
::
span_input_adapter
&&
i
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
/*!
/*!
@brief create a JSON value from an input in UBJSON format
@brief create a JSON value from an input in UBJSON format
...
@@ -7167,14 +7241,15 @@ class basic_json
...
@@ -7167,14 +7241,15 @@ class basic_json
@since version 3.1.0; added @a allow_exceptions parameter since 3.2.0
@since version 3.1.0; added @a allow_exceptions parameter since 3.2.0
*/
*/
template
<
typename
InputType
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_ubjson
(
detail
::
input_adapter
&&
i
,
static
basic_json
from_ubjson
(
InputType
&&
i
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
)
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
i
)).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
)
)).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
...
@@ -7182,7 +7257,7 @@ class basic_json
...
@@ -7182,7 +7257,7 @@ class basic_json
@copydoc from_ubjson(detail::input_adapter&&, const bool, const bool)
@copydoc from_ubjson(detail::input_adapter&&, const bool, const bool)
*/
*/
template
<
typename
A1
,
typename
A2
,
template
<
typename
A1
,
typename
A2
,
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
span_
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_ubjson
(
A1
&&
a1
,
A2
&&
a2
,
static
basic_json
from_ubjson
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
...
@@ -7190,10 +7265,22 @@ class basic_json
...
@@ -7190,10 +7265,22 @@ class basic_json
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
))).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
static
basic_json
from_ubjson
(
detail
::
span_input_adapter
&&
i
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
/*!
/*!
@brief Create a JSON value from an input in BSON format
@brief Create a JSON value from an input in BSON format
...
@@ -7254,14 +7341,15 @@ class basic_json
...
@@ -7254,14 +7341,15 @@ class basic_json
@sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
@sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
related UBJSON format
related UBJSON format
*/
*/
template
<
typename
InputType
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_bson
(
detail
::
input_adapter
&&
i
,
static
basic_json
from_bson
(
InputType
&&
i
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
)
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
i
)).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
)
)).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
...
@@ -7269,7 +7357,7 @@ class basic_json
...
@@ -7269,7 +7357,7 @@ class basic_json
@copydoc from_bson(detail::input_adapter&&, const bool, const bool)
@copydoc from_bson(detail::input_adapter&&, const bool, const bool)
*/
*/
template
<
typename
A1
,
typename
A2
,
template
<
typename
A1
,
typename
A2
,
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
span_
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_bson
(
A1
&&
a1
,
A2
&&
a2
,
static
basic_json
from_bson
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
...
@@ -7277,12 +7365,20 @@ class basic_json
...
@@ -7277,12 +7365,20 @@ class basic_json
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)
)).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_bson
(
detail
::
span_input_adapter
&&
i
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
/// @}
/// @}
//////////////////////////
//////////////////////////
...
...
single_include/nlohmann/json.hpp
View file @
617b3cf4
...
@@ -3908,14 +3908,11 @@ struct input_adapter_protocol
...
@@ -3908,14 +3908,11 @@ struct input_adapter_protocol
virtual
~
input_adapter_protocol
()
=
default
;
virtual
~
input_adapter_protocol
()
=
default
;
};
};
/// a type to simplify interfaces
using
input_adapter_t
=
std
::
shared_ptr
<
input_adapter_protocol
>
;
/*!
/*!
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
buffer. This adapter is a very low level adapter.
buffer. This adapter is a very low level adapter.
*/
*/
class
file_input_adapter
:
public
input_adapter_protocol
class
file_input_adapter
final
:
public
input_adapter_protocol
{
{
public
:
public
:
JSON_HEDLEY_NON_NULL
(
2
)
JSON_HEDLEY_NON_NULL
(
2
)
...
@@ -3950,7 +3947,7 @@ characters following those used in parsing the JSON input. Clears the
...
@@ -3950,7 +3947,7 @@ characters following those used in parsing the JSON input. Clears the
std::istream flags; any input errors (e.g., EOF) will be detected by the first
std::istream flags; any input errors (e.g., EOF) will be detected by the first
subsequent call for input from the std::istream.
subsequent call for input from the std::istream.
*/
*/
class
input_stream_adapter
:
public
input_adapter_protocol
class
input_stream_adapter
final
:
public
input_adapter_protocol
{
{
public
:
public
:
~
input_stream_adapter
()
override
~
input_stream_adapter
()
override
...
@@ -3991,7 +3988,7 @@ class input_stream_adapter : public input_adapter_protocol
...
@@ -3991,7 +3988,7 @@ class input_stream_adapter : public input_adapter_protocol
};
};
/// input adapter for buffer input
/// input adapter for buffer input
class
input_buffer_adapter
:
public
input_adapter_protocol
class
input_buffer_adapter
final
:
public
input_adapter_protocol
{
{
public
:
public
:
input_buffer_adapter
(
const
char
*
b
,
const
std
::
size_t
l
)
noexcept
input_buffer_adapter
(
const
char
*
b
,
const
std
::
size_t
l
)
noexcept
...
@@ -4148,7 +4145,7 @@ struct wide_string_input_helper<WideStringType, 2>
...
@@ -4148,7 +4145,7 @@ struct wide_string_input_helper<WideStringType, 2>
};
};
template
<
typename
WideStringType
>
template
<
typename
WideStringType
>
class
wide_string_input_adapter
:
public
input_adapter_protocol
class
wide_string_input_adapter
final
:
public
input_adapter_protocol
{
{
public
:
public
:
explicit
wide_string_input_adapter
(
const
WideStringType
&
w
)
noexcept
explicit
wide_string_input_adapter
(
const
WideStringType
&
w
)
noexcept
...
@@ -4194,60 +4191,50 @@ class wide_string_input_adapter : public input_adapter_protocol
...
@@ -4194,60 +4191,50 @@ class wide_string_input_adapter : public input_adapter_protocol
std
::
size_t
utf8_bytes_filled
=
0
;
std
::
size_t
utf8_bytes_filled
=
0
;
};
};
class
input_adapter
inline
std
::
shared_ptr
<
file_input_adapter
>
input_adapter
(
std
::
FILE
*
file
)
{
{
public
:
return
std
::
make_shared
<
file_input_adapter
>
(
file
);
// native support
}
JSON_HEDLEY_NON_NULL
(
2
)
input_adapter
(
std
::
FILE
*
file
)
:
ia
(
std
::
make_shared
<
file_input_adapter
>
(
file
))
{}
/// input adapter for input stream
input_adapter
(
std
::
istream
&
i
)
:
ia
(
std
::
make_shared
<
input_stream_adapter
>
(
i
))
{}
/// input adapter for input stream
input_adapter
(
std
::
istream
&&
i
)
:
ia
(
std
::
make_shared
<
input_stream_adapter
>
(
i
))
{}
input_adapter
(
const
std
::
wstring
&
ws
)
:
ia
(
std
::
make_shared
<
wide_string_input_adapter
<
std
::
wstring
>>
(
ws
))
{}
input_adapter
(
const
std
::
u16string
&
ws
)
inline
std
::
shared_ptr
<
input_stream_adapter
>
input_adapter
(
std
::
istream
&
stream
)
:
ia
(
std
::
make_shared
<
wide_string_input_adapter
<
std
::
u16string
>>
(
ws
))
{}
{
return
std
::
make_shared
<
input_stream_adapter
>
(
stream
);
}
input_adapter
(
const
std
::
u32string
&
ws
)
inline
std
::
shared_ptr
<
input_stream_adapter
>
input_adapter
(
std
::
istream
&&
stream
)
:
ia
(
std
::
make_shared
<
wide_string_input_adapter
<
std
::
u32string
>>
(
ws
))
{}
{
return
std
::
make_shared
<
input_stream_adapter
>
(
stream
);
}
/// input adapter for buffer
template
<
typename
CharT
,
template
<
typename
CharT
,
typename
std
::
enable_if
<
typename
std
::
enable_if
<
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
int
>::
type
=
0
>
int
>::
type
=
0
>
input_adapter
(
CharT
b
,
std
::
size_t
l
)
std
::
shared_ptr
<
input_buffer_adapter
>
input_adapter
(
CharT
b
,
std
::
size_t
l
)
:
ia
(
std
::
make_shared
<
input_buffer_adapter
>
(
reinterpret_cast
<
const
char
*>
(
b
),
l
))
{}
{
return
std
::
make_shared
<
input_buffer_adapter
>
(
reinterpret_cast
<
const
char
*>
(
b
),
l
);
// derived support
}
/// input adapter for string literal
template
<
typename
CharT
,
template
<
typename
CharT
,
typename
std
::
enable_if
<
typename
std
::
enable_if
<
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
int
>::
type
=
0
>
int
>::
type
=
0
>
input_adapter
(
CharT
b
)
std
::
shared_ptr
<
input_buffer_adapter
>
input_adapter
(
CharT
b
)
:
input_adapter
(
reinterpret_cast
<
const
char
*>
(
b
),
{
std
::
strlen
(
reinterpret_cast
<
const
char
*>
(
b
)))
{}
return
input_adapter
(
reinterpret_cast
<
const
char
*>
(
b
),
std
::
strlen
(
reinterpret_cast
<
const
char
*>
(
b
)));
}
/// input adapter for iterator range with contiguous storage
template
<
class
IteratorType
,
template
<
class
IteratorType
,
typename
std
::
enable_if
<
typename
std
::
enable_if
<
std
::
is_same
<
typename
iterator_traits
<
IteratorType
>::
iterator_category
,
std
::
random_access_iterator_tag
>::
value
,
std
::
is_same
<
typename
iterator_traits
<
IteratorType
>::
iterator_category
,
std
::
random_access_iterator_tag
>::
value
,
int
>::
type
=
0
>
int
>::
type
=
0
>
input_adapter
(
IteratorType
first
,
IteratorType
last
)
std
::
shared_ptr
<
input_buffer_adapter
>
input_adapter
(
IteratorType
first
,
IteratorType
last
)
{
{
#ifndef NDEBUG
#ifndef NDEBUG
// assertion to check that the iterator range is indeed contiguous,
// assertion to check that the iterator range is indeed contiguous,
// see https://stackoverflow.com/a/35008842/266378 for more discussion
// see https://stackoverflow.com/a/35008842/266378 for more discussion
...
@@ -4270,36 +4257,98 @@ class input_adapter
...
@@ -4270,36 +4257,98 @@ class input_adapter
if
(
JSON_HEDLEY_LIKELY
(
len
>
0
))
if
(
JSON_HEDLEY_LIKELY
(
len
>
0
))
{
{
// there is at least one element: use the address of first
// there is at least one element: use the address of first
ia
=
std
::
make_shared
<
input_buffer_adapter
>
(
reinterpret_cast
<
const
char
*>
(
&
(
*
first
)),
len
);
return
std
::
make_shared
<
input_buffer_adapter
>
(
reinterpret_cast
<
const
char
*>
(
&
(
*
first
)),
len
);
}
}
else
else
{
{
// the address of first cannot be used: use nullptr
// the address of first cannot be used: use nullptr
ia
=
std
::
make_shared
<
input_buffer_adapter
>
(
nullptr
,
len
);
return
std
::
make_shared
<
input_buffer_adapter
>
(
nullptr
,
len
);
}
}
}
}
inline
std
::
shared_ptr
<
wide_string_input_adapter
<
std
::
wstring
>>
input_adapter
(
const
std
::
wstring
&
ws
)
{
return
std
::
make_shared
<
wide_string_input_adapter
<
std
::
wstring
>>
(
ws
);
}
inline
std
::
shared_ptr
<
wide_string_input_adapter
<
std
::
u16string
>>
input_adapter
(
const
std
::
u16string
&
ws
)
{
return
std
::
make_shared
<
wide_string_input_adapter
<
std
::
u16string
>>
(
ws
);
}
inline
std
::
shared_ptr
<
wide_string_input_adapter
<
std
::
u32string
>>
input_adapter
(
const
std
::
u32string
&
ws
)
{
return
std
::
make_shared
<
wide_string_input_adapter
<
std
::
u32string
>>
(
ws
);
}
template
<
class
ContiguousContainer
,
typename
std
::
enable_if
<
not
std
::
is_pointer
<
ContiguousContainer
>::
value
and
std
::
is_base_of
<
std
::
random_access_iterator_tag
,
typename
iterator_traits
<
decltype
(
std
::
begin
(
std
::
declval
<
ContiguousContainer
const
>
()))
>::
iterator_category
>::
value
,
int
>::
type
=
0
>
std
::
shared_ptr
<
input_buffer_adapter
>
input_adapter
(
const
ContiguousContainer
&
c
)
{
return
input_adapter
(
std
::
begin
(
c
),
std
::
end
(
c
));
}
template
<
class
T
,
std
::
size_t
N
>
std
::
shared_ptr
<
input_buffer_adapter
>
input_adapter
(
T
(
&
array
)[
N
])
{
return
input_adapter
(
std
::
begin
(
array
),
std
::
end
(
array
));
}
// This class only handles inputs of input_buffer_adapter type.
// It's required so that expressions like {ptr, len} can be implicitely casted
// to the correct adapter.
class
span_input_adapter
{
public
:
template
<
typename
CharT
,
typename
std
::
enable_if
<
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
int
>::
type
=
0
>
span_input_adapter
(
CharT
b
,
std
::
size_t
l
)
:
ia
(
std
::
make_shared
<
input_buffer_adapter
>
(
reinterpret_cast
<
const
char
*>
(
b
),
l
))
{}
template
<
typename
CharT
,
typename
std
::
enable_if
<
std
::
is_pointer
<
CharT
>::
value
and
std
::
is_integral
<
typename
std
::
remove_pointer
<
CharT
>::
type
>::
value
and
sizeof
(
typename
std
::
remove_pointer
<
CharT
>::
type
)
==
1
,
int
>::
type
=
0
>
span_input_adapter
(
CharT
b
)
:
span_input_adapter
(
reinterpret_cast
<
const
char
*>
(
b
),
std
::
strlen
(
reinterpret_cast
<
const
char
*>
(
b
)))
{}
template
<
class
IteratorType
,
typename
std
::
enable_if
<
std
::
is_same
<
typename
iterator_traits
<
IteratorType
>::
iterator_category
,
std
::
random_access_iterator_tag
>::
value
,
int
>::
type
=
0
>
span_input_adapter
(
IteratorType
first
,
IteratorType
last
)
:
ia
(
input_adapter
(
first
,
last
))
{}
/// input adapter for array
template
<
class
T
,
std
::
size_t
N
>
template
<
class
T
,
std
::
size_t
N
>
input_adapter
(
T
(
&
array
)[
N
])
span_
input_adapter
(
T
(
&
array
)[
N
])
:
input_adapter
(
std
::
begin
(
array
),
std
::
end
(
array
))
{}
:
span_
input_adapter
(
std
::
begin
(
array
),
std
::
end
(
array
))
{}
/// input adapter for contiguous container
/// input adapter for contiguous container
template
<
class
ContiguousContainer
,
typename
template
<
class
ContiguousContainer
,
typename
std
::
enable_if
<
not
std
::
is_pointer
<
ContiguousContainer
>::
value
and
std
::
enable_if
<
not
std
::
is_pointer
<
ContiguousContainer
>::
value
and
std
::
is_base_of
<
std
::
random_access_iterator_tag
,
typename
iterator_traits
<
decltype
(
std
::
begin
(
std
::
declval
<
ContiguousContainer
const
>
()))
>::
iterator_category
>::
value
,
std
::
is_base_of
<
std
::
random_access_iterator_tag
,
typename
iterator_traits
<
decltype
(
std
::
begin
(
std
::
declval
<
ContiguousContainer
const
>
()))
>::
iterator_category
>::
value
,
int
>::
type
=
0
>
int
>::
type
=
0
>
input_adapter
(
const
ContiguousContainer
&
c
)
span_
input_adapter
(
const
ContiguousContainer
&
c
)
:
input_adapter
(
std
::
begin
(
c
),
std
::
end
(
c
))
{}
:
span_
input_adapter
(
std
::
begin
(
c
),
std
::
end
(
c
))
{}
operator
input_adapter_
t
()
std
::
shared_ptr
<
input_buffer_adapter
>
ge
t
()
{
{
return
ia
;
return
ia
;
}
}
private
:
private
:
/// the actual adapter
std
::
shared_ptr
<
input_buffer_adapter
>
ia
=
nullptr
;
input_adapter_t
ia
=
nullptr
;
};
};
}
// namespace detail
}
// namespace detail
}
// namespace nlohmann
}
// namespace nlohmann
...
@@ -5171,9 +5220,10 @@ namespace detail
...
@@ -5171,9 +5220,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
;
...
@@ -5186,7 +5236,7 @@ class binary_reader
...
@@ -5186,7 +5236,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
);
...
@@ -7105,7 +7155,7 @@ class binary_reader
...
@@ -7105,7 +7155,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
();
...
@@ -7152,19 +7202,9 @@ namespace detail
...
@@ -7152,19 +7202,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
...
@@ -7205,9 +7245,9 @@ class lexer
...
@@ -7205,9 +7245,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
"'['"
;
...
@@ -7233,15 +7273,33 @@ class lexer
...
@@ -7233,15 +7273,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
;
explicit
lexer
(
detail
::
input_adapter_t
&&
adapter
)
public
:
using
token_type
=
typename
lexer_base
<
BasicJsonType
>::
token_type
;
explicit
lexer
(
input_adapter_ptr_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
:
...
@@ -8610,7 +8668,7 @@ scan_number_done:
...
@@ -8610,7 +8668,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
();
...
@@ -8675,24 +8733,8 @@ namespace detail
...
@@ -8675,24 +8733,8 @@ namespace detail
// parser //
// parser //
////////////
////////////
/*!
enum
class
parse_event_t
:
uint8_t
@brief syntax analysis
This class implements a recursive descent parser.
*/
template
<
typename
BasicJsonType
>
class
parser
{
{
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
;
using
lexer_t
=
lexer
<
BasicJsonType
>
;
using
token_type
=
typename
lexer_t
::
token_type
;
public
:
enum
class
parse_event_t
:
uint8_t
{
/// the parser read `{` and started to process a JSON object
/// the parser read `{` and started to process a JSON object
object_start
,
object_start
,
/// the parser read `}` and finished processing a JSON object
/// the parser read `}` and finished processing a JSON object
...
@@ -8705,14 +8747,32 @@ class parser
...
@@ -8705,14 +8747,32 @@ class parser
key
,
key
,
/// the parser finished reading a JSON value
/// the parser finished reading a JSON value
value
value
};
};
using
parser_callback_t
=
template
<
typename
BasicJsonType
>
using
parser_callback_t
=
std
::
function
<
bool
(
int
depth
,
parse_event_t
event
,
BasicJsonType
&
parsed
)
>
;
std
::
function
<
bool
(
int
depth
,
parse_event_t
event
,
BasicJsonType
&
parsed
)
>
;
/*!
@brief syntax analysis
This class implements a recursive descent parser.
*/
template
<
typename
BasicJsonType
,
typename
InputAdapterType
=
input_adapter_protocol
>
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
;
using
string_t
=
typename
BasicJsonType
::
string_t
;
using
lexer_t
=
lexer
<
BasicJsonType
,
InputAdapterType
>
;
using
token_type
=
typename
lexer_t
::
token_type
;
public
:
/// 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_
)
{
{
...
@@ -9137,7 +9197,7 @@ class parser
...
@@ -9137,7 +9197,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
...
@@ -14710,7 +14770,9 @@ class basic_json
...
@@ -14710,7 +14770,9 @@ class basic_json
private
:
private
:
template
<
detail
::
value_t
>
friend
struct
detail
::
external_constructor
;
template
<
detail
::
value_t
>
friend
struct
detail
::
external_constructor
;
friend
::
nlohmann
::
json_pointer
<
basic_json
>
;
friend
::
nlohmann
::
json_pointer
<
basic_json
>
;
friend
::
nlohmann
::
detail
::
parser
<
basic_json
>
;
template
<
typename
BasicJsonType
,
typename
InputType
>
friend
class
::
nlohmann
::
detail
::
parser
;
friend
::
nlohmann
::
detail
::
serializer
<
basic_json
>
;
friend
::
nlohmann
::
detail
::
serializer
<
basic_json
>
;
template
<
typename
BasicJsonType
>
template
<
typename
BasicJsonType
>
friend
class
::
nlohmann
::
detail
::
iter_impl
;
friend
class
::
nlohmann
::
detail
::
iter_impl
;
...
@@ -14727,8 +14789,17 @@ class basic_json
...
@@ -14727,8 +14789,17 @@ class basic_json
using
basic_json_t
=
NLOHMANN_BASIC_JSON_TPL
;
using
basic_json_t
=
NLOHMANN_BASIC_JSON_TPL
;
// convenience aliases for types residing in namespace detail;
// convenience aliases for types residing in namespace detail;
using
lexer
=
::
nlohmann
::
detail
::
lexer
<
basic_json
>
;
using
lexer
=
::
nlohmann
::
detail
::
lexer_base
<
basic_json
>
;
using
parser
=
::
nlohmann
::
detail
::
parser
<
basic_json
>
;
template
<
typename
InputAdapterType
>
static
::
nlohmann
::
detail
::
parser
<
basic_json
,
InputAdapterType
>
parser
(
std
::
shared_ptr
<
InputAdapterType
>
adapter
,
detail
::
parser_callback_t
<
basic_json
>
cb
=
nullptr
,
bool
allow_exceptions
=
true
)
{
return
::
nlohmann
::
detail
::
parser
<
basic_json
,
InputAdapterType
>
(
std
::
move
(
adapter
),
std
::
move
(
cb
),
allow_exceptions
);
}
using
primitive_iterator_t
=
::
nlohmann
::
detail
::
primitive_iterator_t
;
using
primitive_iterator_t
=
::
nlohmann
::
detail
::
primitive_iterator_t
;
template
<
typename
BasicJsonType
>
template
<
typename
BasicJsonType
>
...
@@ -15658,7 +15729,7 @@ class basic_json
...
@@ -15658,7 +15729,7 @@ class basic_json
@sa @ref parser_callback_t for more information and examples
@sa @ref parser_callback_t for more information and examples
*/
*/
using
parse_event_t
=
typename
parser
::
parse_event_t
;
using
parse_event_t
=
detail
::
parse_event_t
;
/*!
/*!
@brief per-element parser callback type
@brief per-element parser callback type
...
@@ -15709,7 +15780,7 @@ class basic_json
...
@@ -15709,7 +15780,7 @@ class basic_json
@since version 1.0.0
@since version 1.0.0
*/
*/
using
parser_callback_t
=
typename
parser
::
parser_callback_t
;
using
parser_callback_t
=
detail
::
parser_callback_t
<
basic_json
>
;
//////////////////
//////////////////
// constructors //
// constructors //
...
@@ -20741,21 +20812,39 @@ class basic_json
...
@@ -20741,21 +20812,39 @@ class basic_json
@since version 2.0.3 (contiguous containers)
@since version 2.0.3 (contiguous containers)
*/
*/
template
<
typename
InputType
>
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
parse
(
InputType
&&
i
,
const
parser_callback_t
cb
=
nullptr
,
const
bool
allow_exceptions
=
true
)
{
basic_json
result
;
parser
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
)),
cb
,
allow_exceptions
).
parse
(
true
,
result
);
return
result
;
}
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
parse
(
detail
::
input_adapter
&&
i
,
static
basic_json
parse
(
detail
::
span_
input_adapter
&&
i
,
const
parser_callback_t
cb
=
nullptr
,
const
parser_callback_t
cb
=
nullptr
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
)
{
{
basic_json
result
;
basic_json
result
;
parser
(
i
,
cb
,
allow_exceptions
).
parse
(
true
,
result
);
parser
(
i
.
get
()
,
cb
,
allow_exceptions
).
parse
(
true
,
result
);
return
result
;
return
result
;
}
}
static
bool
accept
(
detail
::
input_adapter
&&
i
)
template
<
typename
InputType
>
static
bool
accept
(
InputType
&&
i
)
{
{
return
parser
(
i
).
accept
(
true
);
return
parser
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
))
).
accept
(
true
);
}
}
static
bool
accept
(
detail
::
span_input_adapter
&&
i
)
{
return
parser
(
i
.
get
()).
accept
(
true
);
}
/*!
/*!
@brief generate SAX events
@brief generate SAX events
...
@@ -20809,18 +20898,37 @@ class basic_json
...
@@ -20809,18 +20898,37 @@ class basic_json
@since version 3.2.0
@since version 3.2.0
*/
*/
template
<
typename
SAX
,
typename
InputType
>
JSON_HEDLEY_NON_NULL
(
2
)
static
bool
sax_parse
(
InputType
&&
i
,
SAX
*
sax
,
input_format_t
format
=
input_format_t
::
json
,
const
bool
strict
=
true
)
{
assert
(
sax
);
auto
input_adapter
=
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
));
using
adapter_type
=
typename
decltype
(
input_adapter
)
::
element_type
;
return
format
==
input_format_t
::
json
?
parser
(
std
::
move
(
input_adapter
)).
sax_parse
(
sax
,
strict
)
:
detail
::
binary_reader
<
basic_json
,
SAX
,
adapter_type
>
(
std
::
move
(
input_adapter
)).
sax_parse
(
format
,
sax
,
strict
);
}
template
<
typename
SAX
>
template
<
typename
SAX
>
JSON_HEDLEY_NON_NULL
(
2
)
JSON_HEDLEY_NON_NULL
(
2
)
static
bool
sax_parse
(
detail
::
input_adapter
&&
i
,
SAX
*
sax
,
static
bool
sax_parse
(
detail
::
span_
input_adapter
&&
i
,
SAX
*
sax
,
input_format_t
format
=
input_format_t
::
json
,
input_format_t
format
=
input_format_t
::
json
,
const
bool
strict
=
true
)
const
bool
strict
=
true
)
{
{
assert
(
sax
);
assert
(
sax
);
auto
input_adapter
=
i
.
get
();
using
adapter_type
=
typename
decltype
(
input_adapter
)
::
element_type
;
return
format
==
input_format_t
::
json
return
format
==
input_format_t
::
json
?
parser
(
std
::
move
(
i
)).
sax_parse
(
sax
,
strict
)
?
parser
(
std
::
move
(
i
nput_adapter
)).
sax_parse
(
sax
,
strict
)
:
detail
::
binary_reader
<
basic_json
,
SAX
>
(
std
::
move
(
i
)).
sax_parse
(
format
,
sax
,
strict
);
:
detail
::
binary_reader
<
basic_json
,
SAX
,
adapter_type
>
(
std
::
move
(
input_adapter
)).
sax_parse
(
format
,
sax
,
strict
);
}
}
/*!
/*!
@brief deserialize from an iterator range with contiguous storage
@brief deserialize from an iterator range with contiguous storage
...
@@ -21513,14 +21621,15 @@ class basic_json
...
@@ -21513,14 +21621,15 @@ class basic_json
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
since 3.2.0
since 3.2.0
*/
*/
template
<
typename
InputType
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_cbor
(
detail
::
input_adapter
&&
i
,
static
basic_json
from_cbor
(
InputType
&&
i
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
)
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
i
)).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
)
)).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
...
@@ -21528,7 +21637,7 @@ class basic_json
...
@@ -21528,7 +21637,7 @@ class basic_json
@copydoc from_cbor(detail::input_adapter&&, const bool, const bool)
@copydoc from_cbor(detail::input_adapter&&, const bool, const bool)
*/
*/
template
<
typename
A1
,
typename
A2
,
template
<
typename
A1
,
typename
A2
,
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
span_
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_cbor
(
A1
&&
a1
,
A2
&&
a2
,
static
basic_json
from_cbor
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
...
@@ -21536,7 +21645,18 @@ class basic_json
...
@@ -21536,7 +21645,18 @@ class basic_json
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
))).
sax_parse
(
input_format_t
::
cbor
,
&
sdp
,
strict
);
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_cbor
(
detail
::
span_input_adapter
&&
i
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
...
@@ -21622,14 +21742,15 @@ class basic_json
...
@@ -21622,14 +21742,15 @@ class basic_json
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
since 3.2.0
since 3.2.0
*/
*/
template
<
typename
InputType
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_msgpack
(
detail
::
input_adapter
&&
i
,
static
basic_json
from_msgpack
(
InputType
&&
i
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
)
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
i
)).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
)
)).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
...
@@ -21637,7 +21758,7 @@ class basic_json
...
@@ -21637,7 +21758,7 @@ class basic_json
@copydoc from_msgpack(detail::input_adapter&&, const bool, const bool)
@copydoc from_msgpack(detail::input_adapter&&, const bool, const bool)
*/
*/
template
<
typename
A1
,
typename
A2
,
template
<
typename
A1
,
typename
A2
,
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
span_
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_msgpack
(
A1
&&
a1
,
A2
&&
a2
,
static
basic_json
from_msgpack
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
...
@@ -21645,10 +21766,23 @@ class basic_json
...
@@ -21645,10 +21766,23 @@ class basic_json
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)
)).
sax_parse
(
input_format_t
::
msgpack
,
&
sdp
,
strict
);
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_msgpack
(
detail
::
span_input_adapter
&&
i
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
/*!
/*!
@brief create a JSON value from an input in UBJSON format
@brief create a JSON value from an input in UBJSON format
...
@@ -21710,14 +21844,15 @@ class basic_json
...
@@ -21710,14 +21844,15 @@ class basic_json
@since version 3.1.0; added @a allow_exceptions parameter since 3.2.0
@since version 3.1.0; added @a allow_exceptions parameter since 3.2.0
*/
*/
template
<
typename
InputType
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_ubjson
(
detail
::
input_adapter
&&
i
,
static
basic_json
from_ubjson
(
InputType
&&
i
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
)
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
i
)).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
)
)).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
...
@@ -21725,7 +21860,7 @@ class basic_json
...
@@ -21725,7 +21860,7 @@ class basic_json
@copydoc from_ubjson(detail::input_adapter&&, const bool, const bool)
@copydoc from_ubjson(detail::input_adapter&&, const bool, const bool)
*/
*/
template
<
typename
A1
,
typename
A2
,
template
<
typename
A1
,
typename
A2
,
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
span_
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_ubjson
(
A1
&&
a1
,
A2
&&
a2
,
static
basic_json
from_ubjson
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
...
@@ -21733,10 +21868,22 @@ class basic_json
...
@@ -21733,10 +21868,22 @@ class basic_json
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
))).
sax_parse
(
input_format_t
::
ubjson
,
&
sdp
,
strict
);
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
static
basic_json
from_ubjson
(
detail
::
span_input_adapter
&&
i
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
/*!
/*!
@brief Create a JSON value from an input in BSON format
@brief Create a JSON value from an input in BSON format
...
@@ -21797,14 +21944,15 @@ class basic_json
...
@@ -21797,14 +21944,15 @@ class basic_json
@sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
@sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
related UBJSON format
related UBJSON format
*/
*/
template
<
typename
InputType
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_bson
(
detail
::
input_adapter
&&
i
,
static
basic_json
from_bson
(
InputType
&&
i
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
)
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
i
)).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
)
)).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
...
@@ -21812,7 +21960,7 @@ class basic_json
...
@@ -21812,7 +21960,7 @@ class basic_json
@copydoc from_bson(detail::input_adapter&&, const bool, const bool)
@copydoc from_bson(detail::input_adapter&&, const bool, const bool)
*/
*/
template
<
typename
A1
,
typename
A2
,
template
<
typename
A1
,
typename
A2
,
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
span_
input_adapter
,
A1
,
A2
>::
value
,
int
>
=
0
>
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_bson
(
A1
&&
a1
,
A2
&&
a2
,
static
basic_json
from_bson
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
,
const
bool
strict
=
true
,
...
@@ -21820,12 +21968,20 @@ class basic_json
...
@@ -21820,12 +21968,20 @@ class basic_json
{
{
basic_json
result
;
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
)
)).
sax_parse
(
input_format_t
::
bson
,
&
sdp
,
strict
);
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
}
JSON_HEDLEY_WARN_UNUSED_RESULT
static
basic_json
from_bson
(
detail
::
span_input_adapter
&&
i
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
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
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
/// @}
/// @}
//////////////////////////
//////////////////////////
...
...
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