Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
c4c23f34
Unverified
Commit
c4c23f34
authored
May 14, 2020
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' of
https://github.com/nlohmann/json
into release_cleanup
parents
33ae93be
08571408
Changes
10
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
712 additions
and
419 deletions
+712
-419
include/nlohmann/detail/input/binary_reader.hpp
include/nlohmann/detail/input/binary_reader.hpp
+20
-19
include/nlohmann/detail/input/input_adapters.hpp
include/nlohmann/detail/input/input_adapters.hpp
+148
-111
include/nlohmann/detail/input/lexer.hpp
include/nlohmann/detail/input/lexer.hpp
+25
-19
include/nlohmann/detail/input/parser.hpp
include/nlohmann/detail/input/parser.hpp
+25
-24
include/nlohmann/detail/output/binary_writer.hpp
include/nlohmann/detail/output/binary_writer.hpp
+2
-4
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+131
-32
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+351
-209
test/src/unit-class_lexer.cpp
test/src/unit-class_lexer.cpp
+2
-1
test/src/unit-json_pointer.cpp
test/src/unit-json_pointer.cpp
+3
-0
test/src/unit-regression.cpp
test/src/unit-regression.cpp
+5
-0
No files found.
include/nlohmann/detail/input/binary_reader.hpp
View file @
c4c23f34
...
...
@@ -24,6 +24,20 @@ namespace nlohmann
{
namespace
detail
{
/*!
@brief determine system byte order
@return true if and only if system's byte order is little endian
@note from https://stackoverflow.com/a/1001328/266378
*/
static
bool
little_endianess
(
int
num
=
1
)
noexcept
{
return
*
reinterpret_cast
<
char
*>
(
&
num
)
==
1
;
}
///////////////////
// binary reader //
///////////////////
...
...
@@ -31,7 +45,7 @@ namespace detail
/*!
@brief deserialization of CBOR, MessagePack, and UBJSON values
*/
template
<
typename
BasicJsonType
,
typename
SAX
=
json_sax_dom_parser
<
BasicJsonType
>
>
template
<
typename
BasicJsonType
,
typename
InputAdapterType
,
typename
SAX
=
json_sax_dom_parser
<
BasicJsonType
>
>
class
binary_reader
{
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
...
...
@@ -47,10 +61,9 @@ class binary_reader
@param[in] adapter input adapter to read from
*/
explicit
binary_reader
(
input_adapter_t
adapter
)
:
ia
(
std
::
move
(
adapter
))
explicit
binary_reader
(
InputAdapterType
&&
adapter
)
:
ia
(
std
::
move
(
adapter
))
{
(
void
)
detail
::
is_sax_static_asserts
<
SAX
,
BasicJsonType
>
{};
assert
(
ia
);
}
// make class move-only
...
...
@@ -119,18 +132,6 @@ class binary_reader
return
result
;
}
/*!
@brief determine system byte order
@return true if and only if system's byte order is little endian
@note from https://stackoverflow.com/a/1001328/266378
*/
static
constexpr
bool
little_endianess
(
int
num
=
1
)
noexcept
{
return
*
reinterpret_cast
<
char
*>
(
&
num
)
==
1
;
}
private:
//////////
// BSON //
...
...
@@ -1577,8 +1578,8 @@ class binary_reader
return
get_number
(
input_format_t
::
msgpack
,
result
.
subtype
)
and
get_binary
(
input_format_t
::
msgpack
,
16
,
result
);
}
default:
// LCOV_EXCL_LINE
assert
(
false
)
;
// LCOV_EXCL_LINE
default:
// LCOV_EXCL_LINE
return
false
;
// LCOV_EXCL_LINE
}
}
...
...
@@ -2085,7 +2086,7 @@ class binary_reader
int
get
()
{
++
chars_read
;
return
current
=
ia
->
get_character
();
return
current
=
ia
.
get_character
();
}
/*!
...
...
@@ -2273,7 +2274,7 @@ class binary_reader
private:
/// input adapter
input_adapter_t
ia
=
nullptr
;
InputAdapterType
ia
;
/// the current character
int
current
=
std
::
char_traits
<
char
>::
eof
();
...
...
include/nlohmann/detail/input/input_adapters.hpp
View file @
c4c23f34
This diff is collapsed.
Click to expand it.
include/nlohmann/detail/input/lexer.hpp
View file @
c4c23f34
...
...
@@ -22,19 +22,9 @@ namespace detail
// lexer //
///////////
/*!
@brief lexical analysis
This class organizes the lexical analysis during JSON deserialization.
*/
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:
/// token types for the parser
enum
class
token_type
...
...
@@ -75,9 +65,9 @@ class lexer
return
"null literal"
;
case
token_type
:
:
value_string
:
return
"string literal"
;
case
lexer
:
:
token_type
::
value_unsigned
:
case
lexer
:
:
token_type
::
value_integer
:
case
lexer
:
:
token_type
::
value_float
:
case
token_type
:
:
value_unsigned
:
case
token_type
:
:
value_integer
:
case
token_type
:
:
value_float
:
return
"number literal"
;
case
token_type
:
:
begin_array
:
return
"'['"
;
...
...
@@ -103,15 +93,31 @@ class lexer
// LCOV_EXCL_STOP
}
}
};
/*!
@brief lexical analysis
This class organizes the lexical analysis during JSON deserialization.
*/
template
<
typename
BasicJsonType
,
typename
InputAdapterType
>
class
lexer
:
public
lexer_base
<
BasicJsonType
>
{
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_adapter_t
&&
adapter
)
explicit
lexer
(
InputAdapterType
&&
adapter
)
:
ia
(
std
::
move
(
adapter
)),
decimal_point_char
(
get_decimal_point
())
{}
// delete because of pointer members
lexer
(
const
lexer
&
)
=
delete
;
lexer
(
lexer
&&
)
=
de
lete
;
lexer
(
lexer
&&
)
=
de
fault
;
lexer
&
operator
=
(
lexer
&
)
=
delete
;
lexer
&
operator
=
(
lexer
&&
)
=
de
lete
;
lexer
&
operator
=
(
lexer
&&
)
=
de
fault
;
~
lexer
()
=
default
;
private:
...
...
@@ -1256,7 +1262,7 @@ scan_number_done:
}
else
{
current
=
ia
->
get_character
();
current
=
ia
.
get_character
();
}
if
(
JSON_HEDLEY_LIKELY
(
current
!=
std
::
char_traits
<
char
>::
eof
()))
...
...
@@ -1480,7 +1486,7 @@ scan_number_done:
private:
/// input adapter
detail
::
input_adapter_t
ia
=
nullptr
;
InputAdapterType
ia
;
/// the current character
std
::
char_traits
<
char
>::
int_type
current
=
std
::
char_traits
<
char
>::
eof
();
...
...
include/nlohmann/detail/input/parser.hpp
View file @
c4c23f34
...
...
@@ -24,44 +24,45 @@ namespace detail
// 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
This class implements a recursive descent parser.
*/
template
<
typename
BasicJsonType
>
template
<
typename
BasicJsonType
,
typename
InputAdapterType
>
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
lexer_t
=
lexer
<
BasicJsonType
,
InputAdapterType
>
;
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
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
explicit
parser
(
detail
::
input_adapter_t
&&
adapter
,
const
parser_callback_t
cb
=
nullptr
,
explicit
parser
(
InputAdapterType
&&
adapter
,
const
parser_callback_t
<
BasicJsonType
>
cb
=
nullptr
,
const
bool
allow_exceptions_
=
true
)
:
callback
(
cb
),
m_lexer
(
std
::
move
(
adapter
)),
allow_exceptions
(
allow_exceptions_
)
{
...
...
@@ -486,7 +487,7 @@ class parser
private:
/// callback function
const
parser_callback_t
callback
=
nullptr
;
const
parser_callback_t
<
BasicJsonType
>
callback
=
nullptr
;
/// the type of the last read token
token_type
last_token
=
token_type
::
uninitialized
;
/// the lexer
...
...
include/nlohmann/detail/output/binary_writer.hpp
View file @
c4c23f34
...
...
@@ -1092,9 +1092,7 @@ class binary_writer
}
write_number
(
subtype
);
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
value
.
data
()),
value
.
size
());
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
value
.
data
()),
value
.
size
());
}
/*!
...
...
@@ -1540,7 +1538,7 @@ class binary_writer
private:
/// whether we can assume little endianess
const
bool
is_little_endian
=
binary_reader
<
BasicJsonType
>::
little_endianess
();
const
bool
is_little_endian
=
little_endianess
();
/// the output
output_adapter_t
<
CharType
>
oa
=
nullptr
;
...
...
include/nlohmann/json.hpp
View file @
c4c23f34
This diff is collapsed.
Click to expand it.
single_include/nlohmann/json.hpp
View file @
c4c23f34
This diff is collapsed.
Click to expand it.
test/src/unit-class_lexer.cpp
View file @
c4c23f34
...
...
@@ -40,7 +40,8 @@ namespace
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
ia
=
nlohmann
::
detail
::
input_adapter
(
s
);
return
nlohmann
::
detail
::
lexer
<
json
,
decltype
(
ia
)
>
(
std
::
move
(
ia
)).
scan
();
}
}
...
...
test/src/unit-json_pointer.cpp
View file @
c4c23f34
...
...
@@ -101,6 +101,9 @@ TEST_CASE("JSON pointers")
CHECK
(
j
[
"/foo/1"
_json_pointer
]
==
j
[
"foo"
][
1
]);
CHECK
(
j
.
contains
(
json
::
json_pointer
(
"/foo/0"
)));
CHECK
(
j
.
contains
(
json
::
json_pointer
(
"/foo/1"
)));
CHECK
(
not
j
.
contains
(
json
::
json_pointer
(
"/foo/3"
)));
CHECK
(
not
j
.
contains
(
json
::
json_pointer
(
"/foo/+"
)));
CHECK
(
not
j
.
contains
(
json
::
json_pointer
(
"/foo/1+2"
)));
CHECK
(
not
j
.
contains
(
json
::
json_pointer
(
"/foo/-"
)));
// checked array access
...
...
test/src/unit-regression.cpp
View file @
c4c23f34
...
...
@@ -464,6 +464,11 @@ TEST_CASE("regression tests")
s2
=
o
[
"name"
];
CHECK
(
s2
==
"value"
);
// improve coverage
o
[
"int"
]
=
1
;
CHECK_THROWS_AS
(
s2
=
o
[
"int"
],
json
::
type_error
);
CHECK_THROWS_WITH
(
s2
=
o
[
"int"
],
"[json.exception.type_error.302] type must be string, but is number"
);
}
SECTION
(
"issue #146 - character following a surrogate pair is skipped"
)
...
...
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