Home > Net >  How can I write filed value to a json file with the boost?
How can I write filed value to a json file with the boost?

Time:07-24

I have some json file:

{
    ....
    ....
    "frequency_ask": 900
}

and need to change field frequency_ask to 1200 for ex.

I called my function

void setFieldToJson(std::string json, std::string field, int value)
{
    boost::property_tree::ptree pt;
    pt.put(field, value);
    std::ostringstream json;

    boost::property_tree::write_json(json, pt);
}

with next:

setFieldToJson("../config_files/device.json", "frequency_ask", 1200);

but doesn't work. How can I make it correct?

CodePudding user response:

Your problem is most likely that you're using Property Tree, which is NOT a JSON library.

Alternatively, it could be that your input is not valid JSON.

Here's my take using Boost JSON:

enter image description here

CodePudding user response:

As I said in general-comments, it isn't clear whatsoever what your intent is with the two json variables in your code. The latter (the string stream) shadows the former (the string parameter).

The write_json function has two overloads:

  • The first takes an opened stream object that the write operation is performed upon.
  • The second takes a string argument denoting a file name, and will attempt to open and write to that file directly.

Therefore, if you want to write json into a file, just forward the file name to the write_json call and be done with it.

#include <iostream>
#include <string>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

namespace bpt = boost::property_tree;

template<class T>
void writeJsonFileFromPropertyTree(std::string const& fname, std::string const& field, T&& value)
{
    bpt::ptree pt;
    pt.put(field, std::forward<T>(value));
    bpt::write_json(fname, pt);
}

int main()
{
    writeJsonFileFromPropertyTree("output.json", "FieldName", 42);
}

Output (output.json)

{
    "FieldName": "42"
}

Otherwise, if you want to create json and capture the results to a string, use a string stream directly and reap the results thereafter:

#include <iostream>
#include <sstream>
#include <string>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

namespace bpt = boost::property_tree;

template<class T>
std::string writeJsonStringFromPropertyTree(std::string const& field, T&& value)
{
    bpt::ptree pt;
    pt.put(field, std::forward<T>(value));

    std::ostringstream outp;    
    bpt::write_json(outp, pt);
    return outp.str();
}

int main()
{
    std::string res = writeJsonStringFromPropertyTree("FieldName", 42);
    std::cout << res << '\n';
}

Output (console)

{
    "FieldName": "42"
}

Either way could/should work for what you're trying to do. Note that each of the above is pretty bland in that you're getting some very basic, limited json. For multi-field json objects you need to persist the actual property tree object, make all your mods to it, then do similar to the above to achieve the final storage to file or string.

  • Related