Dear all,<div><br></div><div>I'm creating a jquery plugin in seaside for jqgrid and trying out the json way of loading data. I'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:'Times New Roman';font-size:medium"><table><tbody><tr><td style="background-repeat:no-repeat no-repeat">
</td><td style="background-color:white">{"page":"1",<br> "total":2,<br> "records":"13",<br> "rows":<br> [{"id":"1","cell":["1","2007-10-01","Client 1","100.00","20.00","120.00","note 1"]},<br>
{"id":"3","cell":["3","2007-10-02","Client 2","300.00","60.00","360.00","note for invoice 3"]},<br> {"id":"2","cell":["2","2007-10-03","Client 1","200.00","40.00","240.00","note 2"]},<br>
{"id":"4","cell":["4","2007-10-04","Client 3","150.00","0.00","150.00","no tax"]}]}<br><br>I've created some classes to generate this structure automatically.<br>
JQRowContent with an id and a rows array. <br><br>JQRowContent>>jsonOn: aStream<br><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">(Dictionary newFrom: {'id'->id. 'cell'->columnValues}) jsonOn: aStream<br>
<br></blockquote>And the JQGridSearchResponse with a page, totalNumberOfPages, totalNumberOfRecords and rowContents:<br><br>JQGridSearchResponse>>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;">
{'page'->page.<br>'total'->totalNumberOfPages.<br>'records'->totalNumberOfRecords.<br>'rows'->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>'{"page":1,"total":1,"rows":[a JQGridRowContent,a JQGridRowContent,a JQGridRowContent,a JQGridRowContent,a JQGridRowContent],"records":100000}'<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>>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>>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>>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>>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>>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>>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'm experiencing a known issue? Or is there a reason it is designed this way? I'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="'Times New Roman'"><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's not enough that we do our best; sometimes we have to do what's required. - Sir Winston Churchill<br>
</div>