我尝试使用restc-cpp库在C++中发布json数据,但出现了以下错误:
Assertion failed: false, file lib\restc-cpp\include\restc-cpp/SerializeJson.h, line 1273我遵循了此页面上的示例Post data to the server
struct Post
{
int i;
};
// The C++ main function - the place where any adventure starts
int main() {
Post post;
post.i = 22;
// Create an instance of the rest client
auto rest_client = RestClient::Create();
// Create and instantiate a Post from data received from the server.
auto done = rest_client->ProcessWithPromise([&](Context& ctx)
{
// This is a co-routine, running in a worker-thread
auto reply = RequestBuilder(ctx)
.Post("http://ptsv2.com/t/ywexb-1620143951/post")
.Data(post)
// Send the request
.Execute();
cout << "GOT: " << reply->GetBodyAsString() << endl;
});
try
{
// Get the Post instance from the future<>, or any C++ exception thrown
// within the lambda.
done.get();
}
catch(const exception& ex)
{
cout << "Main thread: Caught exception from coroutine: "
<< ex.what() << endl;
}该错误发生在"SerializeJson.h“文件的以下函数中的"post”数据结构序列化期间
template <typename dataT, typename serializerT>
void do_serialize(const dataT& object, serializerT& serializer,
const serialize_properties_t& properties,
typename std::enable_if<
!boost::fusion::traits::is_sequence<dataT>::value
&& !std::is_integral<dataT>::value
&& !std::is_floating_point<dataT>::value
&& !std::is_same<dataT, std::string>::value
&& !is_container<dataT>::value
&& !is_map<dataT>::value
>::type* = 0) {
assert(false);
};行1273在assert上。
有谁有主意吗?
提前感谢
发布于 2021-05-05 17:53:06
我已经找到了错误。我已经阅读了type_traits标准库中关于enable_if的文档。这似乎是一张integral_type,floating_point,...的支票。在……里面
...
typename std::enable_if<
!boost::fusion::traits::is_sequence<dataT>::value
&& !std::is_integral<dataT>::value
&& !std::is_floating_point<dataT>::value
&& !std::is_same<dataT, std::string>::value
&& !is_container<dataT>::value
&& !is_map<dataT>::value
>::type* = 0)
...在我的原始代码中,我为数据函数提供了一个Post结构
Post post;
post.i = 22;
...
auto reply = RequestBuilder(ctx)
.Post("http://ptsv2.com/t/ywexb-1620143951/post")
.Data(post)
// Send the request
.Execute();我直接尝试了一个"int“,它是可以的。然后,我已经读取了restc-cpp doc,并在BOOST_FUSION_ADAPT_STRUCT结构定义之后的结构中添加了带有字符串的STRUCT宏:
struct Post
{
int i;
string name;
};
BOOST_FUSION_ADAPT_STRUCT(
Post,
(int, i)
(string, name)
)并且post请求成功了!
我希望它能帮助其他人!
https://stackoverflow.com/questions/67388587
复制相似问题