cbor-sync
CBOR encode/decode (synchronous, semantic, browser-compatible)
Last updated 7 years ago by thegecko .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install cbor-sync 
SYNC missed versions from official npm registry.

Encode/decode CBOR

Build Status NPM version

This package provides an extensible CBOR encoder/decoder.

Usage

var CBOR = require('cbor-sync');

var encodedBuffer = CBOR.encode({hello: 'world'});
var decodedObject = CBOR.decode(encodedBuffer);

toCBOR()

Much like the toJSON() method, which allows objects to provide a replacement representation for encoding, this package checks for a toCBOR() method.

Note that this step happens after any semantic-tagging/-replacement step, so a custom semantic encoder will always override an objects built-in toCBOR() method.

Semantic extensions

CBOR provides a limited set of basic types (similar to JSON), but provides semantic tagging (optional for both encoder/decoder) that lets you annotate parts of the data so they can be decoded appropriately.

Here is an example (from this module) for encoding Date objects as ISO strings:

// 0 is the CBOR semantic tag number for date/time strings: https://tools.ietf.org/html/rfc7049#section-2.4
CBOR.addSemanticEncode(0, function (data) {
	if (data instanceof Date) {
		return data.toISOString();
	}
});
CBOR.addSemanticDecode(0, function (dateString) {
	return new Date(dateString);
});

Known issues

  • All floats encoded as 64-bit, regardless of whether they strictly need to be

Current Tags

  • 1.0.4                                ...           latest (7 years ago)

6 Versions

  • 1.0.4                                ...           7 years ago
  • 1.0.3                                ...           8 years ago
  • 1.0.2                                ...           12 years ago
  • 0.0.4                                ...           12 years ago
  • 0.0.1                                ...           12 years ago
  • 0.0.0                                ...           12 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (4)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |