Coder Social home page Coder Social logo

saur0n / libserialization Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 49 KB

Effective simple C++ serialization library

License: GNU Lesser General Public License v3.0

Makefile 2.97% C++ 97.03%
serialization deserialization serialization-library deserialization-library

libserialization's Introduction

libserialization

This library allows to serialize standard C++ data types to binary form.

Supported data types

Serialization and deserialization of the following data types is supported:

  • bool
  • char
  • int8_t
  • uint8_t
  • int16_t
  • uint16_t
  • int32_t
  • uint32_t
  • int64_t
  • uint64_t
  • long long (not the same as int64_t)
  • unsigned long long (not the same as uint64_t)
  • std::array
  • std::basic_string
  • std::map
  • std::pair
  • std::set
  • std::vector

Serialization of the following data types is supported:

  • fixed-size arrays
  • const char * (C-strings)
  • any classes that have serialize() method (see below)

Deserialization of the following data types is supported:

  • any custom classes which have deserialization constructor (see below)

Serialization and deserialization of float, double and long double are implemented, but data format will be changed in future.

Usage

The following include statement is necessary:

#include <rohan/Serializer.hpp>

Optional using directives:

using rohan::InputStream;
using rohan::OutputStream;

Writing data

Data is written using the | operator:

const unsigned magic=0x4698CD67;
std::vector<std::string> vec {"alpha", "beta", "gamma"};
writer | magic | vec | "string";

To write fields of a class, add constant method serialize():

class Color {
public:
    explicit Color(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b) {}
    void serialize(Writer &writer) const {
        writer | r | g | b;
    }
private:
    uint8_t r, g, b;
};

Reading data

Data is read from the stream using type conversion operator:

unsigned nCows=unsigned(reader);
std::vector<std::string> vec=std::vector<std::string>(reader);

To read fields of a class, create a unserialization constructor:

class Color {
public:
    explicit Color(Reader &reader) :
        r(uint8_t(reader)),
        g(uint8_t(reader)),
        b(uint8_t(reader)) {}
private:
    uint8_t r, g, b;
};

libserialization's People

Stargazers

 avatar  avatar  avatar

Watchers

 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.