4 ms·
That's not what I mean. Even though it is serializable, it's still not the same when you serialize/deserialize it. For example `JSON.parse(JSON.stringify(Tempo
by VanCoding 7mo ago
That's not what I mean. Even though it is serializable, it's still not the same when you serialize/deserialize it.
For example `JSON.parse(JSON.stringify(Temporal.PlainYearMonth.from({year:2026,month:1}))).subtract({ years: 1})` won't work, because it misses the prototype and is no longer an instance of Temporal.PlainYearMonth.
This is problematic if you use tRPC for example.
- gowld 7mo agoWould a plain data object be an instance of PlainYearMonth? If not, that regardless of being plain data or a serialized object with functions, you'd still need to convert it to the type you want.
- flyingmeteor 7mo agoYou would need to use the `reviver` parameter of `JSON.parse()` to revive your date strings to Temporal objects. As others have said, it's a simple `Temporal.from()` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- cyral 7mo agoI've been doing this for so long and never knew there was a reviver param, thanks - that is super useful.
- Bratmon 7mo agoHaving to provide a complete schema of your json everywhere your json gets parsed negates the advantages of json.
- true_religion 7mo agoThe main advantage of json is that it’s human readable and writable. Beyond that, it has no notion of user created data types so anyone using it has to do custom unmarshalling to get a type apart from sting, number, dict and list.
- hueho 7mo agoMost JSON libraries in typed languages require this for data binding to complex types though.
- Bratmon 7mo agoWhich is why many developers only use JavaScript Object Notation for JavaScript objects, and only JavaScript objects that can losslessly be written as JSON. Which this proposal explicitly does not support.
- afiori 7mo ago{"$temporal_type":"PlainYearMonth","$data":"........"}
- hdjrudni 7mo agoEven if you don't explicitly provide a schema, you implicitly still have one. The recipient needs to know what you're sending them. Unless maybe you want to start parsing JSON payloads with an LLM.
- foresterre 7mo agoDepending on your needs (i.e. how you would otherwise use your output jspn), using the reviver can have a significant impact on performance. JSON.parse itself is hyper-optimized. At the company I work we used the reviver for almost exactly this, but profiling showed that using the reviver had enormous impact on performance. We cut it out, and won in the seconds of performance for some large json's.
- HanyouHottie 7mo agoInstead of using the reviver you can deserialize as a plain string, then later reassign the property as the type you want. E.g.: const myObject = JSON.parse(text); myObject.date = new Date(myObject.date);
- rimunroe 7mo ago> For example `JSON.parse(JSON.stringify(Temporal.PlainYearMonth.from({year:2026,month:1}))).subtract({ years: 1})` won't work, because it misses the prototype and is no longer an instance of Temporal.PlainYearMonth. I don't know if I'm missing something, but that's exactly how I'd expect it to compose. Does the following do what you wanted your snippet to do? Temporal.PlainYearMonth.from(JSON.parse(JSON.stringify(Temporal.PlainYearMonth.from({year:2026,month:1})))) JSON.stringify and JSON.parse should not be viewed as strict inverses of each other. `JSON.parse(JSON.stringify(x)) = x` is only true for a for a small category of values. That category is even smaller if parsing is happening in a different place than stringification because JSON doesn't specify runtime characteristics. This can lead to things like JSON parsing incorrect in JS because they're too large for JS to represent as a number.
- tshaddox 7mo agoThis is also true of JavaScript Date instances, so I’m curious what solution you had that did work with raw JSON stringify and parse.
- aubergene 7mo agoThis seems more to do with how JSON works than Temporal. There are libraries such as Devalue which will handle this for you `devalue.parse(devalue.stringify(Temporal.PlainYearMonth.from({year:2026,month:1}))).subtract({ years: 1})` https://www.npmjs.com/package/devalue https://www.npmjs.com/package/devalue
- cyberrock 7mo agoThen you're talking about changing JSON.parse to start parsing some schema as a type instead of object, which would break compatibility.
- jonathrg 7mo agoThe JSON types are string, number, boolean, null, object and array. So how could the suggested code possibly work? Do you want JSON.parse to do arbitrary code execution like Python's pickle?