Last active
October 23, 2023 06:44
-
-
Save Genzer/55b2babbc43cdc5e3706a08e822ddb6d to your computer and use it in GitHub Desktop.
Replace JSON.stringify to always remove property having value is null
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function replacerJSONStringify() { | |
if (window.__JSON_stringify_replaced__ === true) { | |
return; | |
} | |
const _originalStringify = JSON.stringify; | |
// NOTE: | |
// ---- | |
// Replace `null` to `undefined` because according to JSON.stringify's | |
// specification, the property is not included in the final serialization. | |
// | |
// This implementation *EXPLICITLY* tests for only `null` value instead of | |
// checking its truthy-ness (i.e Boolean(value)). | |
const _removeNullValue = (key, value) => { | |
if (Array.isArray(value)) { | |
return value.filter(i => i != null); | |
} | |
return (value == null) ? undefined : value; | |
}; | |
// NOTE: | |
// Implementation of this method honors the specification of the original | |
// JSON.stringify. | |
// | |
// > | |
// > As an array, its elements indicate the names of the properties | |
// > in the object that should be included in the resulting JSON string. | |
// > Only string and number values are taken into account; symbol keys are ignored. | |
// | |
const _arrayReplacer = (allowedNames) => (key, value) => { | |
/* The first call to the replacer function is for | |
* the original object itself using key is "". | |
*/ | |
if (key === "") { | |
return value; | |
} | |
if (typeof key == "number" || typeof key == "string") { | |
return allowedNames.includes(String(key)) ? value : undefined; | |
} | |
return value; | |
}; | |
/* | |
* Implementation of this function honors the specification. | |
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#the_replacer_parameter | |
*/ | |
function overwrittenStringify(value, replacer, space) { | |
let decoratedReplacer = replacer; | |
if (!replacer) { | |
decoratedReplacer = _removeNullValue; | |
} | |
else if (typeof replacer == "function") { | |
decoratedReplacer = | |
(key, value) => _removeNullValue(key, replacer(key, value)); | |
} | |
else if (Array.isArray(replacer)) { | |
decoratedReplacer = | |
(key, value) => _removeNullValue(key, _arrayReplacer(replacer)(key, value)); | |
} | |
else if (typeof replacer == "object") { | |
decoratedReplacer = | |
(key, value) => _removeNullValue(key, _arrayReplacer(Object.keys(replacer))(key, value)); | |
} | |
return _originalStringify(value, decoratedReplacer, space) | |
}; | |
JSON.stringify = overwrittenStringify.bind(JSON); | |
window.__JSON_stringify_replaced__ = true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Context
Our team had developed a Angular application which used an HTTP API for a customer. After the customer hardened their WAF rules, they demanded that requests sending JSON payload having properties associating with
null
was not allowed (??).Because all requests were sent using Angular
HttpClient
, and the request body was serialized into JSON string viaHttpRequest.serializedBody()
1 usingJSON.stringify()
, there was an idea to decorateJSON.stringify
to supply areplacer
which filtered out allnull
values.The gist was an actualization of that idea.