An open API service indexing awesome lists of open source software.

https://github.com/google/libnop

libnop: C++ Native Object Protocols
https://github.com/google/libnop

cpp-library cpp14 serialization-engine serialization-format serialization-library

Last synced: 8 months ago
JSON representation

libnop: C++ Native Object Protocols

Awesome Lists containing this project

README

          

# libnop: C++ Native Object Protocols

libnop is a header-only library for serializing and deserializing C++ data
types without external code generators or runtime support libraries. The only
mandatory requirement is a compiler that supports the C++14 standard.

Note: This is not an officially supported Google product at this time.

## Goals

libnop has the following goals:

* Make simple serialization tasks easy and complex tasks tractable.
* Remove the need to use code generators and schema files to describe data
types, formats, and protocols: perform these tasks naturally within the C++
language.
* Avoid additional runtime support requirements for serialization.
* Provide contemporary features such as bidirectional binary compatibility,
data validation, type safety, and type fungibility.
* Handle intrinsic types, common STL types and containers, and user-defined
types with a minimum of effort.
* Produce optimized code that is easy to analyze and profile.
* Avoid internal dynamic memory allocation when possible.

## Getting Started

Take a look at [Getting Started](docs/getting-started.md) for an introduction to
the library.

## Quick Examples

Here is a quick series of examples to demonstrate how libnop is used. You can
find more examples in the repository under [examples/](examples/).

### Writing STL Containers to a Stream

```C++
#include
#include
#include
#include
#include

#include
#include

int main(int argc, char** argv) {
using Writer = nop::StreamWriter;
nop::Serializer serializer;

serializer.Write(std::vector{1, 2, 3, 4});
serializer.Write(std::vector{"foo", "bar", "baz"});

using MapType =
std::map>;
serializer.Write(
MapType{{0, {10, "foo"}}, {1, {20, "bar"}}, {2, {30, "baz"}}});

const std::string data = serializer.writer().stream().str();
std::cout << "Wrote " << data.size() << " bytes." << std::endl;
return 0;
}
```

### Simple User-Defined Types

```C++
#include
#include
#include
#include
#include

#include
#include
#include

namespace example {

struct Person {
std::string name;
std::uint32_t age_years;
std::uint8_t height_inches;
std::uint16_t weight_pounds;
NOP_STRUCTURE(Person, name, age_years, height_inches, weight_pounds);
};

} // namespace example

int main(int argc, char** argv) {
using Writer = nop::StreamWriter;
nop::Serializer serializer;

serializer.Write(example::Person{"John Doe", 37, 72, 180});
serializer.Write(std::vector{
{"John Doe", 37, 72, 180}, {"Jane Doe", 36, 69, 130}});

const std::string data = serializer.writer().stream().str();
std::cout << "Wrote " << data.size() << " bytes." << std::endl;
return 0;
}
```

### More Complex User-Defined Types

```C++
#include
#include
#include
#include

#include
#include
#include

namespace example {

// Contrived template type with private members.
template
struct UserDefined {
public:
UserDefined() = default;
UserDefined(std::string label, std::vector vector)
: label_{std::move(label)}, vector_{std::move(vector)} {}

const std::string label() const { return label_; }
const std::vector& vector() const { return vector_; }

private:
std::string label_;
std::vector vector_;

NOP_STRUCTURE(UserDefined, label_, vector_);
};

} // namespace example

int main(int argc, char** argv) {
using Writer = nop::StreamWriter;
nop::Serializer serializer;

serializer.Write(example::UserDefined{"ABC", {1, 2, 3, 4, 5}});

using ArrayType = std::array, 2>;
serializer.Write(
ArrayType{{{"ABC", {1, 2, 3, 4, 5}}, {"XYZ", {3.14, 2.72, 23.14}}}});

const std::string data = serializer.writer().stream().str();
std::cout << "Wrote " << data.size() << " bytes." << std::endl;
return 0;
}
```