Replies: 1 comment
-
A You could use a filter to examine the json events before they are written into the json document: #include <jsoncons/json.hpp>
using namespace jsoncons;
int main()
{
std::string str = R"({
"address-book" :
[
{
"name":"Jane Roe",
"email" : "[email protected]"
},
{
"name":"John",
"email" : "[email protected]"
}
]
})";
class validate_filter : public json_filter
{
std::string member_name_;
public:
validate_filter(json_visitor& visitor)
: json_filter(visitor)
{
}
private:
bool visit_key(const string_view_type& name,
const ser_context& context,
std::error_code& ec) override
{
member_name_ = name;
return this->destination().key(name, context, ec);
}
bool visit_string(const string_view_type& val,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
if (member_name_ == "name")
{
std::size_t end_first = val.find_first_of(" \t");
std::size_t start_last = val.find_first_not_of(" \t", end_first);
if (start_last == string_view_type::npos)
{
std::cout << "Incomplete name '" << val
<< "' at line " << context.line()
<< " and column " << context.column() << std::endl;
}
}
return this->destination().string_value(val, tag, context, ec);
}
};
json_decoder<json> decoder;
validate_filter filter(decoder);
json_string_reader reader(str, filter);
reader.read();
json doc = decoder.get_result();
} Output:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi @danielaparker,
I'm looking into back annotating the logical errors into the source JSON file/stream.
If there a way to retrieve the location for a given jsoncons::json/jsoncons::ojscon object?
What would be the canonical way of doing that?
Can I parse whole file and get the location of the given element or do I need to somehow incrementally parse the file and check of application specific errors during the parsing?
Any help would be greatly appreciated.
Thanks,
Grzegorz
Beta Was this translation helpful? Give feedback.
All reactions