MRC/Tutorials/JSON Parsing: Difference between revisions
Latest revision as of 11:13, 27 March 2020
Saving to File
In the Hospital Challenge you are free to create one executable which executes the "Mapping", "Parking" and "Searching"-phase or you create two executables. In the latter case, the first executable executes the "Mapping", "Parking"-phase, the "Searching"-phase might be a seperate executable. When you have two executables or you have to restart in the "Searching"-phase, you will have to save the map you made, such that it is also available after termination of the executable. For this purpose (and e.g. debugging purposes) you might want to create a text file containing your worldmodel. One could simply write values to a text file ([[1]]), in this tutorial, however, we will apply a bit more structure to such a file using JSON.
JSON
JSON JavaScript Object Notation ([[2]]) is a data-interchange format which is both easily readable and easily interpretable for humans and machines. JSON itself is a format, it does not describe how to put information in a file, only how the information should be formatted.
Example - Writing
To get you started with JSON, start-up company RUVU ([[3]]) created this simple example for you to explain you JSON. Suppose we are interested in storing (an array of) Corners, where Corners are defined by
struct Corner { double x_; double y_; Corner(double x, double y) : x_(x), y_(y) { } };
For this example, we would like to send two Corners in an array, so in our map-building software we have to create an array of corners
// building the data std::vector<Corner> corners; corners.push_back(Corner(3, 4)); corners.push_back(Corner(5, 6));
Using a JSON parser ([[4]]), this information is put into the JSON format
// copy the data into a json object json j; // loop over the corners and push them into the json object as for (auto corner : corners) { json c = { { "x", corner.x_ }, { "y", corner.y_ } }; j.push_back(c); }
and after that the JSON object created j, is printed to corners.json-file, using a output file stream ([[5]]). The array of corners is now stored in the corners.json-file.
// serialize the json to a file std::ofstream output("corners.json"); output << std::setw(4) << j << std::endl;
Example - Reading
After your executable has been restarted, or your second executable has been started, you would like to read the information in the file just created. We then read the file using an input stream ([[6]]) and we move the string from the corners.json-file into j.
// read the json object from a file std::ifstream i("corners.json"); json j; i >> j;
We have to be carefull that this code is robust, note the use of assert.
// copy the data from a json object assert(j.is_array()); for (const auto& c : j) { assert(c.is_object()); assert(c.find("x") != c.end()); // check for key x assert(c.find("y") != c.end()); // check for key y corners.push_back(Corner(c["x"], c["y"])); }
Full Example
The code-package attached below shows a full example for JSON parsing. File:JSONExample.zip For more information on the JSON parser consult the README.md on [7].