Giter Club home page Giter Club logo

serializers's Introduction

E-Agle TRT

Serializers

Serializers is a code generation library for C++ to be used with Google's Protocol Buffers.

Given the .proto files, it generates a header file and a source file for each proto, besides creating a CMakeLists.txt file to integrate the code with your main project and a header "serializers.h".

The library wraps the pb files generated by protoc, providing a simple and convenient interface to handle, serialize and deserialize the defined proto messages. It also provides functions to convert and parse the messages to JSON format.

The generated header consist in a struct for each proto Message and the following methods for serialization:

std::string serializeAsJsonString() const;
std::string serializeAsProtobufString() const;
bool deserializeFromJsonString(const std::string& str);
bool deserializeFromProtobufString(const std::string& str);

The methods implementation is built upon the pb files, which have to be added to the main project and included in the global include path.

Example

bar.proto (input)

syntax = "proto3";

package bar;

enum MyEnum
{
    MY_ENUM_0 = 0;
    My_ENUM_1 = 1;
}

message MySmallMessage
{
    int64 val = 1;
}

message MyMessage
{
    double val = 1;
    string str = 2;
    MySmallMessage mySmallMessage = 3;
    repeated MyEnum enumVec = 4;
    map<uint32, MySmallMessage> structMap = 5;
}

bar.h (output)

#ifndef BAR_H
#define BAR_H

#include "bar.pb.h"

[...]

namespace Serializers
{
enum class MyEnum
{
    MY_ENUM_0 = 0,
    My_ENUM_1 = 1
};

struct MySmallMessage
{
    int64_t val;

    [...]
};

struct MyMessage
{
    double val;
    std::string str;
    MySmallMessage mySmallMessage;
    std::vector<MyEnum> enumVec;
    std::unordered_map<uint32_t, MySmallMessage> structMap;

    MyMessage() = default;
    MyMessage(const bar::MyMessage& proto);
    operator bar::MyMessage() const;

    std::string serializeAsJsonString() const;
    std::string serializeAsProtobufString() const;
    bool deserializeFromJsonString(const std::string& str);
    bool deserializeFromProtobufString(const std::string& str);
};
}

#endif

Usage

  • Generate the code with: python3 src/generator/main.py proto_dir output_dir.
  • Copy the output directory in your project and generate the pb files with protoc, using the files in output_dir/proto.
  • Add add_subdirectory(path/to/output/directory) to your main CMakeLists.txt.
  • Include "serializers.h" in your files and use the library code.
#include "serializers.h"

#include <iostream>
#include <string>

int main()
{
    Serializers::MySmallMessage mySmallMessage;
    mySmallMessage.val = 3.14;

    Serializers::MyMessage myMessage;
    myMessage.str = "Federico Carbone";
    myMessage.enumVec.push_back(Serializers::MyEnum::MY_ENUM_0);
    myMessage.structMap.emplace(404, mySmallMessage);

    std::string myMessageSerialized = myMessage.serializeAsProtobufString();

    std::cout << myMessage.serializeAsJsonString() << std::endl;

    return 0;
}

Features

Implemented / Not implemented yet

  • Multiple file packages
  • All primitive fieldtypes support
  • Enum
  • "repeated" option
  • Maps
  • Package check
  • Import external proto definitions
  • Nested types
  • OneOf
  • Custom options

serializers's People

Contributors

guglielmo-boi avatar andreolli-davide avatar pippo98 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.