Code Monkey home page Code Monkey logo

junhasl's Introduction

junhasl

A simple C++ serialization library.

Contact : [email protected]

Terminologies

Before explaining anything, let me be clear about the terminologies

  1. The header named Header_basic.h does some typedefs for shortened names. For example, uchar, ushort, uint are for unsigned _. long long int is typedef-ed as bint. And also, unordered_map and unordered_set are defined as umap and uset.
  2. Often used std components are declared byusing.
  3. The official name of this library is 'junhaysl', but in code it's called 'ysl'.
  4. 'Serialization' means both write(to()) and read(from()), but usually write. (The term 'deserialization' is more precise for reading)

Types

You can serialize(both read/write) any type that consists only of:

  1. char, uchar, short, ushort, int, uint, bint, ubint, float, double
  2. string, bytes (vector<uchar>)
  3. vector<>, list<>, array<>, umap<>, uset<>
  4. tuple<>
  5. optional<>

And also you can make any class or struct to be 'serializable' with a very simple mark. For example,

struct SSomething : public ysl_struct_base
{
  int a; float b;
  using ysl_parent = void;
  YSL_PACK_S(SSomething, a, b);
};

Your custom struct SSomething will be perfectly serializable.

For example, you can serialize umap<int, tuple<float, vector<SSomething>, optional<string>>> or so.

Usage

Extremely simple.

ysl::yobject r = ysl::to(tuple{1, vector<int>{3,4}, 3.0f, string("hi")});
// (do whatever you want with r)
tuple<int, vector<int>, float, string> q;
ysl::from(r, q);

The struct yobject contains a member variable bytes x;. It's a byte array. You can save it in a file, send via network, compress, or whatever.

Conversion

If you load(or read) a serialized data, you have some degree of freedom to alter the type and let it automatically adapt. There are some rules:

Primitive types

All primitive types (int, float, ...) are interchangeable. For example,

auto r = ysl::to(tuple{int(1), 2.3f, uchar(4)});
tuple<int, float, uchar> same_one;
tuple<float, double, short> altered_one;
ysl::from(r, same_one); ysl::from(r, altered_one); // both are allowed!

The conversion will be conducted by C++'s default casting.

Lists

All vector<>, list<> and array<> will be regarded as a List. They are all interchangeable. For example,

auto r = ysl::to(vector<int>{1,2,3});
list<int> q;
ysl::from(r, q);

Format

Serialized binary data conists of two parts: header & data.

Header area contains type information of the serialized original data. It will be written in a text format, so you can easily read it or even manually write one. (This is important if you serialize the data for other languages that has no exactly corresponding types as C++)

All type (including HKTs like vector<>) has its own mark on header. (YSL_SerializationConfiguration.h does #define about such marks) For example, vector<> is marked as [, and int is marked as i. Then the serialized header of vector<int> will be [i. tuple<> is marked as (N_.... N means number of its entries and _ means marks of each. For example, tuple<int, vector<int>> will be (2i[i.

Implementation

If you just want to just serialize, you never need to care about the implementation.

One of the most poweful feature of this library is the extremely simple overloading resolution. It is based on TMP(Template Meta Programming).

(TODO)

Requirement & Setup

No external dependencies at all. C++ 17 and standard libraries are enough. If you wanna use this library, just merge all files to your project. Then do #include "(Directory)/YSL/YSL.h" in source code.

Note

As you noticed, junhasl doesn't support bool. That's because of std's specialization of vector<bool>. Perhaps I can handle that but not now. You can use ybool which is just uchar. If you really care the size and want to keep bool in a single bit, then I recommend you to keep such structure in bytes.

There is an implementation for glm stuffs. If you don't need that, you can en/disable by modifying YSL_SerializationConfiguration.h. (disabled by default)

There is also a Python3 implementation, but not included in this repo. Contact me if you want.

There are other useful features but I'll skip for now. (sorry!)

junhasl's People

Contributors

junha1 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

junhasl's Issues

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.