MRPT  2.0.3
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
CSchemeArchive.h
mrpt::serialization::CSchemeArchiveBase
Virtual base class for "schematic archives" (JSON, XML,...)
Definition: CSchemeArchiveBase.h:75
WriteAndReadExample
void WriteAndReadExample()
[example]
Definition: vision_stereo_rectify/test.cpp:20
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
val
int val
Definition: mrpt_jpeglib.h:957
mrpt::poses::CPose2D
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle.
Definition: CPose2D.h:39
mrpt::poses::CPose2D::asString
void asString(std::string &s) const
Returns a human-readable textual representation of the object (eg: "[x y yaw]", yaw in degrees)
Definition: CPose2D.cpp:445
argv
const char * argv[]
Definition: RawlogGrabberApp_unittest.cpp:40
mrpt::DEG2RAD
constexpr double DEG2RAD(const double x)
Degrees to radians
Definition: core/include/mrpt/core/bits_math.h:47
CPosePDFGaussian.h
argc
const int argc
Definition: RawlogGrabberApp_unittest.cpp:41
mrpt::poses::CPosePDFGaussian
Declares a class that represents a Probability Density function (PDF) of a 2D pose .
Definition: CPosePDFGaussian.h:28
mrpt::serialization::archiveJSON
CSchemeArchiveBase archiveJSON()
Returns an archive for reading/writing in JSON format.
Definition: CSchemeArchive.cpp:23
mrpt::exception_to_str
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
mrpt::math::CMatrixDouble33
CMatrixFixed< double, 3, 3 > CMatrixDouble33
Definition: CMatrixFixed.h:367



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Fri May 15 23:51:15 UTC 2020