Add parse error handling to rjp++
This commit is contained in:
parent
d76252d473
commit
92704ae8f0
@ -28,11 +28,29 @@
|
|||||||
|
|
||||||
namespace rjp{
|
namespace rjp{
|
||||||
|
|
||||||
|
class parse_res
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
RJP_value* m_value;
|
||||||
|
RJP_parse_error m_err;
|
||||||
|
|
||||||
|
public:
|
||||||
|
parse_res(RJP_value* val, const RJP_parse_error& err);
|
||||||
|
parse_res(const parse_res*) = delete;
|
||||||
|
parse_res(parse_res&& p);
|
||||||
|
|
||||||
|
~parse_res(void);
|
||||||
|
|
||||||
|
operator bool(void)const;
|
||||||
|
bool valid(void)const;
|
||||||
|
|
||||||
|
value get_value(void);
|
||||||
|
string errstr(void)const;
|
||||||
|
};
|
||||||
|
|
||||||
string to_json(const value& val, int format = RJP_FORMAT_PRETTY);
|
string to_json(const value& val, int format = RJP_FORMAT_PRETTY);
|
||||||
value parse_json(const rexy::string_base& str, RJP_parse_flag = RJP_PARSE_NONE);
|
parse_res parse_json(const rexy::string_base& str, RJP_parse_flag = RJP_PARSE_NONE);
|
||||||
value parse_json(const rexy::string_base& str, RJP_parse_flag, RJP_parse_error&);
|
parse_res parse_json(const char* str, RJP_parse_flag = RJP_PARSE_NONE);
|
||||||
value parse_json(const char* str, RJP_parse_flag = RJP_PARSE_NONE);
|
|
||||||
value parse_json(const char* str, RJP_parse_flag, RJP_parse_error&);
|
|
||||||
namespace detail{
|
namespace detail{
|
||||||
template<int... Indexes>
|
template<int... Indexes>
|
||||||
struct sequence_tup{};
|
struct sequence_tup{};
|
||||||
@ -73,20 +91,14 @@ namespace rjp{
|
|||||||
int irjp_parse_callback(char* dest, int size, void* userdata);
|
int irjp_parse_callback(char* dest, int size, void* userdata);
|
||||||
}
|
}
|
||||||
template<class Func, class... Args>
|
template<class Func, class... Args>
|
||||||
value parse_json(RJP_parse_flag f, Func&& func, Args&&... args){
|
parse_res parse_json(RJP_parse_flag f, Func&& func, Args&&... args){
|
||||||
RJP_parse_callback cb;
|
RJP_parse_callback cb;
|
||||||
detail::invoker_impl<Func,Args...> inv(std::forward<Func>(func), std::forward<Args>(args)...);
|
detail::invoker_impl<Func,Args...> inv(std::forward<Func>(func), std::forward<Args>(args)...);
|
||||||
cb.data = static_cast<void*>(&inv);
|
cb.data = static_cast<void*>(&inv);
|
||||||
cb.read = detail::irjp_parse_callback;
|
cb.read = detail::irjp_parse_callback;
|
||||||
return value(detail::parse_cback(f, &cb), true);
|
RJP_parse_error err = {};
|
||||||
}
|
RJP_value* v = detail::parse_cback(f, &cb, err);
|
||||||
template<class Func, class... Args>
|
return parse_res(v, err);
|
||||||
value parse_json(RJP_parse_flag f, RJP_parse_error& err, Func&& func, Args&&... args){
|
|
||||||
RJP_parse_callback cb;
|
|
||||||
detail::invoker_impl<Func,Args...> inv(std::forward<Func>(func), std::forward<Args>(args)...);
|
|
||||||
cb.data = static_cast<void*>(&inv);
|
|
||||||
cb.read = detail::irjp_parse_callback;
|
|
||||||
return value(detail::parse_cback(f, &cb, err), true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,22 +27,43 @@
|
|||||||
|
|
||||||
namespace rjp{
|
namespace rjp{
|
||||||
|
|
||||||
|
parse_res::parse_res(RJP_value* val, const RJP_parse_error& err):
|
||||||
|
m_value(val), m_err(err){}
|
||||||
|
parse_res::parse_res(parse_res&& p):
|
||||||
|
m_value(std::exchange(p.m_value, nullptr)), m_err(std::move(p.m_err)){}
|
||||||
|
|
||||||
|
parse_res::~parse_res(void){
|
||||||
|
rjp_free_value(m_value);
|
||||||
|
if(m_err.parsestate)
|
||||||
|
rjp_delete_parse_error(&m_err);
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_res::operator bool(void)const{
|
||||||
|
return valid();
|
||||||
|
}
|
||||||
|
bool parse_res::valid(void)const{
|
||||||
|
return m_value != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
value parse_res::get_value(void){
|
||||||
|
return value(std::exchange(m_value, nullptr), true);
|
||||||
|
}
|
||||||
|
string parse_res::errstr(void)const{
|
||||||
|
return string(rexy::steal(rjp_parse_error_to_string(&m_err)));
|
||||||
|
}
|
||||||
|
|
||||||
string to_json(const value& val, int format){
|
string to_json(const value& val, int format){
|
||||||
string s;
|
string s;
|
||||||
s.reset(rjp_to_json(val.raw(), format));
|
s.reset(rjp_to_json(val.raw(), format));
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
value parse_json(const rexy::string_base& str, RJP_parse_flag flags, RJP_parse_error& err){
|
parse_res parse_json(const rexy::string_base& str, RJP_parse_flag flags){
|
||||||
return parse_json(str.get(), flags, err);
|
|
||||||
}
|
|
||||||
value parse_json(const rexy::string_base& str, RJP_parse_flag flags){
|
|
||||||
return parse_json(str.get(), flags);
|
return parse_json(str.get(), flags);
|
||||||
}
|
}
|
||||||
value parse_json(const char* str, RJP_parse_flag flags){
|
parse_res parse_json(const char* str, RJP_parse_flag flags){
|
||||||
return value(rjp_parse(str, flags, NULL), true);
|
RJP_parse_error err = {};
|
||||||
}
|
RJP_value* v = rjp_parse(str, flags, &err);
|
||||||
value parse_json(const char* str, RJP_parse_flag flags, RJP_parse_error& err){
|
return parse_res(v, err);
|
||||||
return value(rjp_parse(str, flags, &err), true);
|
|
||||||
}
|
}
|
||||||
namespace detail{
|
namespace detail{
|
||||||
int irjp_parse_callback(char* dest, int size, void* userdata){
|
int irjp_parse_callback(char* dest, int size, void* userdata){
|
||||||
|
|||||||
@ -24,7 +24,7 @@
|
|||||||
namespace rjp{
|
namespace rjp{
|
||||||
|
|
||||||
string::string(RJP_value* r):
|
string::string(RJP_value* r):
|
||||||
rexy::string_intermediary<detail::allocator>(r ? rjp_get_string(r)->value : nullptr,
|
rexy::string_intermediary<detail::allocator>(rexy::steal(r ? rjp_get_string(r)->value : nullptr),
|
||||||
r ? rjp_get_string(r)->length : 0,
|
r ? rjp_get_string(r)->length : 0,
|
||||||
r ? rjp_get_string(r)->length : 0)
|
r ? rjp_get_string(r)->length : 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -16,22 +16,22 @@ int read_callback(char* c, int size, const char* data, int datalen, int& datapos
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_res(const rjp::value& res){
|
int handle_res(const rjp::parse_res& res){
|
||||||
if(res.valid()){
|
if(res.valid()){
|
||||||
fprintf(stderr, "Accepted\n");
|
fprintf(stderr, "Accepted\n");
|
||||||
}else{
|
}else{
|
||||||
fprintf(stderr, "Rejected\n");
|
fprintf(stderr, "%s\n", res.errstr().get());
|
||||||
}
|
}
|
||||||
return !res.valid();
|
return !res.valid();
|
||||||
}
|
}
|
||||||
int test_cbacks(const char* str, RJP_parse_flag flags){
|
int test_cbacks(const char* str, RJP_parse_flag flags){
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
rjp::value res = rjp::parse_json(flags, read_callback, str, strlen(str), pos);
|
rjp::parse_res res = rjp::parse_json(flags, read_callback, str, strlen(str), pos);
|
||||||
return handle_res(res);
|
return handle_res(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
int test(const char* str, RJP_parse_flag flags){
|
int test(const char* str, RJP_parse_flag flags){
|
||||||
rjp::value res = rjp::parse_json(str, flags);
|
rjp::parse_res res = rjp::parse_json(str, flags);
|
||||||
return handle_res(res);
|
return handle_res(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user