Skip to content

Commit 24cf48f

Browse files
committed
making exception handling not print out stuff, instead the exception object has more information
1 parent 7c6d507 commit 24cf48f

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

include/cpp-json/exception.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
namespace json {
66

77
// general error
8-
class exception : public std::exception {};
8+
class exception : public std::exception {
9+
public:
10+
exception() : location(-1) {
11+
}
12+
public:
13+
int location;
14+
};
915

1016
// parsing errors
1117
class boolean_expected : public exception {};

include/cpp-json/json.tcc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@ namespace json {
66
namespace detail {
77

88
template <class In, class Tr>
9-
void print_exception_details_internal(In first, In current, In last, const Tr&) {
9+
int distance_in_stream_internal(In first, In current, const Tr&) {
1010
(void)first;
1111
(void)current;
12-
(void)last;
12+
return -1;
1313
}
1414

1515
template <class In>
16-
void print_exception_details_internal(In first, In current, In last, const std::random_access_iterator_tag &) {
17-
(void)last;
18-
std::cerr << "an error occured " << std::distance(first, current) << " characters into the stream:" << std::endl;
16+
int distance_in_stream_internal(In first, In current, const std::random_access_iterator_tag &) {
17+
return std::distance(first, current);
1918
}
2019

2120
template <class In>
22-
void print_exception_details(In first, In current, In last) {
21+
int distance_in_stream(In first, In current) {
2322
typedef typename std::iterator_traits<In>::iterator_category Cat;
24-
print_exception_details_internal(first, current, last, Cat());
23+
return distance_in_stream_internal(first, current, Cat());
2524
}
2625

2726
}
@@ -33,8 +32,8 @@ value parse(In first, In last) {
3332

3433
try {
3534
return p.parse();
36-
} catch(const exception &e) {
37-
detail::print_exception_details(p.begin(), p.current(), p.end());
35+
} catch(exception &e) {
36+
e.location = detail::distance_in_stream(p.begin(), p.current());
3837
throw;
3938
}
4039
}
@@ -55,7 +54,11 @@ inline double to_number(const value &v) {
5554
if(!is_number(v)) {
5655
throw invalid_type_cast();
5756
}
57+
#if __cplusplus >= 201103L
58+
return stod(as_string(v), 0);
59+
#else
5860
return strtod(as_string(v).c_str(), 0);
61+
#endif
5962
}
6063

6164
inline object to_object(const value &v) {

0 commit comments

Comments
 (0)