当我试图转换Int64时,在poco库上发生了错误的转换异常
Bad cast exception on poco-library when I tried to cast Int64
我写了一些代码来解析JSON字符串。我有时会遇到"坏类型异常"。在我的JSON字符串1。2.不引发异常和4.引发异常。
两组之间的区别是1。2.的BCodeW在long和3范围内。4.的BCodeW在Int64范围内。
为什么强制转换会引发异常?
我为Bad Cast Exception写了一些保护代码,但是我想知道异常的原因。
感谢阅读。
我的环境在下面。
- g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11) Poco -1.6.0 (using Poco::JSON)CentOS release 6.6 (Final)
我的JSON字符串示例如下。
- {"x","y":37.56376:126.97287,"poiY":37.563686111111,"poiX":126.97302222222,"jibunY":37.563805555556,"jibunX":126.97285833333,"BCodeW":1114016700 ,"山芋":"……","jibun":"…"}
- {"x","y":37.59771:127.041493,"poiY":37.597605555556,"poiX":127.041725,"jibunY":37.597547222222,"jibunX":127.04176666667,"BCodeW":1129013600 ,"山芋":"……","jibun":"…"}
- {"x","y":36.760035:127.250362,"poiY":36.759905555556,"poiX":127.25036111111,"jibunY":36.760119444444,"jibunX":127.25040833333,"BCodeW":4413125029 ,"山芋":"……","jibun":"…"}
- {"x","y":36.129513:128.34381,"poiY":36.128672222222,"poiX":128.34373888889,"jibunY":36.129738888889,"jibunX":128.34425833333,"BCodeW":4719010200 ,"山芋":"……","jibun":"…"}
我的代码在下面
bool CUBIUtils::ParseAddressResult( llong& _BCodeW, char* _szPOI, char* _szJibun, char* _szAPIResult )
{
JSON::Parser parser;
try
{
JSON::Object::Ptr _object = parser.parse(_szAPIResult).extract<JSON::Object::Ptr>();
if ( NULL == _object)
{
formatlog( LOG_ERROR, "JSON parsing failed");
return false;
}
formatlog( LOG_DEBUG, "CUBIUtils::%s(%d) AddrSrc: %s", __func__, __LINE__, _szAPIResult);
_BCodeW = 0;
try
{
_BCodeW = _object->get("BCodeW").extract<Int64>();
}
catch(exception &_e)
{
_BCodeW = _object->get("BCodeW").extract<int>();
}
strcpy( _szPOI, _object->get("poi").extract<std::string>().c_str());
strcpy( _szJibun, _object->get("jibun").extract<std::string>().c_str());
}
catch(exception &e)
{
formatlog( LOG_ERROR, "CUBIUtils::%s(%d) JSON parsing Exception. %s", __func__, __LINE__, e.what());
return false;
}
return true;
}
Poco源代码中的Var.h说。
/// Invoke this method to perform a safe conversion. /// /// Example usage: /// Var any("42"); /// int i = any.convert<int>(); /// /// Throws a RangeException if the value does not fit /// into the result variable. /// Throws a NotImplementedException if conversion is /// not available for the given type. /// Throws InvalidAccessException if Var is empty.
用convert<T>()
代替extract<T>()
数据类型不同。"我"、"l"
extract
得到完全匹配类型的数据
_BCodeW = 0;
if ( _object->isNull("BCodeW"))
cout << "BCodeW is NULL" << endl;
else
{
Dynamic::Var _BCodeWVar = _object->get("BCodeW");
cout << "Data Type is " << _BCodeWVar.type().name() << endl;
_BCodeW = _BCodeWVar.convert<Int64>();
cout << "BCodeW is " << _BCodeW << endl;
}
这里的问题不在于JSON解析和/或数据提取。它位于比较行:
if (NULL == _object)
该行将导致抛出BadCastException。
原因是operator==
解析为
inline bool operator == (const Poco::Int32& other, const Var& da)
和Poco::JSON::Object::Ptr
到Poco::Int32
的转换抛出。
将违规行替换为
if (_object.isNull())
一切都会好起来的
相关文章:
- 此测试()中发生了什么意外过程?为什么总是覆盖 ch[0 1 2..]?
- 这C++代码中发生了什么C++(指数函数)
- 哪种方式更快?究竟发生了什么,我们没有看到什么?
- 我正在将一个 std::string 传递给一个 boost 函数,该函数对该类型进行常量引用,但该值发生了变化
- c++问题:给一个变量赋值后,另一个变量发生了变化
- 从"LLONG_MAX 秒"构造 std::chrono::毫秒变量时发生了什么?
- 从 uint8_t 到 int 的隐式转换出错了,当显式转换进展顺利时
- 这个片段中关于 n 在 pc[i] 中的表示发生了什么
- istringstream,num1 和 num2 在这里发生了什么?
- 究竟发生了什么,我们需要在 c++ 中双重调度/访客
- libstdc++的make_shared布局在gcc 4.x和gcc 6.x之间是否发生了变化?
- 我是否能够确定在部署一个程序后发生了什么,我在数组末尾写入?
- 为什么我的 int 在 C++ 程序中间发生了变化?
- 这个阶乘程序内部发生了什么?
- 为什么在波纹管程序中发生了从字符串常量到'char*'的警告已弃用的转换
- C++11与C++98转换运算符的行为发生了变化
- 提升 MSM,如何确定是否发生了转换
- 字符到整数的转换 - 这里发生了什么?
- istream中发生了什么隐式转换?
- 当我试图转换Int64时,在poco库上发生了错误的转换异常