json - Why boost property tree write_json saves everything as string? Is it possible to change that? -
I am trying to serialize the boost properties tree using write_json, it saves everything as stars, not so That data is wrong, but I must put them all clearly and I want to use them somewhere else. (In Python or other C ++ JSN (non-Boost) libraries)
Here is some sample code and I depend on local:
boost :: property_tree :: Ptree root, arr, elem1, elem2; Elem1.put & lt; Int & gt; ("Key0", 0); Elem1.put & lt; Bool & gt; ("Key1" is true); Elem2.put & lt; Float & gt; ("Key2", 2.2f); Elem2.put & lt; Double & gt; ("Key 3", 3.3); Arr.push_back (std :: make_pair ("", elem1)); Arr.push_back (std :: make_pair (", elem2)); Root.put_child (" path1.path2 ", arr); Std :: stringstream ss; Write_json (ss, root); Std :: string my_string_to_send_somewhare_else = ss. Str ();
and my_string_to_send_somewhere_else
is STH. {"Key2": {"path2": [{"key0": "0", "key1" : "True"}, {"key2": "2.2", "Key3": "3.3"}]}}
Is it anyway to save them as values, such as: "key1": true
or "Key2": 2.2
?
Okay, I've solved it like this, (of course it will not be a suite for everyone, because it's a bit of hack, which needs more work).
I have written my own writing - Jason Function (Just the file has been copied, Json_parser.hpp and json_parser_write.hpp and my project json_parser_write.hpp) and the following lines Has been modified to json_parser_write.hpp:
- Commented line 37 - Exclude quote '' ''
- Changed line 76 - so that it does not add further quotes Could: Stream & lt; & Lt; Ch ('' ') & lt; & Lt; Data & lt; & Lt; C ('' '); ==> Stream & lt; & Lt; Data;
Except the stars, except the correct values, it will be saved properly, so I wrote a custom translator for it:
template & lt; Typename T & gt; Struct my_id_translator {typedef t internal_type; Typedef t external_type; Promotion: Optional & lt; T & gt; Get_value (consort & amp; v) {return v.substr (1, v) .isize () - 2);} boost :: optional & lt; T & gt; Put_value (const T & v) {return '' '+ v +' '}}}
("Key2", "asdf", my_id_translator & lt; std :: string & Gt; ());
elem2.put & lt; Std :: string & gt; / Pre> Full Program:
#include & lt; Iostream & gt; #include & lt; String & gt; # Include & lt; Sstream & gt; # Include & lt; Boost / property_tree / ptree.hpp & gt; # "Property_Tree / json_parser.hpp" // contains header templates & lt; Typename T & gt; Struct my_id_translator {typedef t internal_type; Typedef t external_type; Promotion: Optional & lt; T & gt; Get_value (cum nst t & amp; v) {return v.substr (1, v.size () - 2); Promotion: Optional & lt; T & gt; Put_value (CONST T & V) {Return '' '+ V +' '; }}; Int main (int, char * []) {namespace std; Boost :: property_tree :: ptree; Boost :: property_tree :: using basic_ptree; Try {ptree root, arr, elem2; Original_ups < Std :: string, std :: string & gt; Elem1; Elem1.put & lt; Int & gt; ("Int", 10); Elem1.put & lt; Bool & gt; ("Boole", truth); Elem2.put & lt; Double & gt; ("Double", 2.2); Elem2.put & lt; Std :: string & gt; ("String", "some string", my_id_translator & lt; std :: string & gt; ()); Arr.push_back (std :: make_pair ("", elem1)); Arr.push_back (std :: make_pair (", elem2)); root.put_child (" path1.path2 ", arr); std :: stringstream ss; write_json (ss, root); std :: string my_string_to_send_somewhere_else = ss. Str (); Cout & lt; My_string_to_send_somewhere_else & lt; & lt; endl;} hold (std :: exception & amp; a) {cout result)
{"path1": {"path2": [{"int": 10, "bool": true} {"Double": 2.2, "string": "some string"}]}}
Comments
Post a Comment