| 1 |
|
|---|
| 2 | #include <boost/property_tree/ptree.hpp>
|
|---|
| 3 | #include <boost/property_tree/json_parser.hpp>
|
|---|
| 4 | #include <boost/version.hpp>
|
|---|
| 5 |
|
|---|
| 6 | #include <string>
|
|---|
| 7 | #include <set>
|
|---|
| 8 | #include <exception>
|
|---|
| 9 | #include <iostream>
|
|---|
| 10 | #include <sstream>
|
|---|
| 11 |
|
|---|
| 12 | static std::string original_problem("{\n \"pipename\": \"quantiser(decode_lut_string=<verbatim>\\u0010\\u0000 \\u0000@\\u0000\200\\u0000\\u0000\\u0001\\u0000\\u0002\\u0000\\u0004\\u0000\\b\377\377\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000<\\/verbatim>)\",\n \"raw\": {\n \"type\": \"t\",\n \"rank\": \"3\",\n \"shape\": {\n \"dim\": \"256\",\n \"dim\": \"128\",\n \"dim\": \"128\"\n }\n },\n \"encoded\": {\n \"bytes\": \"4194304\"\n }\n}\n",3336);
|
|---|
| 13 |
|
|---|
| 14 | static std::string reduced("{\n \"pipename\": \"quantiser(decode_lut_string=<verbatim>\\u0000@\\u0000\200\\u0000<\\/verbatim>)\",\n \"raw\": {\n \"type\": \"t\",\n \"rank\": \"3\",\n \"shape\": {\n \"dim\": \"256\",\n \"dim\": \"128\",\n \"dim\": \"128\"\n }\n },\n \"encoded\": {\n \"bytes\": \"4194304\"\n }\n}\n",296);
|
|---|
| 15 |
|
|---|
| 16 | static std::string problem = reduced;
|
|---|
| 17 |
|
|---|
| 18 | namespace pt = boost::property_tree;
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | struct content {
|
|---|
| 22 |
|
|---|
| 23 | int some_data = 42;
|
|---|
| 24 | std::string some_string = "howdy";
|
|---|
| 25 | std::string some_strange_string = problem;
|
|---|
| 26 |
|
|---|
| 27 | void clear(){
|
|---|
| 28 |
|
|---|
| 29 | some_data = 0;
|
|---|
| 30 | some_string.clear();
|
|---|
| 31 | some_strange_string.clear();
|
|---|
| 32 |
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | bool operator==(const content& _rhs){
|
|---|
| 36 |
|
|---|
| 37 | bool value = true;
|
|---|
| 38 |
|
|---|
| 39 | value = value && this->some_data == _rhs.some_data;
|
|---|
| 40 | value = value && this->some_string == _rhs.some_string;
|
|---|
| 41 | value = value && this->some_string.size() == _rhs.some_string.size();
|
|---|
| 42 | value = value && this->some_strange_string == _rhs.some_strange_string;
|
|---|
| 43 | value = value && this->some_strange_string.size() == _rhs.some_strange_string.size();
|
|---|
| 44 |
|
|---|
| 45 | return value;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | friend std::ostream& operator<<(std::ostream & _cout,
|
|---|
| 49 | const content& _rhs){
|
|---|
| 50 |
|
|---|
| 51 | _cout << "some_data = " << _rhs.some_data << "\n";
|
|---|
| 52 | _cout << "some_string = " << _rhs.some_string << "\n";
|
|---|
| 53 | _cout << "some_strange_string = " << _rhs.some_strange_string << "\n";
|
|---|
| 54 |
|
|---|
| 55 | return _cout;
|
|---|
| 56 | }
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | std::string save(const content& _input)
|
|---|
| 60 | {
|
|---|
| 61 | // Create an empty property tree object.
|
|---|
| 62 | pt::ptree tree;
|
|---|
| 63 |
|
|---|
| 64 | // Put the simple values into the tree. The integer is automatically
|
|---|
| 65 | // converted to a string. Note that the "debug" node is automatically
|
|---|
| 66 | // created if it doesn't exist.
|
|---|
| 67 | tree.put("data", _input.some_data);
|
|---|
| 68 | tree.put("string", _input.some_string);
|
|---|
| 69 | tree.put("strange_string", _input.some_strange_string);
|
|---|
| 70 |
|
|---|
| 71 | // Write property tree to JSON file
|
|---|
| 72 | std::stringstream output;
|
|---|
| 73 | pt::write_json(output, tree);
|
|---|
| 74 |
|
|---|
| 75 | return output.str();
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | content load(const std::string &payload)
|
|---|
| 80 | {
|
|---|
| 81 |
|
|---|
| 82 | pt::ptree tree;
|
|---|
| 83 | content value;
|
|---|
| 84 | value.clear();
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | std::stringstream input(payload);
|
|---|
| 88 | pt::read_json(input, tree);
|
|---|
| 89 |
|
|---|
| 90 | value.some_data = tree.get<int>("data");
|
|---|
| 91 | value.some_string = tree.get<std::string>("string");
|
|---|
| 92 | value.some_strange_string = tree.get<std::string>("strange_string");
|
|---|
| 93 |
|
|---|
| 94 | return value;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | int main()
|
|---|
| 99 | {
|
|---|
| 100 | try
|
|---|
| 101 | {
|
|---|
| 102 | content expected;
|
|---|
| 103 | std::string payload = save(expected);
|
|---|
| 104 |
|
|---|
| 105 | content received = load(payload);
|
|---|
| 106 |
|
|---|
| 107 | if(expected == received)
|
|---|
| 108 | std::cout << "Success (Boost version: " << BOOST_VERSION << ")\n";
|
|---|
| 109 | else{
|
|---|
| 110 | std::cout << "FAILED!! (Boost version: " << BOOST_VERSION << ")\n\n"
|
|---|
| 111 | << "expected:\n" << expected << "\n\n"
|
|---|
| 112 | << "received:\n" << received << "\n\n";
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | }
|
|---|
| 116 | catch (std::exception &e)
|
|---|
| 117 | {
|
|---|
| 118 | std::cout << "Error: " << e.what() << "\n(Boost version: " << BOOST_VERSION << ")\n";
|
|---|
| 119 | }
|
|---|
| 120 | return 0;
|
|---|
| 121 | }
|
|---|