serdepp icon indicating copy to clipboard operation
serdepp copied to clipboard

In STL containers, as long as the key type in std:: map is not std:: string, it cannot be used. I tested in yaml-cpp and nlohmann-json

Open WangFengtu1996 opened this issue 4 months ago • 2 comments

  • serdecpp can be serialize, can not be deserialize

WangFengtu1996 avatar Dec 19 '25 15:12 WangFengtu1996

  • I try to fix bug!
  • You can test using example modified from example1
#include <serdepp/serde.hpp>
#include <serdepp/adaptor/nlohmann_json.hpp>
#include <serdepp/adaptor/yaml-cpp.hpp>
#include <serdepp/adaptor/fmt.hpp>

#include <map>


struct example {
    DERIVE_SERDE(example,
                 (&Self::map_,     "map")
                )
    // std::map<std::string, float> map_;
    std::map<uint8_t, float> map_;
};


int main() {
    example ex;
    // ex.map_ = {{"a", 1.0}};
    ex.map_ = {{1, 1.0}};

    // nlohmann::json json_from_ex = serde::serialize<nlohmann::json>(ex);
    YAML::Node yaml_from_ex = serde::serialize<YAML::Node>(ex);
    // example ex_from_json = serde::deserialize<example>(json_from_ex);
    example ex_from_yaml = serde::deserialize<example>(yaml_from_ex);
    // fmt::print("json:{}\n",json_from_ex.dump(4));

    // fmt::print("fmt:{}\n",ex_from_json);
    // fmt::print("yaml:{}\n",YAML::Dump(yaml_from_ex));
}

  • I try to fix bug ! I can make it build successfully, but demo is core dumped shen runtime!

WangFengtu1996 avatar Dec 19 '25 15:12 WangFengtu1996

For int keys, since JSON does not have a map type and only supports the object type, which allows only string keys, this is currently not implemented and no key types other than string are provided. (YAML and TOML are JSON-compatible specs, so this policy is maintained.)

If this were to be implemented, fields in classes would have to be disallowed because field names cannot be int. It would likely need to be implemented only for map[K]V, in a way that converts between K and string.

We would also probably need to restrict which types are allowed for K (roughly int, float, bool, etc.).

injae avatar Dec 20 '25 02:12 injae