4

My input: data.txt

{
        "target_url":"www.19lou.com/forum-1637-thread-10031471311655793-1-1.html",
        "trespassing_field":{
            "ratetype":5,
            "spider":{
                "prod_name":"name",
                "link_src":1 
            }
        }
}

use code:

boost::property_tree::ptree json_tree;
boost::property_tree::read_json("data.txt", json_tree);
std::stringstream json_main_pack;
boost::property_tree::write_json(json_main_pack, json_tree);
LOG(NOTICE) << "json: " << json_main_pack.str();

output:

{
    "target_url": "www.19lou.com\/forum-1637-thread-10031471311655793-1-1.html",
    "trespassing_field": {
        "ratetype": "5",
        "spider": {
            "prod_name": "name",
            "link_src": "1"
        }
    }
}

write_json() convert integer value to string.It convert "ratetype":5 to "ratetype": "5".It is incorrect. How to convert accurately?Input integer value, then output integer value.

Griyn
  • 77
  • 7
  • Possible duplicate of [Why boost property tree write\_json saves everything as string? Is it possible to change that?](https://stackoverflow.com/questions/2855741/why-boost-property-tree-write-json-saves-everything-as-string-is-it-possible-to) – dewaffled Aug 15 '18 at 11:00
  • @ dewaffled Thanks. It is a way to solve the problem. But it is not graceful... – Griyn Aug 15 '18 at 11:32

1 Answers1

5

From the Boost.PropertyTree documentation here, it looks like all type information is lost. Relevant quote:

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.

Emphasis mine. Looks like you might need to use a different JSON parser if you want to retain type information, unfortunately.

Karl Nicoll
  • 15,270
  • 3
  • 47
  • 60