Can JSON.stringify convert ANY javascript variable into text? Or are there limitations (functions, prototypes, etc.)?
JSON.stringify(JSON.stringify)
This returns undefined; JSON does not support functions.
JSON.stringify(/JSON.stringify/)
This returns "{}"; JSON.stringify skips non-enumerable properties.
JSON.stringify(JSON)
This returns "{}"; JSON.stringify skips properties that return unsupported values.
JSON.stringify(JSON.JSON = JSON)
This throws an exception; JSON does not support circular references.
There are two answers to your question:
The caveat is it cannot do this by default, and it cannot do so in any standardized way. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify - specifically the replacer argument here https://developer.mozilla.org/en-US/docs/Using_native_JSON#The_replacer_parameter, which is a function that is like:
function(key,value) {
if (SPECIALLOGIC) {
// ... return some special value
// like {__SPECIAL__:'datetime', value:'some_custom_encoding'}
} else
return value;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With