buffer-json-encoding

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

Usage no npm install needed!

<script type="module">
  import bufferJsonEncoding from 'https://cdn.skypack.dev/buffer-json-encoding';
</script>

README

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.