This C++ code demonstrates how to serialize and deserialize a custom data structure (Person
) using templates and standard library features. Let's break down the code step by step:
#include <iostream>
#include <tuple>
#include <string>
#include <type_traits>
#include <vector>
#include <sstream>
#include <optional>
#include <iomanip>
<iostream>
: For input and output streams.<tuple>
: Included but not used; could be used for handling tuples if necessary.<string>
: For usingstd::string
.<type_traits>
: To check types at compile time.<vector>
: Included but not used; could be for handling collections if necessary.<sstream>
: For string stream operations (reading/writing strings as streams).<optional>
: To represent optional values that may or may not be present.<iomanip>
: To manipulate the output format (e.g., setting precision).
template<typename T>
std::string serialize(const T& value) {
...
}
template<typename T>
std::optional<T> deserialize(const std::string& str) {
...
}
-
serialize
function: Converts various types (int
,double
,std::string
) to their string representation.- Uses
std::to_string
for integers. - Uses
std::ostringstream
for doubles, formatting them to two decimal places. - Returns the string directly for
std::string
. - Throws a static assertion for unsupported types.
- Uses
-
deserialize
function: Converts a string back to its original type, returning anstd::optional<T>
to indicate success or failure.- Uses
std::stoi
for integers andstd::stod
for doubles, catching exceptions to returnstd::nullopt
if conversion fails. - Directly returns the string for
std::string
. - Throws a static assertion for unsupported types.
- Uses
struct Person {
std::string name;
int age;
double height;
std::string serialize() const {
return serialize(name) + "," + serialize(age) + "," + serialize(height);
}
static std::optional<Person> deserialize(const std::string& str) {
...
}
};
- Data Members: Contains three fields:
name
(string),age
(int), andheight
(double). serialize
Method: Serializes thePerson
object by calling theserialize
function on each member and concatenating them with commas.deserialize
Method: A static method that takes a comma-separated string, splits it into tokens, and assigns the values to aPerson
object. It checks for successful deserialization using thedeserialize
function.
int main() {
Person person{"Alice", 30, 5.5};
std::string serialized = person.serialize();
std::cout << "Serialized: " << serialized << std::endl;
auto deserialized = Person::deserialize(serialized);
if (deserialized) {
std::cout << "Deserialized: Name: " << deserialized->name
<< ", Age: " << deserialized->age
<< ", Height: " << deserialized->height << std::endl;
} else {
std::cout << "Deserialization failed!" << std::endl;
}
return 0;
}
- Creates a
Person
object with the name "Alice", age 30, and height 5.5. - Serializes the
Person
object to a string and prints it. - Deserializes the string back into a
Person
object and checks if the operation was successful. If successful, it prints the deserialized object's properties; otherwise, it indicates that deserialization failed.
This code is a self-contained example of how to serialize and deserialize a custom data structure in C++. It shows the use of templates for type-safe serialization and deserialization, handling common types, and managing optional return values to deal with potential conversion errors.