diff --git a/packages/fresh/src/jsonify/__snapshots__/round_trip_test.ts.snap b/packages/fresh/src/jsonify/__snapshots__/round_trip_test.ts.snap index f7f4f3716c8..ae21ba2af00 100644 --- a/packages/fresh/src/jsonify/__snapshots__/round_trip_test.ts.snap +++ b/packages/fresh/src/jsonify/__snapshots__/round_trip_test.ts.snap @@ -44,6 +44,8 @@ snapshot[`round trip - URL {\\n href: 'https://fresh.deno.dev/',\\n origin: 'h snapshot[`round trip - 1990-05-31T00:00:00.000Z 1`] = `'[["Date","1990-05-31T00:00:00.000Z"]]'`; +snapshot[`round trip - Invalid Date 1`] = `'[["Date","Invalid Date"]]'`; + snapshot[`round trip - Map(2) { 1 => null, undefined => -2 } 1`] = `'[["Map",[1,-2,-1,2]],1,-2]'`; snapshot[`round trip - Set(5) { 1, 2, null, -2, NaN } 1`] = `'[["Set",[1,2,-2,3,-3]],1,2,-2]'`; diff --git a/packages/fresh/src/jsonify/round_trip_test.ts b/packages/fresh/src/jsonify/round_trip_test.ts index 83e11108c73..c44e5f9bad0 100644 --- a/packages/fresh/src/jsonify/round_trip_test.ts +++ b/packages/fresh/src/jsonify/round_trip_test.ts @@ -34,6 +34,7 @@ const TESTS = [ new Uint8Array([1, 2, 3]), new URL("https://fresh.deno.dev"), new Date("1990-05-31"), + new Date("Invalid Date"), new Map([[1, null], [undefined, -2]]), new Set([1, 2, null, -2, NaN]), [1, , 3], diff --git a/packages/fresh/src/jsonify/stringify.ts b/packages/fresh/src/jsonify/stringify.ts index 1179b5a3cde..00231115f73 100644 --- a/packages/fresh/src/jsonify/stringify.ts +++ b/packages/fresh/src/jsonify/stringify.ts @@ -108,7 +108,13 @@ function serializeInner( if (value instanceof URL) { str += `["URL","${value.href}"]`; } else if (value instanceof Date) { - str += `["Date","${value.toISOString()}"]`; + let iso: string; + try { + iso = value.toISOString(); + } catch { + iso = "Invalid Date"; + } + str += `["Date","${iso}"]`; } else if (value instanceof RegExp) { str += `["RegExp",${JSON.stringify(value.source)}, "${value.flags}"]`; } else if (value instanceof Uint8Array) {