MRPT  1.9.9
serialization_json_example/test.cpp
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| https://www.mrpt.org/ |
| |
| Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
| See: https://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See: https://www.mrpt.org/License |
+------------------------------------------------------------------------+ */
/** \example serialization_json_example/test.cpp */
#include <iostream> // cout
#include <sstream> // stringstream
//! [example]
#include <iostream> // cout
{
// Define the MRPT objects to be serialized:
// --------------------
// JSON Serialization
// --------------------
// Create a JSON archive:
// Writes the objects to the JSON archive:
arch["pose_pdf"] = pdf1;
arch["pose"] = p1;
// Writes the JSON representation to an std::ostream
std::stringstream ss;
ss << arch;
// also, print to cout for illustration purposes:
std::cout << arch << std::endl;
// --------------------
// JSON Deserialization
// --------------------
// rewind stream for reading from the start
ss.seekg(0);
// Create a new JSON archive for reading
// Load the plain text representation into the archive:
ss >> arch2;
// Parse the JSON data into an MRPT object:
arch2["pose_pdf"].readTo(pdf2);
arch2["pose"].readTo(p2);
std::cout << "read pose:" << p2.asString() << std::endl;
}
//! [example]
int main(int argc, char** argv)
{
try
{
return 0;
}
catch (const std::exception& e)
{
std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
return -1;
}
catch (...)
{
printf("Untyped exception!");
return -1;
}
}
#if 0 // code disabled, only present as an example for the docs:
//! [example_raw]
#include <json/json.h>
void test()
{
Json::Value val;
std::make_unique<CSchemeArchive<Json::Value>>(val));
mrpt::poses::CPose2D pt1{1.0, 2.0, 3.0};
// Store any CSerializable object into the JSON value:
arch = pt1;
// Alternative:
// arch["pose"] = pt1;
std::stringstream ss;
ss << val;
std::cout << val << std::endl;
}
//! [example_raw]
#endif



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020