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
74520d8b
Unverified
Commit
74520d8b
authored
Jun 17, 2020
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🚧
extend API
parent
e9bfcf72
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
148 additions
and
66 deletions
+148
-66
include/nlohmann/detail/input/lexer.hpp
include/nlohmann/detail/input/lexer.hpp
+8
-0
include/nlohmann/detail/input/parser.hpp
include/nlohmann/detail/input/parser.hpp
+5
-2
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+42
-21
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+55
-23
test/src/unit-class_lexer.cpp
test/src/unit-class_lexer.cpp
+38
-20
No files found.
include/nlohmann/detail/input/lexer.hpp
View file @
74520d8b
...
...
@@ -837,6 +837,7 @@ class lexer : public lexer_base<BasicJsonType>
{
switch
(
get
())
{
// single-line comments skip input until a newline or EOF is read
case
'/'
:
{
while
(
true
)
...
...
@@ -845,6 +846,8 @@ class lexer : public lexer_base<BasicJsonType>
{
case
'\n'
:
case
'\r'
:
case
std
:
:
char_traits
<
char_type
>::
eof
()
:
case
'\0'
:
return
true
;
default:
...
...
@@ -853,6 +856,7 @@ class lexer : public lexer_base<BasicJsonType>
}
}
// multi-line comments skip input until */ is read
case
'*'
:
{
while
(
true
)
...
...
@@ -877,10 +881,14 @@ class lexer : public lexer_base<BasicJsonType>
}
}
}
default:
break
;
}
}
}
// unexpected character after reading '/'
default:
return
false
;
}
...
...
include/nlohmann/detail/input/parser.hpp
View file @
74520d8b
...
...
@@ -63,8 +63,11 @@ class parser
/// a parser reading from an input adapter
explicit
parser
(
InputAdapterType
&&
adapter
,
const
parser_callback_t
<
BasicJsonType
>
cb
=
nullptr
,
const
bool
allow_exceptions_
=
true
)
:
callback
(
cb
),
m_lexer
(
std
::
move
(
adapter
)),
allow_exceptions
(
allow_exceptions_
)
const
bool
allow_exceptions_
=
true
,
const
bool
skip_comments
=
false
)
:
callback
(
cb
)
,
m_lexer
(
std
::
move
(
adapter
),
skip_comments
)
,
allow_exceptions
(
allow_exceptions_
)
{
// read first token
get_token
();
...
...
include/nlohmann/json.hpp
View file @
74520d8b
...
...
@@ -196,10 +196,12 @@ class basic_json
static
::
nlohmann
::
detail
::
parser
<
basic_json
,
InputAdapterType
>
parser
(
InputAdapterType
adapter
,
detail
::
parser_callback_t
<
basic_json
>
cb
=
nullptr
,
bool
allow_exceptions
=
true
const
bool
allow_exceptions
=
true
,
const
bool
ignore_comments
=
false
)
{
return
::
nlohmann
::
detail
::
parser
<
basic_json
,
InputAdapterType
>
(
std
::
move
(
adapter
),
std
::
move
(
cb
),
allow_exceptions
);
return
::
nlohmann
::
detail
::
parser
<
basic_json
,
InputAdapterType
>
(
std
::
move
(
adapter
),
std
::
move
(
cb
),
allow_exceptions
,
ignore_comments
);
}
using
primitive_iterator_t
=
::
nlohmann
::
detail
::
primitive_iterator_t
;
...
...
@@ -6563,6 +6565,8 @@ class basic_json
(optional)
@param[in] allow_exceptions whether to throw exceptions in case of a
parse error (optional, true by default)
@param[in] ignore_comments whether comments should be ignored (true) or
yield a parse error (true); (optional, false by default)
@return deserialized JSON value; in case of a parse error and
@a allow_exceptions set to `false`, the return value will be
...
...
@@ -6591,16 +6595,18 @@ class basic_json
@liveexample{The example below demonstrates the `parse()` function reading
from a contiguous container.,parse__contiguouscontainer__parser_callback_t}
@since version 2.0.3 (contiguous containers)
@since version 2.0.3 (contiguous containers); version 3.9.0 allowed to
ignore comments.
*/
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
)
const
bool
allow_exceptions
=
true
,
const
bool
ignore_comments
=
false
)
{
basic_json
result
;
parser
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
)),
cb
,
allow_exceptions
).
parse
(
true
,
result
);
parser
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
)),
cb
,
allow_exceptions
,
ignore_comments
).
parse
(
true
,
result
);
return
result
;
}
...
...
@@ -6617,6 +6623,8 @@ class basic_json
(optional)
@param[in] allow_exceptions whether to throw exceptions in case of a
parse error (optional, true by default)
@param[in] ignore_comments whether comments should be ignored (true) or
yield a parse error (true); (optional, false by default)
@return deserialized JSON value; in case of a parse error and
@a allow_exceptions set to `false`, the return value will be
...
...
@@ -6632,10 +6640,11 @@ class basic_json
static
basic_json
parse
(
IteratorType
first
,
IteratorType
last
,
const
parser_callback_t
cb
=
nullptr
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
,
const
bool
ignore_comments
=
false
)
{
basic_json
result
;
parser
(
detail
::
input_adapter
(
std
::
move
(
first
),
std
::
move
(
last
)),
cb
,
allow_exceptions
).
parse
(
true
,
result
);
parser
(
detail
::
input_adapter
(
std
::
move
(
first
),
std
::
move
(
last
)),
cb
,
allow_exceptions
,
ignore_comments
).
parse
(
true
,
result
);
return
result
;
}
...
...
@@ -6643,10 +6652,11 @@ class basic_json
JSON_HEDLEY_DEPRECATED_FOR
(
3.8.0
,
parse
(
ptr
,
ptr
+
len
))
static
basic_json
parse
(
detail
::
span_input_adapter
&&
i
,
const
parser_callback_t
cb
=
nullptr
,
const
bool
allow_exceptions
=
true
)
const
bool
allow_exceptions
=
true
,
const
bool
ignore_comments
=
false
)
{
basic_json
result
;
parser
(
i
.
get
(),
cb
,
allow_exceptions
).
parse
(
true
,
result
);
parser
(
i
.
get
(),
cb
,
allow_exceptions
,
ignore_comments
).
parse
(
true
,
result
);
return
result
;
}
...
...
@@ -6666,6 +6676,8 @@ class basic_json
iterators.
@param[in] i input to read from
@param[in] ignore_comments whether comments should be ignored (true) or
yield a parse error (true); (optional, false by default)
@return Whether the input read from @a i is valid JSON.
...
...
@@ -6678,22 +6690,25 @@ class basic_json
from a string.,accept__string}
*/
template
<
typename
InputType
>
static
bool
accept
(
InputType
&&
i
)
static
bool
accept
(
InputType
&&
i
,
const
bool
ignore_comments
=
false
)
{
return
parser
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
))).
accept
(
true
);
return
parser
(
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
))
,
nullptr
,
false
,
ignore_comments
).
accept
(
true
);
}
template
<
typename
IteratorType
>
static
bool
accept
(
IteratorType
first
,
IteratorType
last
)
static
bool
accept
(
IteratorType
first
,
IteratorType
last
,
const
bool
ignore_comments
=
false
)
{
return
parser
(
detail
::
input_adapter
(
std
::
move
(
first
),
std
::
move
(
last
))).
accept
(
true
);
return
parser
(
detail
::
input_adapter
(
std
::
move
(
first
),
std
::
move
(
last
))
,
nullptr
,
false
,
ignore_comments
).
accept
(
true
);
}
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_DEPRECATED_FOR
(
3.8.0
,
accept
(
ptr
,
ptr
+
len
))
static
bool
accept
(
detail
::
span_input_adapter
&&
i
)
static
bool
accept
(
detail
::
span_input_adapter
&&
i
,
const
bool
ignore_comments
=
false
)
{
return
parser
(
i
.
get
()).
accept
(
true
);
return
parser
(
i
.
get
()
,
nullptr
,
false
,
ignore_comments
).
accept
(
true
);
}
/*!
...
...
@@ -6713,6 +6728,9 @@ class basic_json
@param[in,out] sax SAX event listener
@param[in] format the format to parse (JSON, CBOR, MessagePack, or UBJSON)
@param[in] strict whether the input has to be consumed completely
@param[in] ignore_comments whether comments should be ignored (true) or
yield a parse error (true); (optional, false by default); only applieds to
the JSON file format.
@return return value of the last processed SAX event
...
...
@@ -6737,11 +6755,12 @@ class basic_json
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
)
const
bool
strict
=
true
,
const
bool
ignore_comments
=
false
)
{
auto
ia
=
detail
::
input_adapter
(
std
::
forward
<
InputType
>
(
i
));
return
format
==
input_format_t
::
json
?
parser
(
std
::
move
(
ia
)).
sax_parse
(
sax
,
strict
)
?
parser
(
std
::
move
(
ia
)
,
nullptr
,
true
,
ignore_comments
).
sax_parse
(
sax
,
strict
)
:
detail
::
binary_reader
<
basic_json
,
decltype
(
ia
),
SAX
>
(
std
::
move
(
ia
)).
sax_parse
(
format
,
sax
,
strict
);
}
...
...
@@ -6749,11 +6768,12 @@ class basic_json
JSON_HEDLEY_NON_NULL
(
3
)
static
bool
sax_parse
(
IteratorType
first
,
IteratorType
last
,
SAX
*
sax
,
input_format_t
format
=
input_format_t
::
json
,
const
bool
strict
=
true
)
const
bool
strict
=
true
,
const
bool
ignore_comments
=
false
)
{
auto
ia
=
detail
::
input_adapter
(
std
::
move
(
first
),
std
::
move
(
last
));
return
format
==
input_format_t
::
json
?
parser
(
std
::
move
(
ia
)).
sax_parse
(
sax
,
strict
)
?
parser
(
std
::
move
(
ia
)
,
nullptr
,
true
,
ignore_comments
).
sax_parse
(
sax
,
strict
)
:
detail
::
binary_reader
<
basic_json
,
decltype
(
ia
),
SAX
>
(
std
::
move
(
ia
)).
sax_parse
(
format
,
sax
,
strict
);
}
...
...
@@ -6762,11 +6782,12 @@ class basic_json
JSON_HEDLEY_NON_NULL
(
2
)
static
bool
sax_parse
(
detail
::
span_input_adapter
&&
i
,
SAX
*
sax
,
input_format_t
format
=
input_format_t
::
json
,
const
bool
strict
=
true
)
const
bool
strict
=
true
,
const
bool
ignore_comments
=
false
)
{
auto
ia
=
i
.
get
();
return
format
==
input_format_t
::
json
?
parser
(
std
::
move
(
ia
)).
sax_parse
(
sax
,
strict
)
?
parser
(
std
::
move
(
ia
)
,
nullptr
,
true
,
ignore_comments
).
sax_parse
(
sax
,
strict
)
:
detail
::
binary_reader
<
basic_json
,
decltype
(
ia
),
SAX
>
(
std
::
move
(
ia
)).
sax_parse
(
format
,
sax
,
strict
);
}
...
...
single_include/nlohmann/json.hpp
View file @
74520d8b
This diff is collapsed.
Click to expand it.
test/src/unit-class_lexer.cpp
View file @
74520d8b
...
...
@@ -37,11 +37,11 @@ using nlohmann::json;
namespace
{
// 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
,
const
bool
ignore_comments
=
false
);
json
::
lexer
::
token_type
scan_string
(
const
char
*
s
,
const
bool
ignore_comments
)
{
auto
ia
=
nlohmann
::
detail
::
input_adapter
(
s
);
return
nlohmann
::
detail
::
lexer
<
json
,
decltype
(
ia
)
>
(
std
::
move
(
ia
)).
scan
();
return
nlohmann
::
detail
::
lexer
<
json
,
decltype
(
ia
)
>
(
std
::
move
(
ia
)
,
ignore_comments
).
scan
();
}
}
...
...
@@ -163,9 +163,6 @@ TEST_CASE("lexer class")
break
;
}
// case ('/'):
// break;
// anything else is not expected
default:
{
...
...
@@ -185,18 +182,39 @@ TEST_CASE("lexer class")
CHECK
((
scan_string
(
s
.
c_str
())
==
json
::
lexer
::
token_type
::
value_string
));
}
// SECTION("ignore comments")
// {
// CHECK((scan_string("/") == json::lexer::token_type::parse_error));
//
// CHECK((scan_string("/!") == json::lexer::token_type::parse_error));
// CHECK((scan_string("/*") == json::lexer::token_type::parse_error));
// CHECK((scan_string("/**") == json::lexer::token_type::parse_error));
//
// CHECK((scan_string("//") == json::lexer::token_type::end_of_input));
// CHECK((scan_string("/**/") == json::lexer::token_type::end_of_input));
// CHECK((scan_string("/** /") == json::lexer::token_type::parse_error));
//
// CHECK((scan_string("/***/") == json::lexer::token_type::end_of_input));
// }
SECTION
(
"fail on comments"
)
{
CHECK
((
scan_string
(
"/"
,
false
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/!"
,
false
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/*"
,
false
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/**"
,
false
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"//"
,
false
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/**/"
,
false
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/** /"
,
false
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/***/"
,
false
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/* true */"
,
false
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/*/**/"
,
false
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/*/* */"
,
false
)
==
json
::
lexer
::
token_type
::
parse_error
));
}
SECTION
(
"ignore comments"
)
{
CHECK
((
scan_string
(
"/"
,
true
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/!"
,
true
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/*"
,
true
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/**"
,
true
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"//"
,
true
)
==
json
::
lexer
::
token_type
::
end_of_input
));
CHECK
((
scan_string
(
"/**/"
,
true
)
==
json
::
lexer
::
token_type
::
end_of_input
));
CHECK
((
scan_string
(
"/** /"
,
true
)
==
json
::
lexer
::
token_type
::
parse_error
));
CHECK
((
scan_string
(
"/***/"
,
true
)
==
json
::
lexer
::
token_type
::
end_of_input
));
CHECK
((
scan_string
(
"/* true */"
,
true
)
==
json
::
lexer
::
token_type
::
end_of_input
));
CHECK
((
scan_string
(
"/*/**/"
,
true
)
==
json
::
lexer
::
token_type
::
end_of_input
));
CHECK
((
scan_string
(
"/*/* */"
,
true
)
==
json
::
lexer
::
token_type
::
end_of_input
));
}
}
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