<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>ADM Blog &#187; deserialize</title> <atom:link href="http://blog.another-d-mention.ro/tag/deserialize/feed/" rel="self" type="application/rss+xml" /><link>http://blog.another-d-mention.ro</link> <description>No matter how you see things, reality changes when you reach understanding</description> <lastBuildDate>Fri, 22 Jul 2011 06:56:49 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1</generator> <item><title>Serialize JavaScript object to JSON</title><link>http://blog.another-d-mention.ro/programming/flex-actionscript/serialize-javascript-object-to-json/</link> <comments>http://blog.another-d-mention.ro/programming/flex-actionscript/serialize-javascript-object-to-json/#comments</comments> <pubDate>Wed, 28 Apr 2010 11:57:51 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Flex / ActionScript]]></category> <category><![CDATA[Java Script]]></category> <category><![CDATA[deserialize]]></category> <category><![CDATA[flash]]></category> <category><![CDATA[flex]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[json]]></category> <category><![CDATA[object]]></category> <category><![CDATA[serialize]]></category> <guid
isPermaLink="false">http://blog.another-d-mention.ro/?p=816</guid> <description><![CDATA[Recently, on a project I was working, I needed a function to serialize a JavaScript object and all I could find online were scripts and jquery plugins for serializing a html form. Then, I found this, a script that takes advantage of the .toSource() method available in Gecko-based browsers and for the rest of them [...]
Related posts:<ol><li><a
href='http://blog.another-d-mention.ro/programming/how-to-clone-duplicate-an-object-in-actionscript-3/' rel='bookmark' title='Permanent Link: How to clone (duplicate) an object in ActionScript 3'>How to clone (duplicate) an object in ActionScript 3</a></li><li><a
href='http://blog.another-d-mention.ro/programming/java-script/for-javascript-devs/' rel='bookmark' title='Permanent Link: For Javascript devs'>For Javascript devs</a></li><li><a
href='http://blog.another-d-mention.ro/programming/javascript-object-oriented-programming-oop-jclass/' rel='bookmark' title='Permanent Link: JavaScript Object-Oriented Programming (OOP) &#8211; jClass'>JavaScript Object-Oriented Programming (OOP) &#8211; jClass</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Recently, on a project I was working, I needed a function to serialize a JavaScript object and all I could find online were scripts and jquery plugins for serializing a html form. Then, I found <a
href="http://blog.stchur.com/2007/04/06/serializing-objects-in-javascript/" target="_blank">this</a>, a script that takes advantage of the <i>.toSource()</i> method available in Gecko-based browsers and for the rest of them plain old recursion. But it does the trick as expected and I'm pretty sure someone else might need it.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td
class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> serialize<span style="color: #009900;">&#40;</span>_obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_obj <span style="color: #339933;">!=</span> undefined <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> _obj.<span style="color: #660066;">toSource</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">'undefined'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> _obj.<span style="color: #660066;">callee</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> _obj.<span style="color: #660066;">toSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> _obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'number'</span><span style="color: #339933;">:</span>
      <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'boolean'</span><span style="color: #339933;">:</span>
      <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'function'</span><span style="color: #339933;">:</span>
         <span style="color: #000066; font-weight: bold;">return</span> _obj<span style="color: #339933;">;</span>
         <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'string'</span><span style="color: #339933;">:</span>
          <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'&quot;'</span> <span style="color: #339933;">+</span> _obj.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&quot;/mg</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;'</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'object'</span><span style="color: #339933;">:</span>
          <span style="color: #003366; font-weight: bold;">var</span> str<span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_obj.<span style="color: #660066;">constructor</span> <span style="color: #339933;">===</span> Array <span style="color: #339933;">||</span> <span style="color: #000066; font-weight: bold;">typeof</span> _obj.<span style="color: #660066;">callee</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              str <span style="color: #339933;">=</span> <span style="color: #3366CC;">'['</span><span style="color: #339933;">;</span>
              <span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> _obj.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
              <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> str <span style="color: #339933;">+=</span> Utils.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span>_obj<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">','</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
              str <span style="color: #339933;">+=</span> Utils.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span>_obj<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">']'</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
              str <span style="color: #339933;">=</span> <span style="color: #3366CC;">'{'</span><span style="color: #339933;">;</span>
              <span style="color: #003366; font-weight: bold;">var</span> key<span style="color: #339933;">;</span>
              <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>key <span style="color: #000066; font-weight: bold;">in</span> _obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> str <span style="color: #339933;">+=</span> key <span style="color: #339933;">+</span> <span style="color: #3366CC;">':'</span> <span style="color: #339933;">+</span> Utils.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span>_obj<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">','</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
              str <span style="color: #339933;">=</span> str.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\,$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'}'</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
          <span style="color: #000066; font-weight: bold;">return</span> str<span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
          <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'&quot;&quot;'</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>To deserialize it, just use <i>eval()</i>.</p><p>Also, if you need the same thing for your Flash/Flex Actionscript3 project, here is a class for that as well -> <a
href="http://blog.another-d-mention.ro/stuff/Serializer.as" target="_blank">Click Me</a>.</p><p>Cheers! </pre><p>Related posts:<ol><li><a
href='http://blog.another-d-mention.ro/programming/how-to-clone-duplicate-an-object-in-actionscript-3/' rel='bookmark' title='Permanent Link: How to clone (duplicate) an object in ActionScript 3'>How to clone (duplicate) an object in ActionScript 3</a></li><li><a
href='http://blog.another-d-mention.ro/programming/java-script/for-javascript-devs/' rel='bookmark' title='Permanent Link: For Javascript devs'>For Javascript devs</a></li><li><a
href='http://blog.another-d-mention.ro/programming/javascript-object-oriented-programming-oop-jclass/' rel='bookmark' title='Permanent Link: JavaScript Object-Oriented Programming (OOP) &#8211; jClass'>JavaScript Object-Oriented Programming (OOP) &#8211; jClass</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://blog.another-d-mention.ro/programming/flex-actionscript/serialize-javascript-object-to-json/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
