当我试图转换Int64时,在poco库上发生了错误的转换异常

Bad cast exception on poco-library when I tried to cast Int64

本文关键字:转换 发生了 错误 异常 poco Int64      更新时间:2023-10-16

我写了一些代码来解析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字符串示例如下。

  1. {"x","y":37.56376:126.97287,"poiY":37.563686111111,"poiX":126.97302222222,"jibunY":37.563805555556,"jibunX":126.97285833333,"BCodeW":1114016700 ,"山芋":"……","jibun":"…"}
  2. {"x","y":37.59771:127.041493,"poiY":37.597605555556,"poiX":127.041725,"jibunY":37.597547222222,"jibunX":127.04176666667,"BCodeW":1129013600 ,"山芋":"……","jibun":"…"}
  3. {"x","y":36.760035:127.250362,"poiY":36.759905555556,"poiX":127.25036111111,"jibunY":36.760119444444,"jibunX":127.25040833333,"BCodeW":4413125029 ,"山芋":"……","jibun":"…"}
  4. {"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::PtrPoco::Int32的转换抛出。

将违规行替换为

if (_object.isNull())

一切都会好起来的