DEV Community

Cover image for Smart_Store API Guide: Exporting and Importing with Confidence
Emmanuel Victor
Emmanuel Victor

Posted on

Smart_Store API Guide: Exporting and Importing with Confidence

In the realm of modern C++ development, data persistence and runtime type safety are often treated as afterthoughts—until they become bottlenecks. With Smart_Store, these challenges are not only addressed but elegantly solved. This guide dives deep into the export/import API of Smart_Store, revealing how it empowers developers to serialize complex object graphs with precision, safety, and speed.

Exporting and Importing: The Smart Way
Smart_Store’s export/import system is designed for seamless data persistence, backup automation, and system integration. Whether you're building a game inventory, a configuration manager, or a dynamic asset tracker, Smart_Store ensures your data flows smoothly across sessions and systems.

Key Highlights

  • Format Flexibility: Supports JSON, XML, CSV, and Binary with embedded type metadata
  • Thread Safety: Internal mutexes guard concurrent access
  • Type-Safe Deserialization: Uses typeid to match runtime types
  • Selective Export: Only properly registered items are serialized
  • Custom Serialization: User-defined types must implement toJson() or equivalent
  • Singleton Flush: Prevents stale data from polluting exports
  • Undo/Redo History: Excluded by default, configurable if needed
  • Robust Importing: Bulk restore or single-object retrieval with runtime type validation
  • Use this link to explore Smart_Store Undo/Redo API: Undo/Redo

Here's an example:

#include "t_manager/ItemManager.h"
#include "err_log/Logger.hpp"
#include <iostream>
#include <string>

using namespace std;

class HumanBeing {
public:
    virtual void speak() const = 0;
    virtual void walk() const = 0;
    virtual void eat() const = 0;
    virtual ~HumanBeing() = default;
};

// Example derived class 
class Boy : public HumanBeing {
public:
    std::string name;
    int age;

public:
    // If you have a constructor that takes parameters, make sure to define 
    // the default constructor as well to avoid issues.
    Boy(std::string name, int age): name(name), age(age) {}
    Boy() {}
    ~Boy() {}

    void speak() const override {
        LOG_CONTEXT(LogLevel::DISPLAY, "Hello, I am a boy named: " 
               + name +  " I am " + to_string(age) + " years old.", {});
    }

    void walk() const override {
        LOG_CONTEXT(LogLevel::DISPLAY, "I am walking like a boy named "
                          + name+ ".", {});
    }

    void eat() const override {
        LOG_CONTEXT(LogLevel::DISPLAY, "I am eating like a boy named "
                           + name + ".", {});
    }
};

// Example of a girl
class Girl : public HumanBeing {
public:
    std::string name;
    int age;

public:
    // If you have a constructor that takes parameters, make sure to define 
    // the default constructor as well to avoid issues.
    Girl(std::string name, int age): name(name), age(age) {}
    Girl() {}
    ~Girl() {}

    void speak() const override {
        LOG_CONTEXT(LogLevel::DISPLAY, "Hello, I am a girl named: "
               + name +  " I am " + to_string(age) + " years old.", {});
    }

    void walk() const override {
        LOG_CONTEXT(LogLevel::DISPLAY, "I am walking like a girl named "
                    + name + ".", {});
    }

    void eat() const override {
        LOG_CONTEXT(LogLevel::DISPLAY, "I am eating like a girl named "
                         + name + ".", {});
    }
};
Enter fullscreen mode Exit fullscreen mode

Exports to files
In this section, we will export items in various file formats, specifically JSON and Binary.

Exporting and Importing with JSON

ItemManager manager;
auto Item1 = std::make_shared<Boy>("Tom", 12);
auto Item2 = std::make_shared<Girl>("Alice", 10);

manager.addItem(Item1, "Boy1");
manager.addItem(Item2, "Girl1");

manager.exportToFile_Json("items.json");
manager.importFromFile_Json("items.json");
manager.importSingleObject_Json("items.json", typeid(Boy).name(), "Boy1");
Enter fullscreen mode Exit fullscreen mode

Expected outputs:

manager.exportToFile_Json("items.json");

manager.importFromFile_Json("items.json");

manager.importSingleObject_Json("items.json", typeid(Boy).name(), "Boy1");

Example: Binary API in Action

ItemManager manager;

auto Item1 = std::make_shared<Boy>("Tom", 12);
auto Item2 = std::make_shared<Girl>("Alice", 10);

manager.addItem(Item1, "Boy1");
manager.addItem(Item2, "Girl1");

manager.exportToFile_Binary("items.bin");
manager.importFromFile_Binary("items.bin");
manager.importSingleObject_Binary("items.bin", typeid(Boy).name(), "Boy1");
Enter fullscreen mode Exit fullscreen mode

Exporting and Importing with Binary
Binary serialization offers compactness and speed, making it ideal for performance-critical applications. Smart_Store handles binary export/import with the same rigor as JSON, embedding type metadata and enforcing registration rules.

Expected outputs:
manager.exportToFile_Binary("items.bin");

manager.importFromFile_Binary("items.bin");

manager.importSingleObject_Binary("items.bin", typeid(Boy).name(), "Boy1");

Why typeid Still Matters
Just like with JSON, Smart_Store uses typeid to ensure runtime type safety during binary deserialization. This is especially critical when dealing with STL containers or polymorphic hierarchies, where relying on mangled names or manual identifiers can lead to subtle bugs.

Best Practices

  • Validate exported files before use
  • Use consistent tag naming for traceability
  • Schedule regular backups
  • Log all operations for auditability
  • Flush singleton managers to avoid stale data
  • Test imports with typeid to ensure type safety

Final Thoughts
Smart_Store’s export/import API isn’t just a utility; it’s a philosophy of safe, scalable, and smart data management. By embedding type metadata, enforcing registration discipline, and supporting flexible formats, it gives developers the confidence to persist and restore complex object graphs without fear.

Ready to take control of your data? Explore the full repo:Smart_Store on GitHub

Top comments (0)