From: ······@gmail.com
Subject: If you're familiar w/ CL-JSON HELP ME!
Date: 
Message-ID: <1191822559.118013.274420@50g2000hsm.googlegroups.com>
So.. I can't seem to get cl-json to encode a JSON object that is an
object with an associative array containing associative arrays.  It
essentially complains about a number not being of type list...

(defun get-empty-board()
     (make-array 12 :initial-contents  `(,`("wp" . 0) ,`("wn" .
0) ...))

(encode-json (list (cons "test" (get-empty-board))) *standard-output*)

Also... is there a way to make a JSON object from a class or instance
of one.  I'm not terribly thrilled with CL-JSON and I have half a mind
to code it myself (but that probably end badly)

From: ··@codeartist.org
Subject: Re: If you're familiar w/ CL-JSON HELP ME!
Date: 
Message-ID: <1191851724.874871.269020@g4g2000hsf.googlegroups.com>
On 8 Okt., 07:49, ······@gmail.com wrote:
> Also... is there a way to make a JSON object from a class or instance
> of one.  I'm not terribly thrilled with CL-JSON and I have half a mind
> to code it myself (but that probably end badly)

I'm not sure if I understood you right; you can use the MOP to create
a metaclass for JSON objects.
I've done so for an implementation of a wiki server. I can then just
say:

(defclass article ()
  ((store :accessor store :initarg :store :json nil)
   (uid :accessor uid :initarg :uid)
   (creation-date :accessor creation-date :initarg :creation-date)
   (content :accessor content :initarg :content)
   (attributes :accessor attributes :initarg :attributes))
  (:metaclass json-object-class))

One can add attributes to an JSON-OBJECT-CLASS easily by just doing
(setf (slot-value a-json-object 'some-slot) "Foo"). Additionally I
serialize the class-name of the class of the object to JSON too. If
such a JSON literal gets deserialized it is converted to this class.
This way you get can send and receive JSON-Objects and treat them like
normal CLOS objects.

ciao,
Jochen
From: Kamen TOMOV
Subject: Re: If you're familiar w/ CL-JSON HELP ME!
Date: 
Message-ID: <uve9h7rp6.fsf@cybuild.com>
On Mon, Oct 08 2007, ······@gmail.com wrote:

> So.. I can't seem to get cl-json to encode a JSON object that is an
> object with an associative array containing associative arrays.  It
> essentially complains about a number not being of type list...
>
> (defun get-empty-board()
>      (make-array 12 :initial-contents  `(,`("wp" . 0) ,`("wn" .
> 0) ...))
>
> (encode-json (list (cons "test" (get-empty-board))) *standard-output*)

You can make do without cl-json. Instead use parenscript:

(js*
 `(create :wp 0 :wn ,var :wn (create ....)))
        

> Also... is there a way to make a JSON object from a class or
> instance of one.  

Oh that would be almost CL to JS compiler. One can start from
parenscipt to reach to a point where this:

(defclass obj ()
  ((slot1 :initform (someform))
   (slot2)))

(defgeneric f1 (obj var1 var2))
(defmethod f1 ((obj obj) var1 var2)
  ...)

compiles to something like this:

function Obj = {
  this.slot1 = someForm;
  this.slot2 = null;
}

Obj.prototype.f1 = function (var1, var2) {
                     ...; 
}        

> I'm not terribly thrilled with CL-JSON and I have half a mind to
> code it myself (but that probably end badly)

You would better spend your time evolving parenscript. 

Google already did a Java to JS compiler. Perhaps it's a good idea to
do it with CL.


Regards,

-- 
�����
From: Holger Duerer
Subject: JSON isn't Javascript [Was: If you're familiar w/ CL-JSON HELP ME!]
Date: 
Message-ID: <87bqb7ovan.fsf_-_@ronaldann.demon.co.uk>
>>>>> "Kamen" == Kamen TOMOV <·····@cybuild.com> writes:

    Kamen> On Mon, Oct 08 2007, ······@gmail.com wrote:
    >> [... question how to get a JSON encoded output ...]

    Kamen> You can make do without cl-json. Instead use parenscript:

    Kamen> (js*
    Kamen>  `(create :wp 0 :wn ,var :wn (create ....)))

No you can't.  That outputs Javascript -- in a form that is
syntactically not JSON (missing quotes around the keys).  Many JSON
decoders ignore this difference (CL-JSON does not IIRC).

         Holger
From: Kamen TOMOV
Subject: Re: JSON isn't Javascript
Date: 
Message-ID: <ufy0gzqjd.fsf@cybuild.com>
On Wed, Oct 10 2007, Holger Duerer wrote:

>>>>>> "Kamen" == Kamen TOMOV <·····@cybuild.com> writes:
>
>     Kamen> On Mon, Oct 08 2007, ······@gmail.com wrote:
>     >> [... question how to get a JSON encoded output ...]
>
>     Kamen> You can make do without cl-json. Instead use parenscript:
>
>     Kamen> (js*
>     Kamen>  `(create :wp 0 :wn ,var :wn (create ....)))
>
> No you can't.  That outputs Javascript -- in a form that is
> syntactically not JSON (missing quotes around the keys).  Many JSON
> decoders ignore this difference (CL-JSON does not IIRC).

JSON is a subset of JavaScript as defined in rfc 4627 [1]. It defines
a small set of formatting rules for data representation among which,
indeed, is the requirement for the keys to be strings. So thanks for
pointing that out!

However, it is not that we can't use parenscript for the
purpose. Instead of the former mentioned notation one should write:

(js*
 `(create "wp" 0 "wn" ,var "wn" (create ....)))


References

[1] http://www.ietf.org/rfc/rfc4627.txt

Regards,

-- 
�����
From: Alex Mizrahi
Subject: Re: If you're familiar w/ CL-JSON HELP ME!
Date: 
Message-ID: <470a94f3$0$90271$14726298@news.sunsite.dk>
(message (Hello ·······@gmail.com)
(you :wrote  :on '(Mon, 08 Oct 2007 05:49:19 -0000))
(

 j> So.. I can't seem to get cl-json to encode a JSON object that is an
 j> object with an associative array containing associative arrays.  It
 j> essentially complains about a number not being of type list...

* (json:decode-json-from-string "{\"test\": {\"wp\":0, \"wn\": 1}}")

((:TEST (:WP . 0) (:WN . 1)))
* (json:encode-json-to-string '((:TEST (:WP . 0) (:WN . 1))))

"{\"test\":{\"wp\":0,\"wn\":1}}"

 j> (defun get-empty-board()
 j>      (make-array 12 :initial-contents  `(,`("wp" . 0) ,`("wn" .
 j> 0) ...))

why array? why `,` freaking stuff?

 j> Also... is there a way to make a JSON object from a class or instance
 j> of one.

this totally depends on how you're going to use it, so it's not included 
into general purpose library.
simpliest way would be quite easy to do in few lines of code -- use MOP 
stuff to collect slots and make list of them, then CL-JSON do the rest.

however, you might want to make some slots serializable, some not -- this 
leads you to MOP.
you might want to add functions to your objects -- that leads you to 
parenscript, maybe..

 j> I'm not terribly thrilled with CL-JSON and I have half a mind to code it
 j> myself (but that probably end badly)

it's fine if you'll implement your own high-level functions, but i recomend 
to use standard ones for actual serialization -- at least they were somehow 
tested.. it's pretty easy to misunderstand or oversimplify the standard and 
make terrible errors, for example authors of a popular Mootools JavaScript 
libraries did so!

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"Hanging In The Balance Of Deceit And Blasphemy")