【发布时间】:2012-06-06 20:06:08
【问题描述】:
我不确定这是否会是 jsoncpp 的特定事物,还是如何使 C++ 库表现更好的一般范例。基本上我得到了这个跟踪:
imagegeneratormanager.tsk: src/lib_json/json_value.cpp:1176: const Json::Value& Json::Value::operator[](const char*) const: Assertion `type_ == nullValue || type_ == objectValue' failed.
当输入错误时会发生这种情况。当输入(通过 memcached 来自我的另一个应用程序)发生错误时,我想处理此错误。你知道,优雅地。可能类似于“错误:项目 15006 的输入错误”进入日志。不会使我的整个 JSON 字符串处理任务崩溃。
这只是一个写得不好的库还是可以更巧妙地配置它?
编辑:这是一些调用代码:
Json::Value root;
Json::Reader reader;
succeeded = reader.parse(jsonString, root);
if(!succeeded) {
throw std::runtime_error(std::string("Failed to parse JSON for key ") + emailInfoKey.str());
}
std::string userEmail = root.get("userId", "").asString();
std::string bodyFilePath = root.get("bodyFilePath", "").asString();
std::string msgId = root.get("msgId", "").asString();
【问题讨论】:
-
你是说不能检查元素的类型?
-
也许吧?我不知道这个错误是来自我的解析行还是我的访问行,因为它不像断言给你一个调用堆栈或自定义错误消息或任何东西。
-
你在一个值上调用
operator[],比如一个int或一个字符串。 -
添加了我的客户代码。在访问之前我应该做一个检查吗?我认为这就是我的默认值
""的用途。
标签: c++ error-handling assert fault-tolerance jsoncpp