Giter Club home page Giter Club logo

mochilo's Introduction

Mochilo

Mochilo is a Ruby library implementing the BananaPack protocol. BananaPack is a superset of MessagePack. It adds three new types to the protocol: Symbol and String in 16 and 32 bit lengths.

The Symbol type is a String of text composed of ASCII characters with a maximum length of (2^8)-1 bytes.

The String16 and String32 types are exactly the same as the Raw16 and Raw32 types from MessagePack except there is an encoding flag stored along with the bytes. This allows the format to differentiate binary from text.

Check out docs/format-spec.md for more detailed information on the differences between BananaPack and MessagePack.

Usage

require 'mochilo'
obj = {key: "value"}
bpack = Mochilo.pack(obj)
#=> "\x81\xD8\x00\x03\x01key\xD8\x00\x05\x00value"

hash = Mochilo.unpack(bpack)
#=> {"key"=>"value"}

Notice how key came back into Ruby as a String instead of a Symbol? This is because the pack method of Mochilo only generates "safe" bpack.

bpack without Symbols is considered "safe" for Ruby because in Ruby, Symbols aren't garbage collected. So if you unpack arbitrary bpack from an untrusted or malicious source, you could end up potentially exhausting the memory of your server.

To generate "unsafe" bpack, use pack_unsafe and unpack_unsafe methods instead:

require 'mochilo'
obj = {key: "value"}
bpack = Mochilo.pack_unsafe(obj)
#=> "\x81\xD4\x03key\xD8\x00\x05\x00value"

hash = Mochilo.unpack_unsafe(bpack)
#=> {:key=>"value"}

If you attempt to unpack "unsafe" bpack without using unpack_unsafe, an exception is raised.

Supported Ruby Types

The following Ruby types are supported. Meaning they will be deserialized into the same Ruby type they were before serialization.

If any other object type is encountered during serialization, an exception is raised. This is to ensure you have explicit control over what is being serialized.

  • Fixnum
  • Bignum
  • Float
  • Symbol
  • String (with encoding)
  • nil
  • true
  • false
  • Array
  • Hash

mochilo's People

Contributors

brianmario avatar vmg avatar

Watchers

 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.