Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/achille-roussel/mpack-js
Javascript implementation of MessagePack encoder and decoder.
https://github.com/achille-roussel/mpack-js
Last synced: 15 days ago
JSON representation
Javascript implementation of MessagePack encoder and decoder.
- Host: GitHub
- URL: https://github.com/achille-roussel/mpack-js
- Owner: achille-roussel
- License: mit
- Created: 2014-12-07T08:38:28.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-05-20T22:33:27.000Z (over 9 years ago)
- Last Synced: 2024-08-09T20:51:30.616Z (3 months ago)
- Language: JavaScript
- Size: 339 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mpack-js
========Browser-oriented javascript implementation of MessagePack encoder and decoder.
Usage
-----Here's a simple example of how to use mpack-js:
```js
var mpack = require('mpack-js')var bytes = mpack.encode({
"hello" : "world",
"answer": 42,
})var object = mpack.decode(bytes)
console.log(object.answer) // 42
```You can also use the Encoder and Decoder objects to build MessagePack messages
from a sequence of multiple objects:
```js
var mpack = require('mpack-js')var encoder = new mpack.Encoder()
encoder.encode('hello')
encoder.encode(42)
encoder.encode([1, 2, 3])var decoder = new mpack.Decoder(encoder.flush())
var object = nullobject = decoder.decode() // hello
object = decoder.decode() // 42
object = decoder.decode() // [1, 2, 3]
object = decoder.decode() // undefined
```MessagePack Extensions
----------------------MessagePack supports encoding *extended* types to embed arbitrary data into a
serialized message.
Here's a quick example showing how to use extended types with mpack-js:
```js
var mpack = require('mpack-js')var data = new Uint8Array(...) // some pre-serialized data
var type = 42 // must be an integer in the range [-128; 127]// Encode the given binary data as an extended data type using MessagePack
// extension support.
var bytes = mpack.encode(new mpack.Extended(type, data))// Decode works like any other data type, the returned object has two fields
// named data and type.
var object = mpack.decode(bytes)console.log(object.type) // 42
console.log(object.data) // Uint8Array
```