-
Notifications
You must be signed in to change notification settings - Fork 9
cookbook scenario to JSON.parse bigint in schema-less data #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
similarly, would this be an ok cookbook solution for the schema-less JSON.stringify roundtrip? require("http").createServer(function (req, res) {
let replacer;
let result;
replacer = function (ignore, val) {
/*
* this [schema-less] replacer will stringify and annotate
* bigint/bigdecimal <val> with unique prefix "bignum_j9234v9d_"
*/
if (typeof val === "bigdecimal" || typeof val === "bigint") {
return "bignum_j9234v9d_" + val.toString();
}
return val;
};
result = {
"dict": {
"bigdecimal": 12345678901234567890.1234d,
"bigint": 12345678901234567890n,
"float": 1234.5678,
"int": 1234
},
"list": [
12345678901234567890.1234d,
12345678901234567890n,
1234.5678,
1234
]
};
// 1st-pass to annotate bigint/bigdecimal
result = JSON.stringify(result, replacer, 4);
// result = "{\
// \"dict\": {\
// \"bigdecimal\": \"bignum_j9234v9d_12345678901234567890.1234\",\
// \"bigint\": \"bignum_j9234v9d_12345678901234567890\",\
// \"float\": 1234.5678,\
// \"int\": 1234,\
// },\
// \"list\": [\
// \"12345678901234567890.1234\",\
// \"12345678901234567890\",\
// 1234.5678,\
// 1234\
// ]\
// }"
// 2nd-pass to remove bigint/bigdecimal annotation
result = result.replace((
/"bignum_j9234v9d_(.*?)"/g
), "$1");
// result = "{\
// \"dict\": {\
// \"bigdecimal\": 12345678901234567890.1234,\
// \"bigint\": 12345678901234567890,\
// \"float\": 1234.5678,\
// \"int\": 1234,\
// },\
// \"list\": [\
// 12345678901234567890.1234,\
// 12345678901234567890,\
// 1234.5678,\
// 1234\
// ]\
// }"
res.end(result);
}).listen(8080); |
Your Likewise for your |
if json-form has decimal-point e.g. at risk of beating a dead-horse, are you open to revisiting issue #5 to improve usability (and maybe performance) for this common, schemaless scenario? require("https").request({
"url": "https://third-party.api.com/arbitrary_data"
}, async function (clientResponse) {
let result;
let jsonOptions;
jsonOptions = {
// upscale-as-needed to bigint/bigdecimal to preserve numeric precision.
// user can ad-hoc-enforce bignum-schema
// after JSON.parse has done its job.
"bignumPreservePrecision": true,
// security
"bignumMaxDigits": 1000
};
result = await ... // read responseText from http-client
// result = "[\
// 12345678901234567890.0,\
// 12345678901234567890,\
// 1234.5678,\
// 1234\
// ]"
result = JSON.parse(result, jsonOptions);
// result = [
// 12345678901234567890.0d,
// 12345678901234567890n,
// 1234.5678,
// 1234
// ]
// user can ad-hoc-enforce bignum-schema
// after JSON.parse has done its job.
result = result.map(function (elem) {
return BigDecimal(elem);
});
// result = "[
// 12345678901234567890.0d,
// 12345678901234567890.0d,
// 1234.5678d,
// 1234.0d
// ]"
result = JSON.stringify(result, jsonOptions, 4);
// result = "[\
// 12345678901234567890.0,\
// 12345678901234567890.0,\
// 1234.5678,\
// 1234.0\
// ]"
require("fs").writeFileSync(
"/Downloads/data.json",
result
);
}).end(); |
I'm not opposed to introducing an options parameter for |
k, closing this issue, though i might reopen to revisit bigdecimal |
say i'm working on an agile web-project with bigint where either:
the schema changes so frequently with each iteration that the schema-based
reviver
function (key, val, src, keys) {...}
becomes tech-debtor it has schema-less dictionaries with arbitrary key/val pairs.
in both cases i need an idiot-proof, schema-less JSON.parse solution that will preserve integer-precision by up-coercing integers to bigint as needed. would a cookbook solution look as follows?
The text was updated successfully, but these errors were encountered: