0

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

#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

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);

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是一个 C++ JSON 库,https://github.com/miloyip/rapidjson

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

#include "deserialize.h>

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

它会抛出编译错误:

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

似乎没有办法像序列化那样简单地实现反序列化功能。

有任何想法吗?

4

1 回答 1

2

您每次都尝试使用相同的参数和不同的返回类型重载相同的函数 (from_json(...))。这是不合法的。对于重载,您需要不同的参数类型或参数编号。

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);
}

也许试试这个,或者至少这应该是一般的想法:

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);
}

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

于 2015-09-20T22:17:20.420 回答