0.2.1 • Published 3 years ago

json-exec v0.2.1

Weekly downloads
2
License
Apache-2.0
Repository
github
Last release
3 years ago

json-exec

Build Status Coverage Status

Fast templated json stringify. Convenient API, very simple to use. Great for logging, RPC, or other use cases where the object schema does not vary.

const json_comp = require('json-exec').json_comp;
const json_exec = require('json-exec').json_exec;

const coder = json_comp({ name: 'String', age: 123 });

json_exec({ name: 'Mickey Mouse', age: 91, color: 'black' });
// => '{"name":"Mickey Mouse","age":91}'
json_exec({ name: 'Minnie Mouse' });
// => '{"name":"Minnie Mouse","age":null}'

For example, to speed up the logging of fixed-pattern objects:

// auto-initialize the logline json encoder
var loglineCoder = loglineCoder || json_comp(logObject);
var logline = json_exec(loglineCoder, logObject);
logger.log(logline);

API

encoder = json_comp( templateObject )

Build a json encoder to stringify the properties of the template. The template is a duck-typing description of the properties and types to encode from data objects; in particular, the template may be an instance of a data object.

The data object properties should be of the same type as the corresponding template properties. A missing data property will be encoded as null (instead of being skipped as by JSON.stringify).

string = json_exec( encoder, dataObject )

Use the encoder to stringify the data object according to the template. Missing values are not skipped, they're stringified as null.

Changelog

  • 0.1.0 - initial version with json_comp and json_exec

Todo

  • allow for constant properties that will not be re-stringified each time
  • build a template-specific stringifier function instead of walking the template

Related Work