ebergstedt / ProtoBuffer

A simple wrapper library for protobuf-net with async, gzip and less boilerplate.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ProtoBuffer

A simple wrapper library for protobuf-net with async, gzip and less boilerplate.

ProtoBuffer will remove some repetitive code declarations, like streams initializations and reading. It supports object to byte array or to file, with serialization and deserialization. It can also employ gzip. Just remember to keep track on which objects are gzipped or not when you deserialize them.

Performance report for ProtoBuf shows that it's one of the fastest serializers for C#. But what ProtoBuf is designed for is unprecedented speed of deserialization, with a very compact format. This makes it a prime candidate for your caching solution, such as using Redis together with ProtoBuf for extremely fast data transfer and deserialization. This is as of 2016 the solution deployed in production at Stack Overflow.

Usage

The test files are self-explanatory. Click below to go to the test files.

Protobuffer.SimpleSerializer helps with serialization.

Protobuffer.SimpleDeserializer helps with deserialization.

Here is a sample:

[TestCase(false)]
[TestCase(true)]
public void Given_an_object_Then_protobuf_serialize_and_deseserialize_with_byte(bool useGzip)
{
    var serialize = _simpleSerializer.ToStringValue(GetObjectWithProtobufContract(), gzipCompress: useGzip);

    Person deserialize = _simpleDeserializer.FromStringValue<Person>(serialize, gzipDecompress: useGzip);

    Assert.NotNull(deserialize); // True !!
}

Methods

Protobuffer.SimpleSerializer

string SaveToFile(
    object item,
    string filePath,
    FileMode fileMode = FileMode.Create,
    bool gzipCompress = false);

Task<string> SaveToFileAsync(
    object item, 
    string filePath,
    FileMode fileMode = FileMode.Create, 
    bool gzipCompress = false);

byte[] ToByteArray(
    object item,
    bool gzipCompress = false);

string ToStringValue(
    object item,
    bool gzipCompress = false);

Protobuffer.SimpleDeserializer

T FromFile<T>(
    string filePath, 
    bool gzipDecompress = false);

Task<T> FromFileAsync<T>(
    string filePath,
    bool gzipDecompress = false);

T FromByteArray<T>(
    byte[] value,
    bool gzipDecompress = false);

T FromStringValue<T>(
    string value,
    bool gzipDecompress = false);

Gzip?

In ProtoBuffer you may chose to employ Gzip. Your data can be Gzipped before it's serialized into the ProtoBuf format.

Gzip's purpose is primarily used to compress strings. That is why it's the preferred serialization format for webservers to serve html/javascript/css which are in a string format. If your data contains a lot of strings, then you may see a considerable size compression when using Gzip. Be aware though that Gzip can increase the size of your data should it not contain enough strings.

Helpful links

Protobuf-net: the unofficial manual

Google ProtoBuf documentation

ProtoBuf vs Json vs XML

Contributors

ebergstedt eridleyj

Contributing

Getting started with Git and GitHub

License

The MIT License (MIT)

Copyright (c) 2016 Erik Bergstedt

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A simple wrapper library for protobuf-net with async, gzip and less boilerplate.

License:MIT License


Languages

Language:C# 100.0%