4

When I put_value using an int, it gets written as a string. Does anyone know how to get it to print as an int?

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

using boost::property_tree::ptree;
using namespace std;

int main(int argc, char* argv[]) {

    ptree node;
    node.put("string", "text here");
    node.put("int", 1);//outputs as "1" and should be 1
    write_json(cout, node, false);//{"string":"text here","int":"1"}

    return 0;
}
6
  • Only int main() and int main(int argc, char * argv[]) are standard-conforming.
    – Neil Kirk
    Mar 24, 2015 at 22:06
  • 2
    @NeilKirk why did you comment this?
    – sehe
    Mar 24, 2015 at 23:53
  • @sehe Because his main isn't standard-conforming?
    – Neil Kirk
    Mar 25, 2015 at 4:57
  • 1
    @NeilKirk It's clearly irrelevant. If you fear it distracts, consider editing. Right now you're just ignoring and detracting from a well-posed, fit question.
    – sehe
    Mar 25, 2015 at 8:07
  • 1
    @NeilKirk That's what I used the edit comment for. Anyhoops. It's noise.
    – sehe
    Mar 25, 2015 at 10:15

1 Answer 1

7

The library expressly doesn't support it.

Boost Property Library has not been named "Boost Json Library" because it's not a JSON library. Instead, it's a Property Tree library (that happens to use JSON subsets for its purposes).

From the documentation:

The property tree dataset is not typed, and does not support arrays as such. Thus, the following JSON / property tree mapping is used:

  • JSON objects are mapped to nodes. Each property is a child node.
  • JSON arrays are mapped to nodes. Each element is a child node with an empty name. If a node has both named and unnamed child nodes, it cannot be mapped to a JSON representation.
  • JSON values are mapped to nodes containing the value. However, all type information is lost; numbers, as well as the literals "null", "true" and "false" are simply mapped to their string form.
  • Property tree nodes containing both child nodes and data cannot be mapped.

And

JSON round-trips, except for the type information loss.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.