<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Serialization and Deserialization in JS]]></title><description><![CDATA[Serialization and Deserialization in JS]]></description><link>https://serialization-and-deserialization-in-js.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 09:21:16 GMT</lastBuildDate><atom:link href="https://serialization-and-deserialization-in-js.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Serialization & Deserialization in JavaScript – The Doctor Strange’s Teleportation Analogy]]></title><description><![CDATA["The Data Must Travel Across Universes!"
Imagine Doctor Strange using his Sling Ring to teleport between dimensions. 🌌 He opens a portal, steps in, and appears in another universe instantly.
But what if the multiverse has different rules? What if on...]]></description><link>https://serialization-and-deserialization-in-js.hashnode.dev/serialization-and-deserialization-in-javascript-the-doctor-stranges-teleportation-analogy</link><guid isPermaLink="true">https://serialization-and-deserialization-in-js.hashnode.dev/serialization-and-deserialization-in-javascript-the-doctor-stranges-teleportation-analogy</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[js]]></category><dc:creator><![CDATA[Abbas Sakarwala]]></dc:creator><pubDate>Thu, 13 Feb 2025 20:05:08 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1739475301467/a1acd645-2c37-4e52-9b4c-064df84de914.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-the-data-must-travel-across-universes"><strong>"The Data Must Travel Across Universes!"</strong></h2>
<p>Imagine <strong>Doctor Strange</strong> using his <strong>Sling Ring</strong> to teleport between dimensions. 🌌 He opens a portal, steps in, and appears in another universe instantly.</p>
<p>But what if the <strong>multiverse has different rules?</strong> What if one dimension doesn’t understand the form in which Strange arrives? 🤯</p>
<p>That’s where <strong>Serialization &amp; Deserialization</strong> come in—<strong>just like teleportation, data needs to be converted into a universal format (JSON) before traveling across different systems!</strong></p>
<h2 id="heading-step-1-doctor-strange-prepares-for-a-journey-serialization"><strong>Step 1: Doctor Strange Prepares for a Journey (Serialization)</strong></h2>
<p>Doctor Strange <strong>records important details</strong> before entering a portal, so he can reconstruct himself after teleportation.</p>
<pre><code class="lang-javascript">jsCopyEditconst doctorStrange = {  
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Doctor Stephen Strange"</span>,  
    <span class="hljs-attr">abilities</span>: [<span class="hljs-string">"Mystic Arts"</span>, <span class="hljs-string">"Time Manipulation"</span>, <span class="hljs-string">"Teleportation"</span>],  
    <span class="hljs-attr">artifacts</span>: { <span class="hljs-attr">cloak</span>: <span class="hljs-string">"Cloak of Levitation"</span>, <span class="hljs-attr">eye</span>: <span class="hljs-string">"Eye of Agamotto"</span> },  
    <span class="hljs-attr">universe</span>: <span class="hljs-number">616</span>  
};  

<span class="hljs-keyword">const</span> serializedStrange = <span class="hljs-built_in">JSON</span>.stringify(doctorStrange);  
<span class="hljs-built_in">console</span>.log(serializedStrange);

<span class="hljs-comment">//output</span>
<span class="hljs-comment">//{"name":"Doctor Stephen Strange","abilities":["Mystic Arts","Time Manipulation","Teleportation"],"artifacts":{"cloak":"Cloak of Levitation","eye":"Eye of Agamotto"},"universe":616}</span>
</code></pre>
<p>Now, Doctor Strange’s data is serialized and ready to be sent through the multiverse!</p>
<p><img src="https://static1.srcdn.com/wordpress/wp-content/uploads/2023/07/doctor-strange-mcu-portal-powers.jpg" alt="Why Doctor Strange Needs A Ring To Make Portals In The MCU" /></p>
<h2 id="heading-step-2-reconstructing-doctor-strange-in-another-universe-deserialization"><strong>Step 2: Reconstructing Doctor Strange in Another Universe (Deserialization)</strong></h2>
<p>When he arrives in <strong>Earth-838</strong>, their systems can’t understand raw JSON. They need to <strong>deserialize</strong> the data to make him function again.</p>
<pre><code class="lang-javascript">jsCopyEditconst deserializedStrange = <span class="hljs-built_in">JSON</span>.parse(serializedStrange);  
<span class="hljs-built_in">console</span>.log(deserializedStrange.abilities);

<span class="hljs-comment">//output</span>
<span class="hljs-comment">//["Mystic Arts", "Time Manipulation", "Teleportation"]</span>
</code></pre>
<p><strong>Doctor Strange is back in action!</strong> He can now cast spells and manipulate time again.</p>
<p><img src="https://i.pinimg.com/originals/3f/35/90/3f3590a3809163db554425361295f121.jpg" alt="GitHub - Arslanex/Doctor-Strange-Filter: Python project using OpenCV and  Mediapipe to create a Dr. Strange filter. It detects hand gestures,  calculates palm midpoint and openness, then overlays a mask if the hand is" /></p>
<h2 id="heading-flowchart">Flowchart:-</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739475922968/1988c28f-bb2a-4748-aaec-4e56f7e172fe.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-challenges-amp-multiversal-errors-in-serialization"><strong>Challenges &amp; "Multiversal Errors" in Serialization</strong></h2>
<h3 id="heading-1-doctor-strange-lost-his-powers-function-loss-issue"><strong>1) "Doctor Strange Lost His Powers!" (Function Loss Issue)</strong></h3>
<p>If Strange’s <strong>abilities are defined as functions</strong>, they <strong>disappear</strong> when serialized!</p>
<pre><code class="lang-javascript">jsCopyEditconst strange = {  
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Doctor Strange"</span>,  
    <span class="hljs-attr">castSpell</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{ <span class="hljs-keyword">return</span> <span class="hljs-string">"Portal Opened!"</span>; }  
};  

<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">JSON</span>.stringify(strange));

<span class="hljs-comment">//output</span>
<span class="hljs-comment">//{"name":"Doctor Strange"}</span>
</code></pre>
<p>🚨 <strong>Oh no! Strange lost his spell-casting ability!</strong> JSON <strong>removes functions</strong> when converting to a string.</p>
<p><img src="https://i.ytimg.com/vi/QEEK0AnPcsg/hq720.jpg?sqp=-oaymwEhCK4FEIIDSFryq4qpAxMIARUAAAAAGAElAADIQj0AgKJD&amp;rs=AOn4CLAQEC1HA-fG4yx4OUrN_bLVLx_eEw" alt="Doctor Strange Sad Ending Scene - What If Episode 4 - YouTube" /></p>
<h3 id="heading-solution"><strong>Solution?</strong></h3>
<p>Store the result instead of the function.</p>
<pre><code class="lang-javascript">jsCopyEditconst strangeFixed = {  
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Doctor Strange"</span>,  
    <span class="hljs-attr">castSpell</span>: <span class="hljs-string">"Portal Opened!"</span>  
};
</code></pre>
<p><img src="https://45.media.tumblr.com/356c0f260fdb0bf347f69d1e65841abc/tumblr_o03jfx0hGg1qggdn7o1_540.gif" alt="Benedict As Doctor Strange… | HiddleBatch Fans!" /></p>
<h3 id="heading-2-the-eye-of-agamotto-is-missing-data-type-issues"><strong>2) “The Eye of Agamotto is Missing!" (Data Type Issues)</strong></h3>
<p>Some special artifacts <strong>don’t serialize properly</strong> and get lost in the process!</p>
<pre><code class="lang-javascript">jsCopyEditconst timeStoneData = {  
    <span class="hljs-attr">time</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(),  
    <span class="hljs-attr">relics</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-string">"Eye of Agamotto"</span>, <span class="hljs-string">"Darkhold"</span>])  
};  

<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">JSON</span>.stringify(timeStoneData));

<span class="hljs-comment">//output</span>
<span class="hljs-comment">//{"time":"2025-02-14T12:00:00.000Z","relics":{}}</span>
</code></pre>
<p>❌ <strong>The relics are lost!</strong> Just like how Strange lost access to the Time Stone.</p>
<p><img src="https://static1.srcdn.com/wordpress/wp-content/uploads/2021/06/Doctor-strange-2-how-is-th-eye-of-Agamatto-back-after-thanos-destroyed-it-Avengers-infinity-war-.jpg" alt="Doctor Strange 2: How Is The Eye of Agamotto Back After Thanos Destroyed It?" /></p>
<h3 id="heading-solution-1"><strong>Solution?</strong></h3>
<p>Convert unsupported types before serialization.</p>
<pre><code class="lang-javascript">jsCopyEditconst fixedTimeStoneData = {  
    <span class="hljs-attr">time</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().toISOString(),  
    <span class="hljs-attr">relics</span>: [...new <span class="hljs-built_in">Set</span>([<span class="hljs-string">"Eye of Agamotto"</span>, <span class="hljs-string">"Darkhold"</span>])]  
};
</code></pre>
<p><img src="https://wallpapers.com/images/hd/time-stone-doctor-strange-4k-8qjprwk848em0pa7.jpg" alt="Download Time Stone Doctor Strange 4K Wallpaper | Wallpapers.com" /></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Serialization &amp; Deserialization help in:</p>
<ul>
<li><p><strong>Transferring Strange’s details across multiverses (or data across APIs).</strong></p>
</li>
<li><p><strong>Ensuring JSON remains readable and structured for different universes (or servers).</strong></p>
</li>
<li><p><strong>Reconstructing Doctor Strange correctly without losing data.</strong></p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739477369391/20c2a1b9-22f6-4aa2-97cd-122b728530fe.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item></channel></rss>