如何在C ++中对称地实现序列化和反序列化模板函数 [英] How to symmetrically implement serialize and deserialize template functions in C++

查看:439
本文介绍了如何在C ++中对称地实现序列化和反序列化模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一系列模板函数来序列化和反序列化对象。我已完成序列化部分,一切正常:

  #ifndef SERIALIZE_H 
#define SERIALIZE_H

#include< string>
#include< vector>
#include< unordered_set>
#include< unordered_map>
#include< memory>

inline std :: string to_json(int value){
return std :: to_string(value);
}


inline std :: string to_json(long value){
return std :: to_string(value);
}


inline std :: string to_json(double value){
return std :: to_string(value);
}


inline std :: string to_json(const std :: string& myStr){
return\+ myStr +\ ;
}


template< typename T>
std :: string to_json(const std :: vector< T>& vec){
std :: string json([);

for(auto& i:vec){
json + = to_json(i);
json + =,;
}

if(!vec.empty())json.pop_back();
json + =];
return json;
}


template< typename T>
std :: string to_json(const std :: unordered_set< T>& mySet){
std :: string json([);

for(const auto& i:mySet){
json + = to_json(i);
json + =,;
}

if(!mySet.empty())json.pop_back();
json + =];
return json;
}


template< typename K,typename V>
std :: string to_json(const std :: unordered_map< K,V>& myMap){
std :: string json({);

for(const auto& i:myMap){
json + = to_json(i.first);
json + =:;
json + = to_json(i.second);
json + =,;
}

if(!myMap.empty())json.pop_back();
json + =};
return json;
}

#endif // SERIALIZE_H

c $ c> serialize.h 可以序列化所有种类的组合,例如 unordered_map< string,vector< int>> 。 >

现在我不知道如何实现反序列化函数递归支持任意组合。



以下是我的 deserialize.h 无效:

  #ifndef DESERIALIZE_H 
#define DESERIALIZE_H

#include< string>
#include< rapidjson / document.h>


template< typename T>
T from_json(const std :: string& json);

模板<>
int from_json(const std :: string& json){
return std :: stoi(json);
}

模板<>
long from_json(const std :: string& json){
return std :: stol(json);
}

模板<>
double from_json(const std :: string& json){
return std :: stod(json);
}

模板<>
std :: string from_json(const std :: string& json){
return json.substr(1,json.size() - 1);
}

//
template< typename T>
std :: vector< T> from_json(const std :: string& json){
rapidjson :: Value jsonValue;
{
const std :: string& input ={\input\:+ json +};
rapidjson ::文档文档;
document.Parse(input.c_str());
jsonValue = document [input];
};
std :: vector< T> vec;
assert(jsonValue.IsArray());

for(rapidjson :: SizeType i = 0; i int element = from_json< T>(std :: string(jsonValue [i ] .GetString()));
vec.push_back(element);
}
return vec;
}

#endif // DESERIALIZE_H

rapidjson 是一个C ++ JSON库, https://github.com/miloyip/ rapidjson



然后,如果我尝试反序列化一个JSON字符串:

  #includedeserialize.h> 

int main(){
auto vec1 = from_json< std :: vector< int>>(std :: string 1,2,3]));
return 0;
}

它会抛出编译错误:

 错误:调用重载的'from_json(std :: string)'是不明确的

似乎没有办法像序列化一样容易实现反序列化函数。



任何想法?

解决方案

您尝试重载相同的函数(from_json )
对于重载,你需要不同的参数类型或参数号。

 而不是
模板<>
double from_json(const std :: string& json){
return std :: stod(json);
}

模板<>
std :: string from_json(const std :: string& json){
return json.substr(1,json.size() - 1);
}

可能试试这个,至少这应该是一般的想法: p>

 模板<> 
void from_json(const std :: string& json,double * out_value){
* out_value = std :: stod(json);
}

模板<>
void from_json(const std :: string& json,std :: string * out_value){
out_value = json.substr(1,json.size() - 1);
}



我确定有错误,但我认为这样可以工作如果你修复它们)


I want to write a serial of template functions to serialize and deserialize objects. I've finished the serialization part and everything works:

#ifndef SERIALIZE_H
#define SERIALIZE_H

#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <memory>

inline std::string to_json(int value) {
    return std::to_string(value);
}


inline std::string to_json(long value) {
    return std::to_string(value);
}


inline std::string to_json(double value) {
    return std::to_string(value);
}


inline std::string to_json(const std::string& myStr) {
    return "\"" + myStr + "\"";
}


template <typename T>
std::string to_json(const std::vector<T>& vec) {
    std::string json("[");

    for(auto &i : vec) {
        json += to_json(i);
        json += ",";
    }

    if (!vec.empty()) json.pop_back();
    json += "]";
    return json;
}


template <typename T>
std::string to_json(const std::unordered_set<T>& mySet) {
    std::string json("[");

    for(const auto& i : mySet) {
        json += to_json(i);
        json += ",";
    }

    if (!mySet.empty()) json.pop_back();
    json += "]";
    return json;
}


template <typename K, typename V>
std::string to_json(const std::unordered_map<K, V>& myMap) {
    std::string json("{");

    for(const auto& i : myMap) {
        json += to_json(i.first);
        json += ":";
        json += to_json(i.second);
        json += ",";
    }

    if (!myMap.empty()) json.pop_back();
    json += "}";
    return json;
}

#endif //SERIALIZE_H

This serialize.h can serialize all kinds of combinations, such as unordered_map<string, vector<int>>.

Now I don't know how to implement deserialization functions recursively to support arbitrary combinations.

The following is my deserialize.h which doesn't work:

#ifndef DESERIALIZE_H
#define DESERIALIZE_H

#include <string>
#include <rapidjson/document.h>


template<typename T>
T from_json(const std::string &json);

template<>
int from_json(const std::string &json) {
    return std::stoi(json);
}

template<>
long from_json(const std::string &json) {
    return std::stol(json);
}

template<>
double from_json(const std::string &json) {
    return std::stod(json);
}

template<>
std::string from_json(const std::string &json) {
    return json.substr(1, json.size()-1);
}

//
template<typename T>
std::vector<T> from_json(const std::string& json) {
    rapidjson::Value jsonValue;
    {
        const std::string &input = "{\"input\":" + json + "}";
        rapidjson::Document document;
        document.Parse(input.c_str());
        jsonValue = document["input"];
    };
    std::vector<T> vec;
    assert(jsonValue.IsArray());

    for (rapidjson::SizeType i = 0; i < jsonValue.Size(); i++) {
        int element = from_json<T>(std::string(jsonValue[i].GetString()));
        vec.push_back(element);
    }
    return vec;
}

#endif //DESERIALIZE_H

rapidjson is a C++ JSON library, https://github.com/miloyip/rapidjson

Then if I try to deserialize a JSON string:

#include "deserialize.h>

int main() {
    auto vec1 = from_json<std::vector<int>>(std::string("[1,2,3]"));
    return 0;
}

It will throw out compilation errors:

error: call of overloaded ‘from_json(std::string)’ is ambiguous

It seems there is no way to implement deserialization function as easily as serialization.

Any ideas?

解决方案

You are trying to overload the same function (from_json(...)) with the same arguments and a different return type each time. This is not legal. For overloading, you need different argument types or argument number.

Instead of
template<>
double from_json(const std::string &json) {
    return std::stod(json);
}

template<>
std::string from_json(const std::string &json) {
    return json.substr(1, json.size()-1);
}

maybe try this, or at least this should be the general idea:

template<>
void from_json(const std::string &json, double *out_value) {
    *out_value = std::stod(json);
}

template<>
void from_json(const std::string &json, std::string *out_value) {
    out_value = json.substr(1, json.size()-1);
}

I am sure there are errors, but I think this way it can work (if you fix them)

这篇关于如何在C ++中对称地实现序列化和反序列化模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆