smart-replacer
v0.4.0
Published
An extensible JSON stringify replacer with default support for error objects, sets, and maps.
Maintainers
Readme
smart-replacer
An extensible JSON stringify replacer with default support for error objects, sets, and maps.
Installation
npm install smart-replacerUsage
const { createReplacerFunction } = require('smart-replacer');
const movie = {
name: "Star Wars",
cast: new Map([["director", "George Lucas"], ["producer", "Gary Kurtz"]]),
actors: new Set(["Mark Hamill", "Harrison Ford", "Carrie Fisher", "Peter Cushing", "Alec Guinness"])
};
// default output of JSON.stringify
console.log(JSON.stringify(movie));
// => {"name":"Star Wars","cast":{},"actors":{}}
// replacer in action
console.log(JSON.stringify(movie, createReplacerFunction()));
// => {"name":"Star Wars","cast":{"director":"George Lucas","producer":"Gary Kurtz"},"actors":["Mark Hamill","Harrison Ford","Carrie Fisher","Peter Cushing","Alec Guinness"]}
// replacer with monkey patching (change global JSON.stringify)
createReplacerFunction({monkeyPatchJSON: true});
console.log(JSON.stringify(movie));
// => {"name":"Star Wars","cast":{"director":"George Lucas","producer":"Gary Kurtz"},"actors":["Mark Hamill","Harrison Ford","Carrie Fisher","Peter Cushing","Alec Guinness"]}Motivation
Have you ever tried to JSON serialize an error object or a Map? It seems that JavaScript (or ECMAScript, whatever...) doesn't support serialization of these types out-of-the-box. Properly serializing objects to JSON can be a great advantage when it comes to structured logging.
API
The single function createReplacerFunction(options?) takes an options parameter (optional),
and returns a replacer function, which becomes handy when using JSON.stringify
options
useErrorReplacer: boolean (default:true) - should the replacer convert the error objectuseMapReplacer: boolean (default:true) - should the replacer convert aMapobject to a "regular" objectuseSetReplacer: boolean (default:true) - should the replacer convert aSetobject to an arrayreplacers: array (default:[]) - an array of implementation of "Replacer" interface (see below) to be usedmonkeyPatchJSON: boolean (default:false) - should the call have a side effect on the globalJSONobject, so that the resultingreplacerfunction would be also used as an argument toJSON.stringify. Monkey patching is nice and all, but use with caution.
Extensibility
If one wishes to add their own logic, this can be easily done using the following protocol.
Replacer interface
The following interface is a "Replacer":
canHandle(key, value); // returns boolean
replace(key, value); // returns the replaced resultThe protocol
Custom logic can be applied using one or more instances of the Replacer interface described above.
These instances should be passed in the replacers field of the options argument.
The main replacer function would do the following:
for each key-value pair that are evaluated during the `JSON.stringify` execution:
for each customReplacer in options.replacers:
if customReplacer.canHandle(key, value) then
return customReplacer.replace(key, value) and we're done
for each coreReplacer in enabled core replacers (for Error, Map, and Set):
if coreReplacer.canHandle(key, value) then
return coreReplacer.replace(key, value) and we're done
otherwise, return value // unchangedExample:
Suppose you want Date to have a shorter output than the regular ISO representation when serializing to JSON:
const aNewHope = {releaseDate: new Date(1977, 4, 25)}; // May --> 4
console.log(JSON.stringify(aNewHope));
// => {"releaseDate":"1977-05-25T00:00:00.000Z"}
createReplacerFunction({
replacers: [
{
canHandle: function (key, value) {
return value instanceof Date;
},
replace: function (key, value) {
return value.toISOString().replace('T', ' ').substr(0, 19)
}
}
],
monkeyPatchJSON: true
});
console.log(JSON.stringify(aNewHope));
// => {"releaseDate":"1977-05-25 00:00:00"}Notes
- This project serializes a
Setto an array and aMapto an object. This means that once serialized to JSON, there's no way to blindly deserialize it back to in-memory objects. For logging purposes that's fine, but for other scenarios, one might need to know the desired schema in advance for proper deserialization. - A
Mapobject is serialized to a regular object with keys as strings. This means that another extra step would be needed if the keys of the originalMapwere numbers, for instance. - If
options.monkeyPatchJSONis set totrue, thenJSON.stringifyacts differently: if thereplacerargument is missing, or set toundefined,null,0orfalse(anything evaluated tofalse, basically), then the actual replacer to be used is the one returned from the functionReplacer.createReplacerFunction. If, however, the code that callsJSON.stringifyexplicitly provides a replacer functionf, thenfwould be used as the actual replacer.
Testing
First, install the dev dependencies:
npm install --devthen:
npm testThanks
- This project wraps the great work that Sindre Sorhus has done in serialize-error
Discussions and Resources
License
MIT © Ron Klein
