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
3d4f6a29
Unverified
Commit
3d4f6a29
authored
Mar 11, 2018
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
cleaner exception interface
parent
ad47b0fb
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
74 additions
and
61 deletions
+74
-61
include/nlohmann/detail/input/binary_reader.hpp
include/nlohmann/detail/input/binary_reader.hpp
+6
-11
include/nlohmann/detail/input/json_sax.hpp
include/nlohmann/detail/input/json_sax.hpp
+20
-10
include/nlohmann/detail/input/parser.hpp
include/nlohmann/detail/input/parser.hpp
+9
-8
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+37
-30
test/src/unit-class_parser.cpp
test/src/unit-class_parser.cpp
+1
-1
test/src/unit-deserialization.cpp
test/src/unit-deserialization.cpp
+1
-1
No files found.
include/nlohmann/detail/input/binary_reader.hpp
View file @
3d4f6a29
...
...
@@ -147,8 +147,7 @@ class binary_reader
{
// EOF
case
std
:
:
char_traits
<
char
>::
eof
()
:
unexpect_eof
();
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
"unexpected end of input"
);
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
parse_error
::
create
(
110
,
chars_read
,
"unexpected end of input"
));
// Integer 0x00..0x17 (0..23)
case
0x00
:
...
...
@@ -399,7 +398,7 @@ class binary_reader
{
std
::
stringstream
ss
;
ss
<<
std
::
setw
(
2
)
<<
std
::
uppercase
<<
std
::
setfill
(
'0'
)
<<
std
::
hex
<<
current
;
JSON_THROW
(
parse_error
::
create
(
112
,
chars_read
,
"error reading CBOR; last byte: 0x"
+
ss
.
str
()));
return
sax
->
parse_error
(
chars_read
,
ss
.
str
(),
parse_error
::
create
(
112
,
chars_read
,
"error reading CBOR; last byte: 0x"
+
ss
.
str
()));
}
}
}
...
...
@@ -410,8 +409,7 @@ class binary_reader
{
// EOF
case
std
:
:
char_traits
<
char
>::
eof
()
:
unexpect_eof
();
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
"unexpected end of input"
);
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
parse_error
::
create
(
110
,
chars_read
,
"unexpected end of input"
));
// positive fixint
case
0x00
:
...
...
@@ -712,8 +710,7 @@ class binary_reader
{
std
::
stringstream
ss
;
ss
<<
std
::
setw
(
2
)
<<
std
::
uppercase
<<
std
::
setfill
(
'0'
)
<<
std
::
hex
<<
current
;
JSON_THROW
(
parse_error
::
create
(
112
,
chars_read
,
"error reading MessagePack; last byte: 0x"
+
ss
.
str
()));
return
sax
->
parse_error
(
chars_read
,
ss
.
str
(),
parse_error
::
create
(
112
,
chars_read
,
"error reading MessagePack; last byte: 0x"
+
ss
.
str
()));
}
}
}
...
...
@@ -1189,8 +1186,7 @@ class binary_reader
switch
(
prefix
)
{
case
std
:
:
char_traits
<
char
>::
eof
()
:
// EOF
unexpect_eof
();
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
"unexpected end of input"
);
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
parse_error
::
create
(
110
,
chars_read
,
"unexpected end of input"
));
case
'T'
:
// true
return
sax
->
boolean
(
true
);
...
...
@@ -1223,8 +1219,7 @@ class binary_reader
{
std
::
stringstream
ss
;
ss
<<
std
::
setw
(
2
)
<<
std
::
uppercase
<<
std
::
setfill
(
'0'
)
<<
std
::
hex
<<
current
;
JSON_THROW
(
parse_error
::
create
(
113
,
chars_read
,
"byte after 'C' must be in range 0x00..0x7F; last byte: 0x"
+
ss
.
str
()));
return
sax
->
parse_error
(
chars_read
,
ss
.
str
(),
parse_error
::
create
(
113
,
chars_read
,
"byte after 'C' must be in range 0x00..0x7F; last byte: 0x"
+
ss
.
str
()));
}
return
sax
->
string
(
string_t
(
1
,
static_cast
<
char
>
(
current
)));
}
...
...
include/nlohmann/detail/input/json_sax.hpp
View file @
3d4f6a29
...
...
@@ -4,6 +4,8 @@
#include <string>
#include <vector>
#include <nlohmann/detail/exceptions.hpp>
namespace
nlohmann
{
...
...
@@ -118,7 +120,7 @@ struct json_sax
*/
virtual
bool
parse_error
(
std
::
size_t
position
,
const
std
::
string
&
last_token
,
const
std
::
string
&
error_msg
)
=
0
;
const
detail
::
exception
&
ex
)
=
0
;
virtual
~
json_sax
()
=
default
;
};
...
...
@@ -224,19 +226,27 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return
true
;
}
bool
parse_error
(
std
::
size_t
position
,
const
std
::
string
&
token
,
const
std
::
string
&
error_msg
)
override
bool
parse_error
(
std
::
size_t
,
const
std
::
string
&
,
const
detail
::
exception
&
ex
)
override
{
errored
=
true
;
if
(
allow_exceptions
)
{
if
(
error_msg
==
"number overflow"
)
{
JSON_THROW
(
BasicJsonType
::
out_of_range
::
create
(
406
,
"number overflow parsing '"
+
token
+
"'"
));
}
else
// determine the proper exception type from the id
switch
((
ex
.
id
/
100
)
%
100
)
{
JSON_THROW
(
BasicJsonType
::
parse_error
::
create
(
101
,
position
,
error_msg
));
case
1
:
JSON_THROW
(
*
reinterpret_cast
<
const
detail
::
parse_error
*>
(
&
ex
));
case
2
:
JSON_THROW
(
*
reinterpret_cast
<
const
detail
::
invalid_iterator
*>
(
&
ex
));
case
3
:
JSON_THROW
(
*
reinterpret_cast
<
const
detail
::
type_error
*>
(
&
ex
));
case
4
:
JSON_THROW
(
*
reinterpret_cast
<
const
detail
::
out_of_range
*>
(
&
ex
));
case
5
:
JSON_THROW
(
*
reinterpret_cast
<
const
detail
::
other_error
*>
(
&
ex
));
default:
assert
(
false
);
// LCOV_EXCL_LINE
}
}
return
false
;
...
...
@@ -359,7 +369,7 @@ class json_sax_acceptor : public json_sax<BasicJsonType>
return
true
;
}
bool
parse_error
(
std
::
size_t
,
const
std
::
string
&
,
const
std
::
string
&
)
override
bool
parse_error
(
std
::
size_t
,
const
std
::
string
&
,
const
detail
::
exception
&
)
override
{
return
false
;
}
...
...
include/nlohmann/detail/input/parser.hpp
View file @
3d4f6a29
...
...
@@ -117,7 +117,7 @@ class parser
{
sdp
.
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
end_of_input
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
end_of_input
)
));
}
// in case of an error, return discarded value
...
...
@@ -456,7 +456,7 @@ class parser
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
value_string
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
value_string
)
));
}
else
{
...
...
@@ -472,7 +472,7 @@ class parser
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
name_separator
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
name_separator
)
));
}
// parse value
...
...
@@ -499,7 +499,8 @@ class parser
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
end_object
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
end_object
)));
}
}
}
...
...
@@ -546,7 +547,7 @@ class parser
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
end_array
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
end_array
)
));
}
}
}
...
...
@@ -559,7 +560,7 @@ class parser
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
"number overflow"
);
out_of_range
::
create
(
406
,
"number overflow parsing '"
+
m_lexer
.
get_token_string
()
+
"'"
)
);
}
else
{
...
...
@@ -602,14 +603,14 @@ class parser
// using "uninitialized" to avoid "expected" message
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
uninitialized
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
uninitialized
)
));
}
default:
// the last token was unexpected
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
literal_or_value
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
literal_or_value
)
));
}
}
}
...
...
single_include/nlohmann/json.hpp
View file @
3d4f6a29
...
...
@@ -3138,6 +3138,9 @@ scan_number_done:
#include <string>
#include <vector>
// #include <nlohmann/detail/exceptions.hpp>
namespace
nlohmann
{
...
...
@@ -3252,7 +3255,7 @@ struct json_sax
*/
virtual
bool
parse_error
(
std
::
size_t
position
,
const
std
::
string
&
last_token
,
const
std
::
string
&
error_msg
)
=
0
;
const
detail
::
exception
&
ex
)
=
0
;
virtual
~
json_sax
()
=
default
;
};
...
...
@@ -3358,19 +3361,27 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return
true
;
}
bool
parse_error
(
std
::
size_t
position
,
const
std
::
string
&
token
,
const
std
::
string
&
error_msg
)
override
bool
parse_error
(
std
::
size_t
,
const
std
::
string
&
,
const
detail
::
exception
&
ex
)
override
{
errored
=
true
;
if
(
allow_exceptions
)
{
if
(
error_msg
==
"number overflow"
)
{
JSON_THROW
(
BasicJsonType
::
out_of_range
::
create
(
406
,
"number overflow parsing '"
+
token
+
"'"
));
}
else
{
JSON_THROW
(
BasicJsonType
::
parse_error
::
create
(
101
,
position
,
error_msg
));
// determine the proper exception type from the id
switch
((
ex
.
id
/
100
)
%
100
)
{
case
1
:
JSON_THROW
(
*
reinterpret_cast
<
const
detail
::
parse_error
*>
(
&
ex
));
case
2
:
JSON_THROW
(
*
reinterpret_cast
<
const
detail
::
invalid_iterator
*>
(
&
ex
));
case
3
:
JSON_THROW
(
*
reinterpret_cast
<
const
detail
::
type_error
*>
(
&
ex
));
case
4
:
JSON_THROW
(
*
reinterpret_cast
<
const
detail
::
out_of_range
*>
(
&
ex
));
case
5
:
JSON_THROW
(
*
reinterpret_cast
<
const
detail
::
other_error
*>
(
&
ex
));
default:
assert
(
false
);
// LCOV_EXCL_LINE
}
}
return
false
;
...
...
@@ -3493,7 +3504,7 @@ class json_sax_acceptor : public json_sax<BasicJsonType>
return
true
;
}
bool
parse_error
(
std
::
size_t
,
const
std
::
string
&
,
const
std
::
string
&
)
override
bool
parse_error
(
std
::
size_t
,
const
std
::
string
&
,
const
detail
::
exception
&
)
override
{
return
false
;
}
...
...
@@ -3611,7 +3622,7 @@ class parser
{
sdp
.
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
end_of_input
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
end_of_input
)
));
}
// in case of an error, return discarded value
...
...
@@ -3950,7 +3961,7 @@ class parser
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
value_string
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
value_string
)
));
}
else
{
...
...
@@ -3966,7 +3977,7 @@ class parser
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
name_separator
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
name_separator
)
));
}
// parse value
...
...
@@ -3993,7 +4004,8 @@ class parser
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
end_object
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
end_object
)));
}
}
}
...
...
@@ -4040,7 +4052,7 @@ class parser
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
end_array
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
end_array
)
));
}
}
}
...
...
@@ -4053,7 +4065,7 @@ class parser
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
"number overflow"
);
out_of_range
::
create
(
406
,
"number overflow parsing '"
+
m_lexer
.
get_token_string
()
+
"'"
)
);
}
else
{
...
...
@@ -4096,14 +4108,14 @@ class parser
// using "uninitialized" to avoid "expected" message
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
uninitialized
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
uninitialized
)
));
}
default:
// the last token was unexpected
{
return
sax
->
parse_error
(
m_lexer
.
get_position
(),
m_lexer
.
get_token_string
(),
exception_message
(
token_type
::
literal_or_value
));
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
exception_message
(
token_type
::
literal_or_value
)
));
}
}
}
...
...
@@ -5437,8 +5449,7 @@ class binary_reader
{
// EOF
case
std
:
:
char_traits
<
char
>::
eof
()
:
unexpect_eof
();
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
"unexpected end of input"
);
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
parse_error
::
create
(
110
,
chars_read
,
"unexpected end of input"
));
// Integer 0x00..0x17 (0..23)
case
0x00
:
...
...
@@ -5689,7 +5700,7 @@ class binary_reader
{
std
::
stringstream
ss
;
ss
<<
std
::
setw
(
2
)
<<
std
::
uppercase
<<
std
::
setfill
(
'0'
)
<<
std
::
hex
<<
current
;
JSON_THROW
(
parse_error
::
create
(
112
,
chars_read
,
"error reading CBOR; last byte: 0x"
+
ss
.
str
()));
return
sax
->
parse_error
(
chars_read
,
ss
.
str
(),
parse_error
::
create
(
112
,
chars_read
,
"error reading CBOR; last byte: 0x"
+
ss
.
str
()));
}
}
}
...
...
@@ -5700,8 +5711,7 @@ class binary_reader
{
// EOF
case
std
:
:
char_traits
<
char
>::
eof
()
:
unexpect_eof
();
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
"unexpected end of input"
);
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
parse_error
::
create
(
110
,
chars_read
,
"unexpected end of input"
));
// positive fixint
case
0x00
:
...
...
@@ -6002,8 +6012,7 @@ class binary_reader
{
std
::
stringstream
ss
;
ss
<<
std
::
setw
(
2
)
<<
std
::
uppercase
<<
std
::
setfill
(
'0'
)
<<
std
::
hex
<<
current
;
JSON_THROW
(
parse_error
::
create
(
112
,
chars_read
,
"error reading MessagePack; last byte: 0x"
+
ss
.
str
()));
return
sax
->
parse_error
(
chars_read
,
ss
.
str
(),
parse_error
::
create
(
112
,
chars_read
,
"error reading MessagePack; last byte: 0x"
+
ss
.
str
()));
}
}
}
...
...
@@ -6479,8 +6488,7 @@ class binary_reader
switch
(
prefix
)
{
case
std
:
:
char_traits
<
char
>::
eof
()
:
// EOF
unexpect_eof
();
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
"unexpected end of input"
);
return
sax
->
parse_error
(
chars_read
,
"<end of file>"
,
parse_error
::
create
(
110
,
chars_read
,
"unexpected end of input"
));
case
'T'
:
// true
return
sax
->
boolean
(
true
);
...
...
@@ -6513,8 +6521,7 @@ class binary_reader
{
std
::
stringstream
ss
;
ss
<<
std
::
setw
(
2
)
<<
std
::
uppercase
<<
std
::
setfill
(
'0'
)
<<
std
::
hex
<<
current
;
JSON_THROW
(
parse_error
::
create
(
113
,
chars_read
,
"byte after 'C' must be in range 0x00..0x7F; last byte: 0x"
+
ss
.
str
()));
return
sax
->
parse_error
(
chars_read
,
ss
.
str
(),
parse_error
::
create
(
113
,
chars_read
,
"byte after 'C' must be in range 0x00..0x7F; last byte: 0x"
+
ss
.
str
()));
}
return
sax
->
string
(
string_t
(
1
,
static_cast
<
char
>
(
current
)));
}
...
...
test/src/unit-class_parser.cpp
View file @
3d4f6a29
...
...
@@ -123,7 +123,7 @@ class SaxEventLogger : public nlohmann::json::json_sax_t
return
true
;
}
bool
parse_error
(
std
::
size_t
position
,
const
std
::
string
&
,
const
std
::
string
&
)
override
bool
parse_error
(
std
::
size_t
position
,
const
std
::
string
&
,
const
json
::
exception
&
)
override
{
errored
=
true
;
events
.
push_back
(
"parse_error("
+
std
::
to_string
(
position
)
+
")"
);
...
...
test/src/unit-deserialization.cpp
View file @
3d4f6a29
...
...
@@ -122,7 +122,7 @@ struct SaxEventLogger : public nlohmann::json::json_sax_t
return
true
;
}
bool
parse_error
(
std
::
size_t
position
,
const
std
::
string
&
,
const
std
::
string
&
)
override
bool
parse_error
(
std
::
size_t
position
,
const
std
::
string
&
,
const
json
::
exception
&
)
override
{
events
.
push_back
(
"parse_error("
+
std
::
to_string
(
position
)
+
")"
);
return
false
;
...
...
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