Skip to content

An abstract-encoding compatible JSON encoder/decoder that properly encodes/decodes buffers.

Notifications You must be signed in to change notification settings

lachenmayer/buffer-json-encoding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

buffer-json-encoding

abstract-encoding

An abstract-encoding compatible JSON encoder/decoder that properly encodes/decodes buffers.

The reason this module exists is that JSON does not have a built-in data type for binary data, so Node.js by default encodes a buffer as an object of shape { type: "Buffer", data: [...] }. The issue is that Node.js does not decode these objects as Buffer instances, which is not very useful if you actually want to do something with them.

This module depends on buffer-json which provides a replacer & reviver for use with JSON.stringify & JSON.parse respectively. Buffer data is encoded as a base64-encoded string, rather than as an array of numbers. Buffers are decoded as expected with both (base-64 & array) encodings.

API

buffer = encode(obj, [buffer], [offset])
obj = decode(buf, [start], [end])
number = encodingLength(obj)

See abstract-encoding for more details.

About

An abstract-encoding compatible JSON encoder/decoder that properly encodes/decodes buffers.

Resources

Stars

Watchers

Forks

Packages

No packages published