Roundtripping bigint and number #676
LeviticusMB
started this conversation in
General
Replies: 2 comments 3 replies
-
|
OK, I managed to hack it but it's not pretty: const stringify = (value: unknown) => {
const doc = new YAML.Document(value);
YAML.visit(doc, (key, node) => {
if (key === 'value' && node instanceof YAML.Scalar && typeof node.value === 'number' && /^[-+0-9]+$/.test(JSON.stringify(node.value))) {
node.minFractionDigits ??= 1;
}
});
return doc.toString();
};The regexp test is there to prevent Is there a better way? |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
yaml-types has |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using the parser as a JSON alternative, that is, i want to parse/serialize objects, arrays and primitives.
I can call
YAML.parseAllDocuments(..., { intAsBigInt: true })go get a document collection withbigintnodes, also preserved when callingtoJS({ mapAsMap: false, ...}).bigintis also serialized OK. All good here.However, I also need to ensure that a
numberis never serialized as an integer but always a float, or else thenumberwill turn into abigintafter serdes.How would I do that? (For JSON, I have a replacer is basically just returning
JSON.rawJSON(`${serialized}.0`)ifserializedis all digits.)Beta Was this translation helpful? Give feedback.
All reactions