Hi Robert,
Strictly necessary, no. However, I actually think it is a better abstraction than decoding the whole object with the default decoder. While that seems to make the external module JSON agnostic, it will still have to deal with the expected output of cl-json, making it a rather empty abstraction advantage. However, by dispatching directly, the general module acts purely as a proxy and does not impose any restrictions on the structure of the payload element (which comes down to more efficiency, as you mentioned).
The encoder actually allows for this pretty elegantly, I like that API a lot. The general module sets up an object environment with json:with->object, invokes json:as-object-member and passes the stream to the external module. The latter, at this point, only needs to send one json element over the stream and has no business with the wrapping structure. Just like the general module has no business with the semantics of the payload. Combining this with generic functions makes for a very sober encoding framework.
Would it not be a useful feature if the API allowed for this? I am interested in hearing your opinion(s) about it. Of course, there is a difference between "should" and "will" be implemented, but that does not make it an irrelevant discussion. :)
Greetings,
Hraban
2011/8/14 Robert P. Goldman rpgoldman@sift.info:
Is it necessary to do this relatively complex thing, or can you simply decode the JSON object in a standard way and then decode the JSON object into your own object?
Yes, slightly less efficient, but more respectful of the API and likely better abstraction.... -- Sent from my Android phone with K-9 Mail. Please excuse my brevity.
Hraban Luyat hraban@0brg.net wrote:
Hello,
As far as I understand the decoder API it is mostly geared towards defining the decoder for an entire snippet in one place. I would like to know what the recommended way is to separate the definition of the decoder over separate modules.
The semantics of my incoming messages are thus:
object:
- "type": string denoting the type
- "payload": type-specific payload
I want to create a decoder that only extracts the type and uses that to determine which decoder to send the payload to. Then it continues with whatever lisp object the decoder returned.
What I thought would be appropriate is to create a generic function;
(defgeneric json->data (type payload))
and then simply register decoders as follows:
(defmethod json->data ((type (eq :foo)) payload) "Decode message of type foo." ...)
(defmethod json->data ((type (eq :bar)) payload) "Decode message of type bar." ...)
But now I am not really sure how to glue this together. What would you recommend? Is this the right frame of mind at all or should I take a totally different approach?
Thanks!
Hraban
cl-json-devel mailing list cl-json-devel@common-lisp.net http://lists.common-lisp.net/cgi-bin/mailman/listinfo/cl-json-devel