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
0585ecc5
Unverified
Commit
0585ecc5
authored
Jun 19, 2020
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✅
add tests for comment skipping
parent
74520d8b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
3 deletions
+58
-3
lexer.hpp
include/nlohmann/detail/input/lexer.hpp
+7
-1
json.hpp
single_include/nlohmann/json.hpp
+7
-1
unit-class_parser.cpp
test/src/unit-class_parser.cpp
+44
-1
No files found.
include/nlohmann/detail/input/lexer.hpp
View file @
0585ecc5
...
@@ -1507,7 +1507,13 @@ scan_number_done:
...
@@ -1507,7 +1507,13 @@ scan_number_done:
error_message
=
"invalid comment"
;
error_message
=
"invalid comment"
;
return
token_type
::
parse_error
;
return
token_type
::
parse_error
;
}
}
get
();
// skip following whitespace
do
{
get
();
}
while
(
current
==
' '
or
current
==
'\t'
or
current
==
'\n'
or
current
==
'\r'
);
}
}
switch
(
current
)
switch
(
current
)
...
...
single_include/nlohmann/json.hpp
View file @
0585ecc5
...
@@ -9574,7 +9574,13 @@ scan_number_done:
...
@@ -9574,7 +9574,13 @@ scan_number_done:
error_message
=
"invalid comment"
;
error_message
=
"invalid comment"
;
return
token_type
::
parse_error
;
return
token_type
::
parse_error
;
}
}
get
();
// skip following whitespace
do
{
get
();
}
while
(
current
==
' '
or
current
==
'\t'
or
current
==
'\n'
or
current
==
'\r'
);
}
}
switch
(
current
)
switch
(
current
)
...
...
test/src/unit-class_parser.cpp
View file @
0585ecc5
...
@@ -224,6 +224,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
...
@@ -224,6 +224,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
json
parser_helper
(
const
std
::
string
&
s
);
json
parser_helper
(
const
std
::
string
&
s
);
bool
accept_helper
(
const
std
::
string
&
s
);
bool
accept_helper
(
const
std
::
string
&
s
);
void
comments_helper
(
const
std
::
string
&
s
);
json
parser_helper
(
const
std
::
string
&
s
)
json
parser_helper
(
const
std
::
string
&
s
)
{
{
...
@@ -241,6 +242,8 @@ json parser_helper(const std::string& s)
...
@@ -241,6 +242,8 @@ json parser_helper(const std::string& s)
json
::
sax_parse
(
s
,
&
sdp
);
json
::
sax_parse
(
s
,
&
sdp
);
CHECK
(
j_sax
==
j
);
CHECK
(
j_sax
==
j
);
comments_helper
(
s
);
return
j
;
return
j
;
}
}
...
@@ -275,11 +278,51 @@ bool accept_helper(const std::string& s)
...
@@ -275,11 +278,51 @@ bool accept_helper(const std::string& s)
// 6. check if this approach came to the same result
// 6. check if this approach came to the same result
CHECK
(
ok_noexcept
==
ok_noexcept_cb
);
CHECK
(
ok_noexcept
==
ok_noexcept_cb
);
// 7. return result
// 7. check if comments are properly ignored
if
(
ok_accept
)
{
comments_helper
(
s
);
}
// 8. return result
return
ok_accept
;
return
ok_accept
;
}
}
void
comments_helper
(
const
std
::
string
&
s
)
{
json
_
;
// parse/accept with default parser
CHECK_NOTHROW
(
_
=
json
::
parse
(
s
));
CHECK
(
json
::
accept
(
s
));
// parse/accept while skipping comments
CHECK_NOTHROW
(
_
=
json
::
parse
(
s
,
nullptr
,
false
,
true
));
CHECK
(
json
::
accept
(
s
,
true
));
std
::
vector
<
std
::
string
>
json_with_comments
;
// start with a comment
json_with_comments
.
push_back
(
std
::
string
(
"// this is a comment
\n
"
)
+
s
);
json_with_comments
.
push_back
(
std
::
string
(
"/* this is a comment */"
)
+
s
);
// end with a comment
json_with_comments
.
push_back
(
s
+
"// this is a comment"
);
json_with_comments
.
push_back
(
s
+
"/* this is a comment */"
);
// check all strings
for
(
const
auto
&
json_with_comment
:
json_with_comments
)
{
CAPTURE
(
json_with_comment
)
CHECK_THROWS_AS
(
_
=
json
::
parse
(
json_with_comment
),
json
::
parse_error
);
CHECK
(
not
json
::
accept
(
json_with_comment
));
CHECK_NOTHROW
(
_
=
json
::
parse
(
json_with_comment
,
nullptr
,
true
,
true
));
CHECK
(
json
::
accept
(
json_with_comment
,
true
));
}
}
}
}
// namespace
TEST_CASE
(
"parser class"
)
TEST_CASE
(
"parser class"
)
{
{
SECTION
(
"parse"
)
SECTION
(
"parse"
)
...
...
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