check_serializability fails for Date
See original GitHub issueDescribe the bug
I’m getting the following error passing a Date
from the server.
Error: data.currentServerDate returned from 'load' in src/routes/+layout.server.ts cannot be serialized as JSON
at check_serializability (file:///Users/chriscarson/linketysplit-dev-august/sk-date-serialization-issue/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:126:8)
at check_serializability (file:///Users/chriscarson/linketysplit-dev-august/sk-date-serialization-issue/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:120:5)
at load_server_data (file:///Users/chriscarson/linketysplit-dev-august/sk-date-serialization-issue/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:34:3)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async file:///Users/chriscarson/linketysplit-dev-august/sk-date-serialization-issue/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:163:13
Date
is serializable to JSON. The problem is check_serializability.https://github.com/sveltejs/kit/blob/566776afcdae54c6f60f4eaf75933b3041c96fb2/packages/kit/src/runtime/server/page/load_data.js#L96
I understand the function gives you the key of the offending non-serializable type. But the error thrown from JSON.stringify
is already pretty descriptive…
const foo = BigInt(3);
const bar = {foo}
JSON.stringify(bar)
VM270:1 Uncaught TypeError: Do not know how to serialize a BigInt
at JSON.stringify (<anonymous>)
at <anonymous>:1:6
(anonymous) @ VM270:1
If you do want to keep track of the path, maybe we can just add a try/catch before we throw (untested):
/**
* Check that the data can safely be serialized to JSON
* @param {any} value
* @param {string} id
* @param {string} path
*/
function check_serializability(value, id, path) {
const type = typeof value;
if (type === 'string' || type === 'boolean' || type === 'number' || type === 'undefined') {
// primitives are fine
return;
}
if (type === 'object') {
// nulls are fine...
if (!value) return;
// ...so are plain arrays...
if (Array.isArray(value)) {
value.forEach((child, i) => {
check_serializability(child, id, `${path}[${i}]`);
});
return;
}
// ...and objects
const tag = Object.prototype.toString.call(value);
if (tag === '[object Object]') {
for (const key in value) {
check_serializability(value[key], id, `${path}.${key}`);
}
return;
}
}
// added to handle things that fail the above tests
try {
JSON.stringify(value);
return;
} catch (error) {
//ignore
}
throw new Error(`${path} returned from 'load' in ${id} cannot be serialized as JSON`);
}
Reproduction
https://github.com/cdcarson/sk-date-serialization-issue.git
Logs
No response
System Info
ystem:
OS: macOS 12.4
CPU: (8) arm64 Apple M1
Memory: 167.59 MB / 8.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
npm: 8.1.2 - ~/.nvm/versions/node/v16.13.2/bin/npm
Browsers:
Chrome: 104.0.5112.101
Safari: 15.5
npmPackages:
@sveltejs/adapter-auto: next => 1.0.0-next.64
@sveltejs/kit: next => 1.0.0-next.426
@sveltejs/package: next => 1.0.0-next.1
svelte: ^3.44.0 => 3.49.0
vite: ^3.0.0 => 3.0.9
Severity
blocking all usage of SvelteKit
Additional Information
I’m blocked until this is fixed, so I’ll take a stab at modifying the function and writing tests.
Issue Analytics
- State:
- Created a year ago
- Reactions:5
- Comments:5 (4 by maintainers)
Top Results From Across the Web
View Serializability in DBMS - GeeksforGeeks
Methods to check View-Serializability of a schedule – ... Problem : Prove whether the given schedule is View-Serializable or not?
Read more >getServerSideProps Fails to Serialize Date Object #13209
When passing a date over the getServerSideProps boundary, it fails to serialize the Date object. Dates are serializable, so they should be ...
Read more >How to overcome "datetime.datetime not JSON serializable"?
My quick & dirty JSON dump that eats dates and everything: json.dumps(my_dictionary, indent=4, sort_keys=True, default=str). default is a function applied ...
Read more >Serializability Middleware - Redux Toolkit
The function to check if a value is considered serializable. This * function is applied recursively to every value contained in the *...
Read more >CONFLICT SERIALIZABILITY- A Tabular Form Example
Your browser can't play this video. Learn more. Switch camera.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Starting with one of the recent updates to SvelteKit, I’ve been having related issues with Prisma and timestamps stored as BigInt.
https://github.com/prisma/studio/issues/614
@DePasqualeOrg
BigInt
isn’t JSON serializable – you have to do that yourself. It’s not a SvelteKit specific limitation, likeDate
currently is. I raised the issue of serialization today here: https://github.com/sveltejs/kit/discussions/6143#discussion-4322035 and it looks like SvelteKit are working on de/serialization of bothDate
andBigInt
in https://github.com/sveltejs/kit/issues/6008.@lulucas I’m not directly familiar with Prisma Decimal, but it looks like it’s a class with methods rather than a plain object: https://mikemcl.github.io/decimal.js. Can you serialize it to JSON yourself, outside Kit? If you can, it probably means that Kit’s
check_serializability
is objecting to it on those grounds.