I am using the Boost Property Tree for a Project and came across a problem. I'm using it like this:
using Namespace boost::property_tree;
ptree proot;
int myInt = 5;
proot.put("Number", myInt);
write_json("myjson.json", proot);
If I use it like this, the data type that is safed is a string, not an int. An example of what i mean:
{ "Number": "5" } //what i get
{ "Number": 5 } //what i want
Is there a way to change that?
No you cannot change this behavior, as the string value type is pretty muched baked into
boost::property_tree
. While you could technically use different template type parameters than the default ones, you loose much of the conversion logic that went into that library.As a somewhat wanky alternative, consider the following.
This essentially reverts some predefined loss of type information. You can use this within an modified version of Boost's json output function:
Call it for your data e.g. as
and the output should be