|
I think allowing a whitelisted set of constructors (so it's extensible, but with some standard ones) as types in a future JSON-like could be good. e.g. {
birthdayParty: Date(1632817436514),
sounds: Map([["cow", "moo"], ["fox", "?"]]),
possibilities: Set(["she loves me", "she loves me not"]),
aSymbol: Symbol("tag"),
aBuffer: UInt8Array(ArrayBuffer([104, 101, 108, 108, 111])),
}
So you'd do something like const serialised = NuJSON.stringify(data)
const result = NuJSON.parse(serialised, allowedConstructors, fallbackConstructor)
And if allowedConstructors wasn't present, it'd default to including all the standard JS ones (Map,Set,Date,Symbol,ArrayBuffer etc). The fallback constructor would be used if there were data types in the data that didn't have matching allowed constructors. By default it would through an error.Of course, my ideal NuJSON would also include bigints, multiline strings, comments, bare keys (i.e. without having to wrap them in quotes). Stretch nuJSON would also include the ability to serialise circular graph structures, perhaps through the use of a Reference([up, up, "aSymbol"]) data type. |
I think all JSON needs in this regard is type hinting. Let everything else be handled by the application: