by roncohen · 5y ago · 0 comments
- Still no date type
- Why make it more loose? Like "Strings may be single quoted." doesn't bring any value.The looseness seems to be aimed at human authors who want to write JSON as if it were JS, so mainly package.json. But, json is not ideal for configuration for multiple reasons.
{
thisIsADate: new Date('1995-12-17T03:24:00Z')
}
It would be simple enough for parsers in other languages to parse this into their own date objects. Much simpler than using regexs to find ISO strings. {
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:
{
birthdayParty: Date 1632817436514,
sounds: String? { // zomgwtfbbq comments too
cow:"moo",
fox: null
},
possibilities: ["she loves me", "she loves me not", 42],
aSymbol: Symbol "tag",
aBuffer: Uint8 [104, 101, 108, 108, 111]
}But it's a matter of taste still and nothing that we couldn't do before. Such changes will lead to meetings where programmers discuss JSON5 quoting choices (yes, it will be a thing).
‘I am “really” angry’
Is much nicer than: “I am \”really\” angry”is this any better?
'I\'m "really" angry, don\'t do it again!'
in many languages single quotes are used as apostrophes and are very very common, much more common than double quotes."I'm \"really\" angry, don't do it again!"