Giter Club home page Giter Club logo

serialize's Introduction

Serialize

Header-only library for binary serialization and deserialization of simple data structures, in order to quickly load and/or write of the state of a program.
serialize.h provides the basic tools to easily define the serialization code of custom data structures.
In order to minimize disk access, a memory buffer is used during serialization.
The library features the following built-in serializtion functions:

  • serialize(): write/read any POD (struct with no allocated resources).
  • serialize_string(): write/read a std::string.
  • serialize_vector(): write/read a std::vector of PODs.
  • serialize_vector(): write/read a std::vector of structs with custom serialization function.

Example

In example/test.cpp the library is tested, showing its usage on a toy data structure.
Build it with build.sh.

#include "serialize.h"

struct Object {
    std::string name;
    std::vector<int> vec;
    int i;
    float val;
};

// Define custom serialization function.
void serialize_object(Serializer& srl, Object& var) {
    serialize(srl, var.i);
    serialize(srl, var.val);
    serialize_string(srl, var.name);
    serialize_vector(srl, var.vec);
}

int main() {
    auto object   = Object{"Hello", {1,2,3,4}, 77, 12.0};
    auto filename = "object.bin"s;
    int  capacity = 64;
    
    // Save object into binary a binary file.
    auto writer = make_writer(filename, capacity);
    serialize_object(writer, object);
    close_serializer(writer);

    // Reload object from disk.
    auto object_reloaded = Object{};
    auto reader = make_reader(filename, capacity);
    serialize_object(reader, object_reloaded);
    close_serializer(reader);

    // object == object_reloaded
}

serialize's People

Contributors

giacomonazzaro avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.