Giter Site home page Giter Site logo

ebergstedt / protobuffer Goto Github PK

View Code? Open in Web Editor NEW
52.0 5.0 5.0 35 KB

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

License: MIT License

C# 100.00%
protobuffer gzip serialization async protobuf-net wrapper-library deserialize boilerplate

protobuffer's Introduction

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.

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.