Dear all,<div><br></div><div>I&#39;m creating a jquery plugin in seaside for jqgrid and trying out the json way of loading data. I&#39;ve noticed following behaviour while trying to convert a Dictionary, Collection to a Json string.</div>
<div>
<br></div><div>This is the expected result:</div><div><span style="font-family:&#39;Times New Roman&#39;;font-size:medium"><table><tbody><tr><td style="background-repeat:no-repeat no-repeat">
</td><td style="background-color:white">{&quot;page&quot;:&quot;1&quot;,<br>  &quot;total&quot;:2,<br>  &quot;records&quot;:&quot;13&quot;,<br>   &quot;rows&quot;:<br>      [{&quot;id&quot;:&quot;1&quot;,&quot;cell&quot;:[&quot;1&quot;,&quot;2007-10-01&quot;,&quot;Client 1&quot;,&quot;100.00&quot;,&quot;20.00&quot;,&quot;120.00&quot;,&quot;note 1&quot;]},<br>
    {&quot;id&quot;:&quot;3&quot;,&quot;cell&quot;:[&quot;3&quot;,&quot;2007-10-02&quot;,&quot;Client 2&quot;,&quot;300.00&quot;,&quot;60.00&quot;,&quot;360.00&quot;,&quot;note for invoice 3&quot;]},<br>    {&quot;id&quot;:&quot;2&quot;,&quot;cell&quot;:[&quot;2&quot;,&quot;2007-10-03&quot;,&quot;Client 1&quot;,&quot;200.00&quot;,&quot;40.00&quot;,&quot;240.00&quot;,&quot;note 2&quot;]},<br>
    {&quot;id&quot;:&quot;4&quot;,&quot;cell&quot;:[&quot;4&quot;,&quot;2007-10-04&quot;,&quot;Client 3&quot;,&quot;150.00&quot;,&quot;0.00&quot;,&quot;150.00&quot;,&quot;no tax&quot;]}]}<br><br>I&#39;ve created some classes to generate this structure automatically.<br>
JQRowContent with an id and a rows array. <br><br>JQRowContent&gt;&gt;jsonOn: aStream<br><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">(Dictionary newFrom: {&#39;id&#39;-&gt;id. &#39;cell&#39;-&gt;columnValues}) jsonOn: aStream<br>
<br></blockquote>And the JQGridSearchResponse with a page, totalNumberOfPages, totalNumberOfRecords and rowContents:<br><br>JQGridSearchResponse&gt;&gt;jsonOn: aStream<br><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">
(Dictionary newFrom:<br></blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">
{&#39;page&#39;-&gt;page.<br>&#39;total&#39;-&gt;totalNumberOfPages.<br>&#39;records&#39;-&gt;totalNumberOfRecords.<br>&#39;rows&#39;-&gt;rowContents})<br>jsonOn: aStream<br></blockquote></blockquote><br>Writing out the response is then just:<br>
<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">searchResponse jsonOn: response stream<br><br></blockquote>However, out of the box, using Seaside 3.0 with Javascript-Core-lr.74 it does not work as expected. <br>
<br>I get the following result:<br>&#39;{&quot;page&quot;:1,&quot;total&quot;:1,&quot;rows&quot;:[a JQGridRowContent,a JQGridRowContent,a JQGridRowContent,a JQGridRowContent,a JQGridRowContent],&quot;records&quot;:100000}&#39;<br>
<br>Meaning that the collection of rowContents of the dictionary did not get json encoded recursively. Is this the normal behaviour? If I look at <br>JSStream&gt;&gt;encodeKey: aKey value: aValue on: aStream<br><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">
self encodeKey: aKey on: aStream.<br>aStream nextPut: $:; javascript:aValue<br></blockquote><br>You see that the value (rowcontents array) gets encoded as plain javascript and not as json.<br>Overriding this method in JSJsonStream (which was used) :<br>
JSJsonStream&gt;&gt;encodeKey: aKey value: aValue on: aStream<br><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">self encodeKey: aKey on: aStream.<br>aStream nextPut: $:.<br>
aValue jsonOn: aStream.<br><br></blockquote>and also overriding:<br><br>JSJsonStream&gt;&gt;encodeCollection: aCollection on: aStream<br><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">
aStream nextPut: $[.<br>aCollection<br></blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">
do: [:each|each jsonOn: aStream]<br>separatedBy: [aStream nextPut: $,].<br></blockquote>aStream nextPut: $]</blockquote><br>gives the correct result.<br><br>The above code should get further refactored (2 much duplication) by extracting in JSStream a method to encode a value:<br>
<br>JSStream&gt;&gt;encodeValue: aValue on: aStream<br><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">aStream javascript: aValue<br><br></blockquote>using this method in #encodeCollection on:, #encodeKey: value: on:, and others from JSStream.<br>
<br>and off course overriding this in JSJsonStream.<br>JSJsonStream&gt;&gt;encodeValue: aValue on: aStream<br><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">aValue jsonOn: aStream<br>
</blockquote><br>(and maybe adding<br>Stream&gt;&gt;json: anObject<br><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">anObject jsonOn: self)<br><br></blockquote>Is the behaviour I&#39;m experiencing a known issue? Or is there a reason it is designed this way? I&#39;ve searched the mailing/bug list but could not find anything.<br>
<br>Thanks for any help.<br><br>Kind Regards,<br><br>Bart</td><td style="background-color:white"><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; font-size: small; "><font class="Apple-style-span" face="&#39;Times New Roman&#39;"><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; font-size: medium;"><br>
</span></font></span></font></td><td style="background-color:white"><br></td><td style="background-color:white"><br></td><td style="background-color:white"><br></td><td style="background-color:white"><br></td><td style="background-color:white">
<br></td><td style="background-color:white"><br></td><td style="background-color: white; "><br></td><td style="background-color:white"><br></td><td style="background-color:white"><br></td></tr></tbody></table></span></div>
<div>-- <br>imagination is more important than knowledge - Albert Einstein<br>Logic will get you from A to B. Imagination will take you everywhere - Albert Einstein<br>
Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning. - Albert Einstein<br>The true sign of intelligence is not knowledge but imagination. - Albert Einstein<br>However beautiful the strategy, you should occasionally look at the results. - Sir Winston Churchill<br>

It&#39;s not enough that we do our best; sometimes we have to do what&#39;s required. - Sir Winston Churchill<br>
</div>