Skip to content

fix: handle null in isJSONSerializable (#571)#577

Closed
ruguoba wants to merge 1 commit into
unjs:mainfrom
ruguoba:fix/is-json-serializable
Closed

fix: handle null in isJSONSerializable (#571)#577
ruguoba wants to merge 1 commit into
unjs:mainfrom
ruguoba:fix/is-json-serializable

Conversation

@ruguoba
Copy link
Copy Markdown

@ruguoba ruguoba commented May 13, 2026

Fixes #571

Problem

The isJSONSerializable function has a bug with null values:

  1. typeof null returns "object", not null", so t === null` is dead code
  2. null is valid JSON (JSON.stringify(null) === "null"), so it should be detected as JSON serializable
  3. Passing null to the function throws: TypeError: Cannot read properties of null (reading 'buffer')

Fix

Check value === null explicitly before the typeof checks. Since null is valid JSON, return true.

 export function isJSONSerializable(value: any): boolean {
   if (value === undefined) {
     return false;
   }
+  if (value === null) {
+    return true;
+  }
   const t = typeof value;
-  if (t === "string" || t === "number" || t === "boolean" || t === null) {
+  if (t === "string" || t === "number" || t === "boolean") {
     return true;
   }

Summary by CodeRabbit

  • Bug Fixes
    • Fixed handling of null values in JSON serialization checks, ensuring they are now correctly identified as serializable data.

Review Change Stack

`typeof null` returns `"object"`, not `null`, so the check `t === null`
was dead code. This caused `null` to fall through to the `value.buffer`
check, throwing a TypeError.

Fix by checking `value === null` explicitly before the typeof checks.
Since `null` is valid JSON (`JSON.stringify(null)` === `"null"`),
it should return `true`.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8ffa92f7-8a1c-4b55-9519-7ac8a4394821

📥 Commits

Reviewing files that changed from the base of the PR and between dfbe3ca and 30a9b80.

📒 Files selected for processing (1)
  • src/utils.ts

📝 Walkthrough

Walkthrough

The PR fixes a bug in isJSONSerializable where null values were not recognized as JSON-serializable. An explicit value === null check now returns true before the existing type checks, replacing an unreachable typeof value === null condition and preventing null reference errors.

Changes

Null JSON serialization fix

Layer / File(s) Summary
isJSONSerializable null handling
src/utils.ts
isJSONSerializable adds an explicit early value === null check to properly identify null as JSON-serializable, fixing detection and preventing null reference errors in downstream object property access.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A null once slipped through,
typeof could never say "null" is true—
One explicit check, now null shines bright,
JSON-serializability: fixed just right! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and accurately summarizes the main change: fixing null handling in the isJSONSerializable function.
Linked Issues check ✅ Passed The PR successfully addresses all coding requirements from issue #571: explicitly checks null before typeof, returns true for null, and prevents TypeError from property access.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the null handling bug in isJSONSerializable as specified in issue #571; no extraneous modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ruguoba
Copy link
Copy Markdown
Author

ruguoba commented May 17, 2026

Closing this duplicate PR. The fix is also covered in #578.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isJSONSerializable bug

1 participant