https://github.com/paroga/cbor-js
The Concise Binary Object Representation (CBOR) data format (RFC7049) implemented in pure JavaScript.
https://github.com/paroga/cbor-js
Last synced: 11 days ago
JSON representation
The Concise Binary Object Representation (CBOR) data format (RFC7049) implemented in pure JavaScript.
- Host: GitHub
- URL: https://github.com/paroga/cbor-js
- Owner: paroga
- License: mit
- Created: 2014-01-20T09:15:26.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2019-06-07T23:57:52.000Z (over 6 years ago)
- Last Synced: 2026-01-24T06:17:51.724Z (about 1 month ago)
- Language: JavaScript
- Size: 61.5 KB
- Stars: 335
- Watchers: 5
- Forks: 49
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - cbor-js
README
cbor-js
=======
The Concise Binary Object Representation (CBOR) data format ([RFC 7049](http://tools.ietf.org/html/rfc7049)) implemented in pure JavaScript.
[](https://travis-ci.org/paroga/cbor-js)
[](https://coveralls.io/r/paroga/cbor-js?branch=master)
[](https://david-dm.org/paroga/cbor-js#info=dependencies&view=table)
[](https://david-dm.org/paroga/cbor-js#info=devDependencies&view=table)
[](https://saucelabs.com/u/paroga-cbor-js)
[](https://saucelabs.com/u/paroga-cbor-js)
API
---
The `CBOR`-object provides the following two functions:
CBOR.**decode**(*data*)
> Take the ArrayBuffer object *data* and return it decoded as a JavaScript object.
CBOR.**encode**(*data*)
> Take the JavaScript object *data* and return it encoded as a ArrayBuffer object.
Usage
-----
Include `cbor.js` in your or HTML page:
```html
```
Then you can use it via the `CBOR`-object in your code:
```javascript
var initial = { Hello: "World" };
var encoded = CBOR.encode(initial);
var decoded = CBOR.decode(encoded);
```
After running this example `initial` and `decoded` represent the same value.
### Combination with WebSocket
The API was designed to play well with the `WebSocket` object in the browser:
```javascript
var websocket = new WebSocket(url);
websocket.binaryType = "arraybuffer";
...
websocket.onmessage = function(event) {
var message = CBOR.decode(event.data);
};
...
websocket.send(CBOR.encode(message));
```