alibaba / hessian2-codec

hessian2-codec it is a complete C++ implementation of hessian2 spec
Apache License 2.0
26 stars 10 forks source link

在做hessian到json的类型转换时,遇到了报错 #31

Closed duxin40 closed 5 months ago

duxin40 commented 5 months ago

本地在做hessian到json的类型转换逻辑,代码如下: case Object::Type::Double: { if (dynamic_cast<Hessian2::DoubleObject>(input) == nullptr) { out = badCastErrorMessageJson(hessianType2String(input->type())); } else { out = (static_cast<Hessian2::DoubleObject*>(input)->toMutableDouble()); } } break;

其中out的类型是include/nlohmann/json.hpp 中的json类型,input是hessian2/object.hpp中的object类型,然后编译时报了如下错: http_dubbo_transcoder/filters/http/source/utility.cc:241:74: error: no match for 'operator=' (operand types are 'Envoy::Extensions::HttpFilters::HttpDubboTranscoder::json' {aka 'nlohmann::json_abi_v3_11_3::basic_json<>'} and 'Hessian2::OptRef' {aka 'std::optional<std::reference_wrapper >'}) 241 | out = static_cast<Hessian2::DoubleObject*>(input)->toMutableDouble(); | ^ In file included from http_dubbo_transcoder/filters/http/source/utility.h:17, from http_dubbo_transcoder/filters/http/source/utility.cc:1: external/com_github_nlohmann_json/include/nlohmann/json.hpp:1231:17: note: candidate: 'nlohmann::json_abi_v3_11_3::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>& nlohmann::json_abi_v3_11_3::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>::operator=(nlohmann::json_abi_v3_11_3::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>) [with ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string; BooleanType = bool; NumberIntegerType = long int; NumberUnsignedType = long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::json_abi_v3_11_3::adl_serializer; BinaryType = std::vector; CustomBaseClass = void]' 1231 | basic_json& operator=(basic_json other) noexcept ( | ^~~~ external/com_github_nlohmann_json/include/nlohmann/json.hpp:1231:38: note: no known conversion for argument 1 from 'Hessian2::OptRef' {aka 'std::optional<std::reference_wrapper >'} to 'nlohmann::json_abi_v3_11_3::basic_json<>' 1231 | basic_json& operator=(basic_json other) noexcept (

想问下是我使用hessian对象的方法有问题嘛?万分感谢帮忙看下

wbpcode commented 5 months ago

toMutableXXX 都会返回一个 optional reference wrapper,你应该通过 value().get() 来获取内部数据的引用,才能赋值给 json 对象

duxin40 commented 5 months ago

@wbpcode 好的,十分感谢指导~