1.0.0 • Published 4 years ago

@bemoje/serializer v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

@bemoje/serializer

Buffer, string + circular references in a single serialization module that basically just merges three different solutions: v8's Buffer serializer, the native JSON stringifier and the drop-in replacer for JSON.stringify: 'safe-stable-stringify', which supplies the support for circular references.

install

npm install --save @bemoje/serializer

API

export default { toBufferV8, toJson, toJsonPretty, deserialize }

/**
 * Serializes with v8 engine.
 * If that fails for any reason, JSON.stringify will be used, after which that resulting stringwill be
 * converted to a Buffer as to not change types.
 * @param {*} data - What to serialize
 * @returns {Buffer}
 */
export function toBufferV8(data: any): Buffer

/**
 * Serializes with JSON.stringify.
 * If that fails for any reason, 'safe-stable-stringify', as it supports circular references. If that
 * fails, the v8 engine is tried. It's returned Buffer will be converted to a string before it is
 * returned as to not change types, suddenly.
 * @param {*} data - What to serialize
 * @param {function} [replacer] - See the JSON.stringify() documentation
 * @param {integer} [space=0] - See the JSON.stringify() documentation
 * @returns {string}
 */
export function toJson(data: any, replacer?: Function, space?: Number): String

/**
 * Serializes with safe-stable-stringify, since that supports circular references. That circular
 * references error from
 * JSON is not pretty :)
 * @param {*} data - What to serialize
 * @param {function} [replacer] - See the JSON.stringify() documentation
 * @param {integer} [space=3] - See the JSON.stringify() documentation
 * @returns {string}
 */
export function toJsonPretty(data: any, replacer?: Function, space?: Number = 3): String

/**
 * Deserializes with v8 engine if the input is a Buffer and with JSON.parse if it's a string.
 * @param {Buffer|string} serialized - What to deserialize
 * @returns {*}
 */
export function deserialize(serialized: Buffer|String, reviver?: Function): any

usage

const data = { a: 3, b: { a: 32 } }

const _toBufferV8 = toBufferV8(data)
const _toJson = toJson(data)
const _toJsonPretty = toJsonPretty(data)

const __toBufferV8 = deserialize(_toBufferV8)
const __toJson = deserialize(_toJson)
const __toJsonPretty = deserialize(_toJsonPretty)

console.log({
	data,
	_toBufferV8,
	_toJson,
	_toJsonPretty,
	__toBufferV8,
	__toJson,
	__toJsonPretty,
})

/*{
  data: { a: 3, b: { a: 32 } },
  _toBufferV8: <Buffer ff 0d 6f 22 01 61 49 06 22 01 62 6f 22 01 61 49 40 7b 01 7b 02>,
  _toJson: '{"a":3,"b":{"a":32}}',
  _toJsonPretty: '{\n   "a": 3,\n   "b": {\n      "a": 32\n   }\n}',
  __toBufferV8: { a: 3, b: { a: 32 } },
  __toJson: { a: 3, b: { a: 32 } },
  __toJsonPretty: { a: 3, b: { a: 32 } }
}*/

const circular = { a: 3, b: { a: 32 } }
circular.b.b = circular.b

const c_toBufferV8 = toBufferV8(circular)
const c_toJson = toJson(circular)
const c_toJsonPretty = toJsonPretty(circular)

const c__toBufferV8 = deserialize(c_toBufferV8)
const c__toJson = deserialize(c_toJson)
const c__toJsonPretty = deserialize(c_toJsonPretty)

console.log({
	circular,
	c_toBufferV8,
	c_toJson,
	c_toJsonPretty,
	c__toBufferV8,
	c__toJson,
	c__toJsonPretty,
})

/*{
  circular: { a: 3, b: { a: 32, b: [Circular] } },
  c_toBufferV8: <Buffer ff 0d 6f 22 01 61 49 06 22 01 62 6f 22 01 61 49 40 22 01 62 5e 01 7b 02 7b 02>,
  c_toJson: '{"a":3,"b":{"a":32,"b":"[Circular]"}}',
  c_toJsonPretty: '{\n   "a": 3,\n   "b": {\n      "a": 32,\n      "b": "[Circular]"\n   }\n}',
  c__toBufferV8: { a: 3, b: { a: 32, b: [Circular] } },
  c__toJson: { a: 3, b: { a: 32, b: '[Circular]' } },
  c__toJsonPretty: { a: 3, b: { a: 32, b: '[Circular]' } }
}*/