Skip to content

The code demonstrates how to serialize and deserialize a custom data structure (Person) using templates and standard library features.

License

Notifications You must be signed in to change notification settings

maximilianfeldthusen/Serialize-Custom-Data-Structure

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Documentation

Serialize-Custom-Data-Structure

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:

Includes

#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 using std::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).

Serialization and Deserialization Functions

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.
  • deserialize function: Converts a string back to its original type, returning an std::optional<T> to indicate success or failure.

    • Uses std::stoi for integers and std::stod for doubles, catching exceptions to return std::nullopt if conversion fails.
    • Directly returns the string for std::string.
    • Throws a static assertion for unsupported types.

Person Struct

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), and height (double).
  • serialize Method: Serializes the Person object by calling the serialize 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 a Person object. It checks for successful deserialization using the deserialize function.

Main 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.

Summary

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.

About

The code demonstrates how to serialize and deserialize a custom data structure (Person) using templates and standard library features.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages