# template class utils::Serializer

## Overview

Serialization utility class. Can perform automatic read and write. More…

    #include <Serializer.h>

    template <typename Type, typename SerializableType>
    class Serializer {
    public:
        // construction

        Serializer(const std::string& root_name);

        // methods

        std::vector<Type> FromJsonString(const std::string& json_string) const;
        std::vector<std::shared_ptr<Type>> FromJsonStringAsPtr(const std::string& json_string) const;
        std::string ToJsonString(const std::vector<Type>& objects) const;
        std::vector<Type> FromJsonFile(const std::string& file_path) const;
        std::vector<std::shared_ptr<Type>> FromJsonFileAsPtr(const std::string& file_path) const;
        bool ToJsonFile(const std::vector<Type>& objects, const std::string& file_path) const;
    };

## Detailed Documentation

Serialization utility class. Can perform automatic read and write.

Uses the “cereal” serialization library - https://uscilab.github.io/cereal/index.html. Cereal allows the possibility to save-load in formats:

- Binary

- JSON

- XML So far this library only makes use of the “JSON” option.

### Methods

    std::vector<Type> FromJsonString(const std::string& json_string) const

Read JSON string.

    std::vector<std::shared_ptr<Type>> FromJsonStringAsPtr(const std::string& json_string) const

Read JSON string, return as shared pointers.

    std::string ToJsonString(const std::vector<Type>& objects) const

Serialize to JSON string.

    std::vector<Type> FromJsonFile(const std::string& file_path) const

Read from JSON file by path.

    std::vector<std::shared_ptr<Type>> FromJsonFileAsPtr(const std::string& file_path) const

Read from JSON file by path, return as shared pointers.

    bool ToJsonFile(const std::vector<Type>& objects, const std::string& file_path) const

Serialize to JSON string and save to file.