alibaba / yalantinglibs

A collection of modern C++ libraries, include coro_rpc, struct_pack, struct_json, struct_xml, struct_pb, easylog, async_simple
https://alibaba.github.io/yalantinglibs/
Apache License 2.0
1.55k stars 240 forks source link

[struct_json] struct_json::from_json一个包含struct_pack::compatible的对象,编译报错 :“from_json_impl”: 未找到匹配的重载函数 #779

Closed xiaocai123123 closed 2 months ago

xiaocai123123 commented 2 months ago

代码如下

#include <cassert>
#include <iguana/json_reader.hpp>
#include <iguana/json_writer.hpp>
#include <iostream>

#include "ylt/struct_json/json_reader.h"
#include "ylt/struct_json/json_writer.h"
#include <ylt/struct_pack.hpp>

void test_user_defined_struct();

struct person {
    std::string name;
    int age;
    int c;
    struct_pack::compatible<int, 114514> c1;
};

bool operator==(const person& a, const person& b) {
    return a.name == b.name && a.age == b.age;
}

YLT_REFL(person, name, age, c, c1);

int main() {
    person p{ "tom", 20 , 11, 12 };
    std::string str;

    // struct to json
    struct_json::to_json<true>(p, str);
    std::cout << str << "\n";

    // struct from json
    person p1;
    struct_json::from_json(p1, str);
    assert(p1.name == p.name);
    return 0;
}

编译的时候产生报警“from_json_impl”: 未找到匹配的重载函数。我想保证这个结构体序列化前后版本兼容,在结构体person中添加了变量struct_pack::compatible<int, 114514> c1。struct_json::to_json可以输出结构体对应的json字符串,但是struct_json::from_json报错。应该是from_json_impl没有找到struct_pack::compatible这个类型的重载,可以支持一下吗?谢谢!

qicosmos commented 2 months ago

目前xml json yaml 还没兼容struct_pack::compatible类型哈,struct_pack::compatible 是struct_pack 专有的类型,把它序列化到json里面会有点奇怪,后面看看怎么处理这个类型吧。