help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] Re: JSON Dumper/Parser


From: Robin Redeker
Subject: Re: [Help-smalltalk] Re: JSON Dumper/Parser
Date: Thu, 20 Sep 2007 13:25:57 +0200
User-agent: Mutt/1.5.11+cvs20060403

On Sun, Aug 19, 2007 at 01:43:52PM +0200, Paolo Bonzini wrote:
> I attach the code I have.

Here is a small fix which fixes writing json directly to eg. files:

--- json.st     2007-09-20 13:02:49.000000000 +0200
+++ json.st.n   2007-09-20 13:02:19.000000000 +0200
@@ -290,7 +290,6 @@
       f := false
    ].
    ws nextPut: $].
-   ^ws contents
 ! !
 
 !UndefinedObject methodsFor: 'json'!


I guess returning the stream contents there is a left over from my old code.

Btw. do you have json.st somewhere in a repository on your side?
Maybe checking it in to the examples/ directory until it is packaged up
would be best?

You wrote earlier that if someone comes up with an idea how to generate
custom objects it would speed up packaging it, what about this:


--- json.st     2007-09-20 13:20:43.000000000 +0200
+++ json.st.n   2007-09-20 13:20:02.000000000 +0200
@@ -56,6 +56,20 @@
    ^c
 ! !
 
+!JSONReader methodsFor: 'object creation'!
+
+newJSONArrayObject
+   "I return an object which implements at least the add: method for storing
+    the elements of a JSON array."
+    ^OrderedCollection new.
+!
+
+newJSONObject
+   "I return a object which implements the method at:put: for storing
+    the key/value pairs of JSON objects."
+    ^Dictionary new.
+! !
+
 !JSONReader methodsFor: 'private'!
 
 nextJSONObject
@@ -76,7 +90,7 @@
 nextJSONArray
    "I decode JSON arrays from self and will return a OrderedCollection for 
them."
    | c obj value |
-   obj := OrderedCollection new.
+   obj := self newJSONArrayObject.
    self next.
    [ c := self peek.
      (c = $]) ] whileFalse: [
@@ -91,7 +105,7 @@
 nextJSONDict
    "I decode JSON objects from self and will return a Dictionary containing 
all the key/value pairs."
    | c obj key value |
-   obj := Dictionary new.
+   obj := self newJSONObject.
    self next.
    [ c := self peek.
      c = $} ] whileFalse: [



It would allow people to inherit from JSONReader and plug in their own
classes for storing json objects and arrays. Of course this doesn't work
for numbers, strings and true/false, etc. But I don't know if it makes much
sense to allow that much customisation.



Robin




reply via email to

[Prev in Thread] Current Thread [Next in Thread]