2.0.0 • Published 2 years ago

@momsfriendlydevco/marshal v2.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
2 years ago

@momsfriendlydevco/marshal

A simple marshaling serializer for modern JavaScript primitives.

Features:

  • Will attempt to serialize all known JS primitive types (dates, NaN, sets, undefined etc.)
  • Can serialize / deserialize circular structures
  • Simple serialize / deserialize usage
  • Extremely fast - uses its own traversal system rather than a 3rd party NPM library
  • Low dependency count - only Lodash is needed
var marshal = require('@momsfriendlydevco/marshal');

var serializedString = marshal.serialize({... some complex object ...});

var deserializedObject = marshal.deserialize(serializedString);

See the testkit for more complex examples.

API

marshal.serialize(Object, Settings)

The object or primitive to transform into a string. Settings are inherited from the main marshal.settings structure or overridden by any settings passed in here.

marshal.deserialize(String, Settings)

Transform a serialized string back into a native JS object. Settings are inherited from the main marshal.settings structure or overridden by any settings passed in here.

marshal.settings

The objects to use when operating.

OptionTypeDefaultDescription
cloneBooleanfalseClone the input to the serialize / deserialize functions before operating. Adds overhead but will not mutate the input
circularBooleantrueDetect and manage circular references, if you know the input / output object cannot be circular there is a speed bonus to disabling this
symetricBooleanfalseEncode objects symetrically - i.e. reorder keys so that they can be consistantly hashed
depthNumber0The maximum depth to traverse into when operating. If zero this is infinite
modulesArrayAll modulesWhich modules to use when operating (see notes below)
stringifyBooleantrueWhether to transform the mutated object in marshal.serialize() into a string
destringifyBooleantrueAssume that input to marshal.deserialize() is a string which needs transforming first

NOTES:

  • Modules can be specified as simple strings (e.g. 'date') which assumes they are built in modules provided with the NPM, if this is a path that path is automatically included. If this is an object it is assumed to be an already compatible module
  • All modules must expose a id, test, serialize and deserialize properties