简体   繁体   English

当我尝试转换Int64时,poco库上的错误转换异常

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

I wrote some code for parse JSON string. 我为解析JSON字符串编写了一些代码。 I got "Bad Cast Exception" sometimes. 我有时会得到“Bad Cast Exception”。 In My JSON string 1. 2. don't raise exception and 3. 4. raise exception. 在My JSON字符串中1. 2.不引发异常和3. 4.引发异常。

A difference between two group is that 1. 2. 's BCodeW is in range long and 3. 4. 's BCodeW is in range Int64. 两组之间的差异是1. 2.的BCodeW在范围长和3. 4.的BCodeW在Int64范围内。

Why the casting raise the exception ? 为什么铸造会引发异常?

I wrote some guard code for Bad Cast Exception but I wanna know the reason of exception. 我为Bad Cast Exception写了一些保护代码,但我想知道异常的原因。

Thanks for reading. 谢谢阅读。


my environment is below. 我的环境如下。

  • g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11) g ++(GCC)4.4.7 20120313(Red Hat 4.4.7-11)
  • poco-1.6.0 (using Poco::JSON) poco-1.6.0(使用Poco :: JSON)
  • CentOS release 6.6 (Final) CentOS 6.6版(最终版)

My JSON string example below. 我的JSON字符串示例如下。

  1. {"y":37.56376,"x":126.97287,"poiY":37.563686111111,"poiX":126.97302222222,"jibunY":37.563805555556,"jibunX":126.97285833333, "BCodeW":1114016700 ,"poi":"...","jibun":"..."} {“y”:37.56376,“x”:126.97287,“poiY”:37.563686111111,“poiX”:126.97302222222,“jibunY”:37.563805555556,“jibunX”:126.97285833333, “BCodeW”:1114016700 ,“poi”:“.. “” jibun “:” ...“}
  2. {"y":37.59771,"x":127.041493,"poiY":37.597605555556,"poiX":127.041725,"jibunY":37.597547222222,"jibunX":127.04176666667, "BCodeW":1129013600 ,"poi":"...","jibun":"..."} {“y”:37.59771,“x”:127.041493,“poiY”:37.597605555556,“poiX”:127.041725,“jibunY”:37.597547222222,“jibunX”:127.04176666667, “BCodeW”:1129013600 ,“poi”:“.. “” jibun “:” ...“}
  3. {"y":36.760035,"x":127.250362,"poiY":36.759905555556,"poiX":127.25036111111,"jibunY":36.760119444444,"jibunX":127.25040833333, "BCodeW":4413125029 ,"poi":"...","jibun":"..."} {“y”:36.760035,“x”:127.250362,“poiY”:36.759905555556,“poiX”:127.25036111111,“jibunY”:36.760119444444,“jibunX”:127.25040833333, “BCodeW”:4413125029 ,“poi”:“.. “” jibun “:” ...“}
  4. {"y":36.129513,"x":128.34381,"poiY":36.128672222222,"poiX":128.34373888889,"jibunY":36.129738888889,"jibunX":128.34425833333, "BCodeW":4719010200 ,"poi":"...","jibun":"..."} {“y”:36.129513,“x”:128.34381,“poiY”:36.128672222222,“poiX”:128.34373888889,“jibunY”:36.129738888889,“jibunX”:128.34425833333, “BCodeW”:4719010200 ,“poi”:“.. “” jibun “:” ...“}

My Code is below. 我的代码如下。

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;
}

Var.h in Poco's source code says. 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. 

Below Code works. 下面的代码工作。

use convert<T>() instead of extract<T>() 使用convert<T>()而不是extract<T>()

Data type is different. 数据类型不同。 "i", "l" “我”,“我”

extract get data which are exactly match type. 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;
}

The problem here is not in the JSON parsing and/or data extraction. 这里的问题不在于JSON解析和/或数据提取。 It is in the comparison line: 它在比较行中:

if (NULL == _object)

that line will result in BadCastException being thrown. 该行将导致抛出BadCastException。

The reason is because the operator== resolves to 原因是operator==解析为

inline bool operator == (const Poco::Int32& other, const Var& da)

and conversion of Poco::JSON::Object::Ptr to Poco::Int32 throws. 转换Poco::JSON::Object::PtrPoco::Int32抛出。

Replace the offending line with 用。替换违规行

if (_object.isNull())

and all will be well. 一切都会好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM