Permalink
Browse files

added some unserialisation code and tests

  • Loading branch information...
chrisdew
chrisdew committed Feb 7, 2011
1 parent 8753ec1 commit b402a952d3c099e1e0dd7a60814388ef1950048b
Showing with 86 additions and 31 deletions.
  1. +31 −8 docs/swoj-codec-test.html
  2. +13 −7 docs/swoj-codec.html
  3. +1 −1 docs/util.html
  4. +14 −8 lib/swoj-codec.js
  5. +1 −1 lib/util.js
  6. +26 −6 test/swoj-codec-test.js
View
@@ -35,12 +35,35 @@
<span class="o">:</span> <span class="nx">wilma</span>
<span class="p">,</span> <span class="s2">&quot;serialise&quot;</span>
<span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">deepEqual</span><span class="p">(</span><span class="nx">codec</span><span class="p">.</span><span class="nx">serialise</span><span class="p">(</span><span class="nx">topic</span><span class="p">),</span> <span class="s1">&#39;{&quot;__constructor__&quot;:&quot;Person&quot;,&quot;personalName&quot;:&quot;Wilma&quot;,&quot;familyName&quot;:&quot;Flintstone&quot;,&quot;spouse&quot;:&quot;__UUID__:1&quot;,&quot;house&quot;:&quot;__UUID__:0&quot;,&quot;__uuid__&quot;:&quot;2&quot;}&#39;</span><span class="p">);</span> <span class="p">}</span>
- <span class="p">}</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>, "deserialise house"
-: { topic
- : '"<strong>constructor":"House","address":"301 Cobblestone Wy., Bedrock, 70777","</strong>uuid__":"0"}'
- , "unserialise"
- : function(topic) { assert.deepEqual(codec.deserialise(topic), house) }
- }</p> </td> <td class="code"> <div class="highlight"><pre> <span class="p">}</span>
-<span class="p">)</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>TODO test unserialisation</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">suite</span><span class="p">.</span><span class="kr">export</span><span class="p">(</span><span class="nx">module</span><span class="p">);</span>
+ <span class="p">}</span>
+ <span class="p">,</span> <span class="s2">&quot;deserialise house&quot;</span>
+ <span class="o">:</span> <span class="p">{</span> <span class="nx">topic</span>
+ <span class="o">:</span> <span class="s1">&#39;{&quot;__constructor__&quot;:&quot;House&quot;,&quot;address&quot;:&quot;301 Cobblestone Wy., Bedrock, 70777&quot;,&quot;__uuid__&quot;:&quot;0&quot;}&#39;</span>
+ <span class="p">,</span> <span class="s2">&quot;deserialise&quot;</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">deepEqual</span><span class="p">(</span><span class="nx">codec</span><span class="p">.</span><span class="nx">deserialise</span><span class="p">(</span><span class="nx">topic</span><span class="p">,</span> <span class="p">{</span><span class="s2">&quot;House&quot;</span><span class="o">:</span> <span class="nx">model</span><span class="p">.</span><span class="nx">House</span><span class="p">,</span> <span class="s2">&quot;Person&quot;</span><span class="o">:</span> <span class="nx">model</span><span class="p">.</span><span class="nx">Person</span><span class="p">}),</span> <span class="nx">house</span><span class="p">)</span> <span class="p">}</span>
+ <span class="p">}</span>
+ <span class="cm">/*</span>
+<span class="cm"> , &quot;deserialise all&quot;</span>
+<span class="cm"> : { topic</span>
+<span class="cm"> : function() {</span>
+<span class="cm"> var instances = {};</span>
+<span class="cm"> var ob0 = codec.deserialise(&#39;{&quot;__constructor__&quot;:&quot;House&quot;,&quot;address&quot;:&quot;301 Cobblestone Wy., Bedrock, 70777&quot;,&quot;__uuid__&quot;:&quot;0&quot;}&#39;);</span>
+<span class="cm"> instances[ob0.__uuid__] = ob0;</span>
+<span class="cm"> var ob1 = codec.deserialise(&#39;{&quot;__constructor__&quot;:&quot;Person&quot;,&quot;personalName&quot;:&quot;Fred&quot;,&quot;familyName&quot;:&quot;Flintstone&quot;,&quot;spouse&quot;:&quot;__UUID__:2&quot;,&quot;house&quot;:&quot;__UUID__:0&quot;,&quot;__uuid__&quot;:&quot;1&quot;}&#39;);</span>
+<span class="cm"> instances[ob1.__uuid__] = ob1;</span>
+<span class="cm"> var ob2 = codec.deserialise(&#39;{&quot;__constructor__&quot;:&quot;Person&quot;,&quot;personalName&quot;:&quot;Wilma&quot;,&quot;familyName&quot;:&quot;Flintstone&quot;,&quot;spouse&quot;:&quot;__UUID__:1&quot;,&quot;house&quot;:&quot;__UUID__:0&quot;,&quot;__uuid__&quot;:&quot;2&quot;}&#39;);</span>
+<span class="cm"> instances[ob2.__uuid__] = ob2;</span>
+<span class="cm"> codec.fixRefs(instances);</span>
+<span class="cm"> return instances;</span>
+<span class="cm"> }</span>
+<span class="cm"> , &#39;deserialise&#39;</span>
+<span class="cm"> : function(topic) {</span>
+
+<span class="cm">//DIVIDER</span>
+<span class="cm"> }</span>
+<span class="cm"> }</span>
+<span class="cm"> */</span>
+ <span class="p">}</span>
+<span class="p">)</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>TODO add some nested topics here</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">suite</span><span class="p">.</span><span class="kr">export</span><span class="p">(</span><span class="nx">module</span><span class="p">);</span>
-</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
+</pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>TODO test unserialisation</p> </td> <td class="code"> <div class="highlight"><pre>undefined</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
@@ -6,7 +6,9 @@
psuedo-class because I can see other codecs needing state. It would be nice
to reduce the two-way linkage between db and codecs. I'll be able to
refactor the db reference into a nicer interface, once I know what the
-codecs require.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">SwojCodec</span><span class="p">(</span><span class="nx">magic</span><span class="p">)</span> <span class="p">{</span>
+codecs require.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">util</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./util&#39;</span><span class="p">);</span>
+
+<span class="kd">function</span> <span class="nx">SwojCodec</span><span class="p">(</span><span class="nx">magic</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">=</span> <span class="nx">magic</span><span class="p">;</span>
<span class="p">}</span>
@@ -34,9 +36,11 @@
<span class="p">}</span>
<span class="k">return</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">tmp</span><span class="p">);</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>This takes a JSON string, and returns an object.
-The real work is done by fixLinks, the objects </p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">deserialise</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">string</span><span class="p">,</span> <span class="nx">constructors</span><span class="p">,</span> <span class="nx">store</span><span class="p">)</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">tmp</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">deserialise</span><span class="p">);</span>
- </pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Find the constructor.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">constructor</span> <span class="o">=</span> <span class="nx">constructors</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;constructor&quot;</span><span class="p">)];</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>And the UUID.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">uuid</span> <span class="o">=</span> <span class="nx">tmp</span><span class="p">[</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;uuid&quot;</span><span class="p">)];</span>
+The real work is done by fixLinks, the objects </p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">deserialise</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">string</span><span class="p">,</span> <span class="nx">constructors</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">tmp</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">string</span><span class="p">);</span>
+ </pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Find the constructor.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">Constructor</span> <span class="o">=</span> <span class="nx">constructors</span><span class="p">[</span><span class="nx">tmp</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;constructor&quot;</span><span class="p">)]];</span>
+ <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;Constructor:&quot;</span><span class="p">,</span> <span class="nx">Constructor</span><span class="p">);</span>
+ </pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>And the UUID.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">uuid</span> <span class="o">=</span> <span class="nx">tmp</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;uuid&quot;</span><span class="p">)];</span>
</pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Create the object - see <a href="http://stackoverflow.com/questions/3362471/how-can-i-call-a-javascript-constructor-using-call-or-apply">Stack Overflow</a>
for a detailed explanation.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">Tmp</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){};</span> <span class="c1">// temporary constructor</span>
<span class="kd">var</span> <span class="nx">inst</span><span class="p">,</span> <span class="nx">ret</span><span class="p">;</span> <span class="c1">// instance and temporary return value</span>
@@ -45,11 +49,13 @@
<span class="nx">inst</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Tmp</span><span class="p">;</span>
<span class="nx">ret</span> <span class="o">=</span> <span class="nx">Constructor</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">inst</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">ob</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">(</span><span class="nx">ret</span><span class="p">)</span> <span class="o">===</span> <span class="nx">ret</span> <span class="o">?</span> <span class="nx">ret</span> <span class="o">:</span> <span class="nx">inst</span><span class="p">;</span>
- <span class="nx">ob</span><span class="p">[</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;uuid&quot;</span><span class="p">)]</span> <span class="o">=</span> <span class="nx">uuid</span><span class="p">;</span> <span class="c1">// Fix UUID.</span>
+ <span class="nx">ob</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;uuid&quot;</span><span class="p">)]</span> <span class="o">=</span> <span class="nx">uuid</span><span class="p">;</span> <span class="c1">// Fix UUID.</span>
- <span class="nx">ownRealKeys</span><span class="p">(</span><span class="nx">tmp</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">keys</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">store</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">tmp</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
+ <span class="nx">util</span><span class="p">.</span><span class="nx">ownRealKeys</span><span class="p">(</span><span class="nx">tmp</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">keys</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">ob</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">tmp</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
<span class="p">});</span>
+
+ <span class="k">return</span> <span class="nx">ob</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>This replaces up any remaining values of <strong>UUID</strong>:... with references to the
correct object, and also handles dates, etc.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">fixRefs</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">instances</span><span class="p">)</span> <span class="p">{</span>
<span class="p">}</span>
View
@@ -4,7 +4,7 @@
<span class="kd">var</span> <span class="nx">accumulator</span> <span class="o">=</span> <span class="p">[];</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">propertyName</span> <span class="k">in</span> <span class="nx">o</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>FIXME: we should find a way of checking that the trailing magic is
present too. </p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span> <span class="nx">o</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">propertyName</span><span class="p">)</span>
- <span class="o">&amp;&amp;</span> <span class="nx">propertyName</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="k">this</span><span class="p">.</span><span class="nx">magic</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="o">!==</span> <span class="nx">magic</span>
+ <span class="o">&amp;&amp;</span> <span class="nx">propertyName</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">magic</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="o">!==</span> <span class="nx">magic</span>
<span class="p">)</span> <span class="p">{</span>
<span class="nx">accumulator</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">propertyName</span><span class="p">);</span>
<span class="p">}</span>
Oops, something went wrong.

0 comments on commit b402a95

Please sign in to comment.