Why does Boost property tree write_json save everything as string? Is it possible to change that?

后端 未结 7 1228
忘掉有多难
忘掉有多难 2020-11-30 20:05

I\'m trying to serialize using boost property tree write_json, it saves everything as strings, it\'s not that data are wrong, but I need to cast them explicitly every time a

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 20:34

    I ended up adding another function to my utils to solve this:

    #include 
    #include 
    #include 
    
    namespace bpt = boost::property_tree;
    typedef bpt::ptree JSON;
    namespace boost { namespace property_tree {
        inline void write_jsonEx(const std::string & path, const JSON & ptree)
        {
            std::ostringstream oss;
            bpt::write_json(oss, ptree);
            std::regex reg("\\\"([0-9]+\\.{0,1}[0-9]*)\\\"");
            std::string result = std::regex_replace(oss.str(), reg, "$1");
    
            std::ofstream file;
            file.open(path);
            file << result;
            file.close();
        }
    } }
    

    Hope that helps.

提交回复
热议问题