flightcontrolhq / superjson

Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.
https://www.flightcontrol.dev?ref=superjson
MIT License
4.01k stars 87 forks source link

Support the Temporal API objects #223

Open hornta opened 1 year ago

hornta commented 1 year ago

Like PlainDate and more

https://tc39.es/proposal-temporal/docs/plaindate.html

Skn0tt commented 1 year ago

Hadn't heard of that proposal yet, that looks great! Good work by the Igalia folks.

This is in Stage 3 right now, and not yet available in Browsers (I checked Chrome), so i'd like to hold off on including it in SuperJSON core right now.

If you want to support PlainDate in your project right now, you can use SuperJSON.registerCustom: https://github.com/blitz-js/superjson#decimaljs--prismadecimal

KATT commented 1 year ago

I do this

import { Temporal } from '@js-temporal/polyfill';
import superjson from 'superjson';

superjson.registerCustom<Temporal.PlainDate, string>(
  {
    isApplicable: (v): v is Temporal.PlainDate =>
      v instanceof Temporal.PlainDate,
    serialize: (v) => v.toJSON(),
    deserialize: (v) => Temporal.PlainDate.from(v),
  },
  'Temporal.PlainDate',
);

superjson.registerCustom<Temporal.Instant, string>(
  {
    isApplicable: (v): v is Temporal.Instant => v instanceof Temporal.Instant,
    serialize: (v) => v.toJSON(),
    deserialize: (v) => Temporal.Instant.from(v),
  },
  'Temporal.Instant',
);