Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Fix a Proxy-Wrapped Object Losing Its Custom Methods After It’s Passed Through JSON.stringify and Rebuilt With JSON.parse

- 29.10.26 - ErcanOPAK

⚡ Fix a Proxy-Wrapped Object Losing Its Custom Methods After It’s Passed Through JSON.stringify and Rebuilt With JSON.parse

JSON.stringify only serializes an object’s own enumerable data properties – it has no concept of a Proxy’s traps, a class’s prototype methods, or any function-valued property, all of which are silently skipped. Round-tripping a reactive object through JSON.stringify and JSON.parse for storage or messaging produces a plain object with the right data but none of the original object’s behavior, and nothing in that process warns that anything was lost.

🔎 The Problem

const state = makeReactive({ count: 0, increment() {
  this.count++;
} }); // makeReactive wraps the object in a Proxy

state.increment();        // works: count === 1, and the
                           // Proxy's set trap fires reactively

const json = JSON.stringify(state);
// '{"count":1}' - the increment method is simply absent;
// JSON.stringify never serializes function-valued properties

const restored = JSON.parse(json);
restored.increment();
// TypeError: restored.increment is not a function

restored.count++;         // works, but this is now a PLAIN
                           // object - no Proxy, no reactivity,
                           // nothing observes this change

✅ Fix: Separate the Data You Serialize From the Behavior You Attach

  • Serialize only the plain data shape (count, in this example) and re-wrap it with makeReactive again after JSON.parse, instead of expecting the round trip to preserve the Proxy or any methods.
  • If the object’s class defines the methods instead of each instance, use a custom toJSON or a reviver function so you can reconstruct a real instance of that class after parsing, rather than a bare object literal.
  • For passing reactive state across a worker or network boundary specifically, prefer structuredClone where it’s supported for plain data, and still re-apply reactivity on the receiving side – structuredClone doesn’t preserve Proxy behavior either.

🚨 Why This Is Dangerous

  • This failure is silent by default – JSON.stringify drops function properties without any warning or error, so the bug only surfaces later as ‘increment is not a function,’ far from the actual serialization call that caused it.

JSON.stringify doesn’t serialize your object – it serializes the parts of it that happen to look like JSON already.

— JavaScript Tip

Related posts:

Ajax Calls Fail When Tab Is Backgrounded

Ajax Calls Fail When Tab Is Backgrounded

JavaScript: Fix Array.prototype.sort() Silently Mutating the Original Array When You Meant to Sort a...

JavaScript: Fix Array.prototype.sort() Silently Mutating the Original Array When You Meant to Sort a...

JavaScript: Why Array Sort Can Reorder Equal Elements Differently Across Browsers

JavaScript: Why Array Sort Can Reorder Equal Elements Differently Across Browsers

Views: 7

Post navigation

ASP.NET Core: Fix a Minimal API Endpoint Returning an Empty JSON Body Because an Async Lambda Returns an Unawaited Task Instead of the Result
CSS: Fix Text Inside a Flex Item Overflowing Past a Fixed-Width Sibling Because Flex Items Default to a Minimum Width of Their Content

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com