Hi Bart,<br><br>Your analysis looks about right to me. It's not a known bug - there just aren't really any users of the JSON stuff yet and I suspect none of them are encoding custom classes.<br><br>Care to submit a fix? Either way, you could create an issue at <a href="http://code.google.com/p/seaside/issues/list">http://code.google.com/p/seaside/issues/list</a> .<br>
<br>Cheers,<br><br>Julian<br><br><div class="gmail_quote">On Sun, Jan 31, 2010 at 9:14 AM, Bart Gauquie <span dir="ltr"><<a href="mailto:bart.gauquie@gmail.com">bart.gauquie@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
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="">
</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 style="border: medium none ; margin: 0pt 0pt 0pt 40px; 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 style="border: medium none ; margin: 0pt 0pt 0pt 40px; padding: 0px;">
(Dictionary newFrom:<br></blockquote><blockquote style="border: medium none ; margin: 0pt 0pt 0pt 40px; padding: 0px;"><blockquote style="border: medium none ; margin: 0pt 0pt 0pt 40px; 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 style="border: medium none ; margin: 0pt 0pt 0pt 40px; 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 style="border: medium none ; margin: 0pt 0pt 0pt 40px; 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 style="border: medium none ; margin: 0pt 0pt 0pt 40px; 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 style="border: medium none ; margin: 0pt 0pt 0pt 40px; padding: 0px;">
aStream nextPut: $[.<br>aCollection<br></blockquote><blockquote style="border: medium none ; margin: 0pt 0pt 0pt 40px; padding: 0px;"><blockquote style="border: medium none ; margin: 0pt 0pt 0pt 40px; 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 style="border: medium none ; margin: 0pt 0pt 0pt 40px; 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 style="border: medium none ; margin: 0pt 0pt 0pt 40px; padding: 0px;">aValue jsonOn: aStream<br>
</blockquote><br>(and maybe adding<br>Stream>>json: anObject<br><blockquote style="border: medium none ; margin: 0pt 0pt 0pt 40px; 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 face="arial"><span style="font-size: small;"><font face="'Times New Roman'"><span style="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>
<br>_______________________________________________<br>
seaside mailing list<br>
<a href="mailto:seaside@lists.squeakfoundation.org">seaside@lists.squeakfoundation.org</a><br>
<a href="http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside" target="_blank">http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside</a><br>
<br></blockquote></div><br>