Giter Site home page Giter Site logo

skyformat99 / nark-serialization Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rockeet/nark-serialization

0.0 3.0 0.0 515 KB

An easy, fast, small, portable C++ object serialization framework

License: GNU Affero General Public License v3.0

Makefile 2.83% C++ 95.93% Shell 0.79% C 0.45%

nark-serialization's Introduction

terark-serialization

An easy, fast, small, portable C++ object serialization framework

Features

  1. Declarative syntax, Easy to use, Easy to integrate
  2. Fast at runtime, Fast at compile time
  3. Serialized data is small, Compiled code is small
  4. Support STL containers & types
  5. Can serialize to BigEndian/LittleEndian in any platform
  6. Support variant int/uint encoding
  7. Optional serialization version control
  8. More and more...

Bench Mark

I have written a benchmark vs boost-serialization, but have not written benchmarks with other serilization libraries. Fortunately, there is a benchmark of many serialization library, including boost-serialization.

You can run benchmark of terark-serialization vs boost-serialization yourself.

Quick Start

terark-serialization is powerful, but using it is very simple:

#include <terark/io/DataIO.hpp>
#include <terark/io/MemStream.hpp>
#include <terark/io/FileStream.hpp>

struct MyData1 {
    int  a, b, c;
    var_int32_t d;  // d.t is int32 value
    var_uint64_t e; // e.t is uint64 value
    std::string f;
    std::map<std::string, int> g;
    std::set<int> h;
 
	// The simplest use case(without version control):
    DATA_IO_LOAD_SAVE(MyData1, &a&b&c&d&e&f&g&h)
};
 
struct MyData2 {
    int  a, b, c;
    var_int32_t d;
    var_uint64_t e;
    std::string f;
    std::map<std::string, int> g;
    std::set<int> h;
 
    DATA_IO_LOAD_SAVE_V(MyData2, // With version control
        1, // 1 is current version
        &a&b&c&d&e&f&g&h)
};
 
struct MyData3 {
    int  a, b, c;
    boost::int32_t d;
    boost::uint64_t e;
    std::string f;
    std::map<std::string, int> g;
    std::set<int> h;
    std::multiset<int> i;
    unsigned version;
 
    DATA_IO_LOAD_SAVE_V(MyData3, // With version control
        2, // current version
        &a &b &c
        &as_var_int(d) // 'd' defined as  int32_t, serialized as var_int32_t
        &as_var_int(e) // 'e' defined as uint64_t, serialized as var_uint64_t
        &f &g &h
        &vmg.since(2, i) // 'i' is added in version=2
        &vmg.get_version(version) // optional, you can keep version number for latter use
        )
};
 
// in system or third-party's header, can not be changed
struct SysData1 {
    int a;
    unsigned b;
    string c;
};
DATA_IO_LOAD_SAVE_E(SysData1, &a &b &c)

struct SysData2 {
    int a;
    unsigned b;
    string c;
};
DATA_IO_LOAD_SAVE_EV(SysData2, &a &b &vmg.since(2, c))

int main(int argc, char* argv[]) {
    PortableDataOutput<AutoGrownMemIO> output; // 'Portable' is BigEndian
    PortableDataInput<MemIO> input; // 'Portable' is BigEndian

    MyData1 d1;
    // set d1 values
    // ...
    MyData2 d2;
    // set d2 values
    // ...
    MyData3 d3;
    // set d3 values
    // ...
	SysData1 sd1;
	SysData2 sd2;
    output << d1 << d2 << d3 << sd1 << sd2; // saving

    input = output.head(); // shallow copy, input is the written part of output
    input >> d1 >> d2 >> d3 >> sd1 >> sd2; // loading

	output.rewind(); // rewind the write pointer
    output & d1 & d2 & d3 & sd1 & sd2; // saving, '&' is an alias of '<<'

    input = output.head(); // shallow copy, input is the written part of output 
    input & d1 & d2 & d3 & sd1 & sd2; // loading, '&' is an alias of '>>'
}

Is it a magic? I wrote the first version of terark-serialization at July 2006, three months later, I wrote terark-rpc on top of terark-serialization.

Compile

$ cd path/to/terark-base
$ make # compile terark-base
$ cd test-serialization
$ make # compile test and benchmark
$ rls/bench.exe # run benchmark

More

To be done

nark-serialization's People

Contributors

rockeet avatar xecycle avatar

Watchers

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