5

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
  • 87
  • 8
  • 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

7

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
  • 16,090
  • 3
  • 51
  • 65
  • Thanks for your answer! It seems I need to find another way. – Griyn Aug 15 '18 at 11:31
  • 1
    @Griyn Find a JSON library. Boost Property Tree is a property-tree library – sehe Aug 17 '18 at 12:49
  • @sehe Hello. I am encountering the same issue. From your response, it appears that using boost json it is not possible to write an int/double that stores the values as int/double respectively and not force convert it to string? Your comment is more than 4 years old. In this time has boost come up with a json library? Do you have any recommendations for a JSON library? Boost is able to properly read a json file where an int is stored as an int (without the string quotations) -- but while writing an int, it unfortunately outputs the quotations. – Tryer Feb 12 '23 at 05:58
  • 1
    @Tryer Google is your friend! https://www.boost.org/doc/libs/1_81_0/libs/json/doc/html/index.html (since boost 1.75.0). Regarding Boost Property Tree, the answer contains a link to the actual documentation, where you can see that indeed nothing has changed (obviously, because Property Trees are still not JSON. Or XML for that matter). – sehe Feb 12 '23 at 14:33
  • As always, one encounters the answer after posting a question on SO! Yes, for the last few hours I have been trying to update my property tree code to boost json code! – Tryer Feb 12 '23 at 14:36
  • 1
    @Tryer - here's what the OP's code would look like in my universe: http://coliru.stacked-crooked.com/a/1161e8bb18dc525c – sehe Feb 12 '23 at 14:38