Skip to content

Instantly share code, notes, and snippets.

@Genzer
Last active October 23, 2023 06:44
Show Gist options
  • Save Genzer/55b2babbc43cdc5e3706a08e822ddb6d to your computer and use it in GitHub Desktop.
Save Genzer/55b2babbc43cdc5e3706a08e822ddb6d to your computer and use it in GitHub Desktop.
Replace JSON.stringify to always remove property having value is null
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;
}
@Genzer
Copy link
Author

Genzer commented Oct 20, 2023

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 via HttpRequest.serializedBody() 1 using JSON.stringify(), there was an idea to decorate JSON.stringify to supply a replacer which filtered out all null values.

The gist was an actualization of that idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment