3 ms·
This isn't about parsing the JSON format but the mapping between JSON datatypes and your language datatypes. JSON numbers are arbitrary precision, how you choo
by Spivak 8y ago
This isn't about parsing the JSON format but the mapping between JSON datatypes and your language datatypes.
JSON numbers are arbitrary precision, how you choose the represent those numbers in your language varies widely, and up to you.
Adding hooks into JSON.parse would actually solve a whole class of problems related to datatype mappings. Right now they're statically defined by whoever implements the parse method but it turns out not to be very flexible.
What if I want to represent my JSON array as an ArrayBuffer or a SortedArray?
What if I want to represent my JSON numbers as strings or my libraries number type for working with currency.
What if I want to map my hashes to my own HashMap?
Being able to say JSON.parse({ array: SortedArray, number: BigInt }) would be very nice.
- olliej 8y agoJSON.parse already supports that through the reviver parameter.
- Spivak 8y agoIt doesn't quite do what I'm looking for since it only allows you to transform data after it's already been mapped to a type. So if my integer can't fit into the Number type without loss of precision then my reviver function is going to get the lossy number so it's already to late to do a conversion to BigInt. I need to actually hook into the parser so I can say, "hey when you're about to parse a number pass the token to me and I'll take it from here."