AI solution for JavaScript Coding Question on BFE.dev
144. serialize and deserialize data types not supported in JSON
One possible solution could be to define custom serialization and deserialization functions for each special data type:
function serialize(value) { if (typeof value === "bigint") { return { _type: "bigint", value: value.toString() }; } else if (value != value) { // NaN check return { _type: "nan" }; } else if (value === Infinity) { return { _type: "infinity", sign: 1 }; } else if (value === -Infinity) { return { _type: "infinity", sign: -1 }; } else { // use default JSON.stringify for other types return value; }}function deserialize(value) { if (value && typeof value === "object") { switch (value._type) { case "bigint": return BigInt(value.value); case "nan": return NaN; case "infinity": return value.sign * Infinity; default: return value; } } else { // use default JSON.parse for other types return value; }}function stringify(data) { return JSON.stringify(data, serialize);}function parse(string) { return JSON.parse(string, deserialize);}
The serialize
function checks for each special data type and returns an object that includes a _type
field and other necessary fields for that type. For example, for BigInts it stores the string representation of the value, for NaN it simply indicates the type as "nan", and for Infinity it stores the sign as 1 or -1.
The deserialize
function checks if the input value is an object with a _type
field, and if so, creates a new value based on the type and the stored information. For example, for BigInts it calls the BigInt
constructor with the stored string value, for NaN it returns the NaN
constant, and for Infinity it multiplies the stored sign with the Infinity
constant.
The stringify
and parse
functions simply use JSON.stringify
and JSON.parse
with the custom serialize
and deserialize
functions passed as the second argument, respectively.
Now we can test it with the examples from the prompt:
console.log(parse(stringify([1n, null, undefined, NaN]))); // [1n, null, undefined, NaN]console.log(parse(stringify({a: undefined, b: NaN}))); // {a: undefined, b: NaN}
Both examples should output the expected results.