Julien Danjou: Overriding cl-json object encoding
CL-JSON provides an encoder for
Lisp data structures and objects to JSON format. Unfortunately, in some
case, its default encoding mechanism for CLOS objects isn't exactly doing
the right thing. I'll show you how Common Lisp makes it easy to change that.
Identifying the problem
CL-JSON & CLOS
CL-JSON mechanism encoding CLOS object is really neat. Let's see how it
works for a simple case:
(defclass kitten ()
((tail :initarg :tail)))
(json:encode-json-to-string (make-instance 'kitten :tail 'black))
will produce:
"tail":"black"
Still using CL-JSON, we can also decode the JSON object to a CLOS object:
(slot-value
(json:with-decoder-simple-clos-semantics
(json:decode-json-from-string " \"tail\":\"black\" "))
:tail)
That code will return "black". Note that it's also possible to specify which class should be used when decoding objects, but that's beyond the purpose of this article. Postmodern Now, let's introduce Postmodern, a wonderful Common Lisp system providing access to the wonderful PostgreSQL database. It also provides a simple system to map rows in a database to CLOS classes, called DAO for Database access objects. With this, we can easily store our kitten into a table.
(defclass kitten ()
((tail :initarg :tail))
(:metaclass postmodern:dao-class))
If we try to encode this to JSON, it will produce the exact same result seen previously. The problem is what happens when one of our column has a NULL value. Postmodern encodes this using the :null symbol. So this code:
(defclass kitten ()
((tail :initarg :tail :col-type (or s-sql:db-null text)))
(:metaclass postmodern:dao-class))
(postmodern:deftable kitten
(postmodern:!dao-def))
(postmodern:connect-toplevel )
(postmodern:create-table 'kitten)
(json:encode-json-to-string
(postmodern:make-dao 'kitten))
will return:
" "tail":"null" "
Fail! The fact that the column is NULL is represented by the :null symbol. And CL-JSON encodes all symbols as string. This is not at all what we want here! Overriding encode-json CL-JSON provides and uses the encode-json method to encode all kind of object. It is defined as a generic function, and a lot of different methods are implemented to handle the different standard Common Lisp types. The one used for standard-object is defined liked that:
(defmethod encode-json ((o standard-object)
&optional (stream *json-output*))
"Write the JSON representation (Object) of the CLOS object O to
STREAM (or to *JSON-OUTPUT*)."
(with-object (stream)
(map-slots (stream-object-member-encoder stream) o)))
All we need to do here, is to create a new method for our kitten objects, that handles correctly the :null case.
(defclass kitten ()
((tail :initarg :tail :col-type (or s-sql:db-null text)))
(:metaclass postmodern:dao-class))
(export 'kitten)
;; Switch package just to define the new method
(in-package :json)
(defmethod encode-json ((o cl-user:kitten)
&optional (stream json:*json-output*))
"Write the JSON representation (Object) of the postmodern DAO CLOS object
O to STREAM (or to *JSON-OUTPUT*)."
(with-object (stream)
(map-slots (lambda (key value)
(as-object-member (key stream)
(encode-json (if (eq value :null) nil value) stream)))
o)))
;; Go back into our package
(in-package :cl-user)
(postmodern:deftable kitten
(postmodern:!dao-def))
(postmodern:connect-toplevel )
(postmodern:create-table 'kitten)
(json:encode-json-to-string
(postmodern:make-dao 'kitten))
With that new method, as soon as we encounter a :null symbol as a value for an object's slot, we replace it by nil. Now if we try to encode another kitten, we'll get:
"tail":null
which is far better for our JavaScript data consumers! In the end, I think that this kind of trick is feasible that easily because of the way CLOS provides its generic method implementation. The fact that methods don't belong to any class makes the extension of every program, library and class so much easier. Doing this in another language like Java would likely by impossible, and in Python it would unlikely be as clean as it is done in Common Lisp. The ability to teach any library about how it should handle your class just by defining a new method is really handy!
This is my monthly summary of my free software related activities. If you re among the people who 




I want to start this post by saying two things:

So far, Logitech has been kind enough to help me understanding part of the
protocol and even sent me a few devices so I can play and test my work with
them. Unfortunately, this will probably requires some work and time, and so
far Logitech was not able to help with that.
There should be enough information out there to at least add support for
battery to HID++ 2.0 devices, and probably a few other things too. I hope
I'd get the time do this at some point, but feel free to beat me in this
race!
A (well, now former) coworker let me know about a problem he was having with a Lenovo Thinkcentre M92p. It booted Fedora UEFI install media fine, but after an apparently successful installation refused to boot. UEFI installs on Windows worked perfectly. Secure Boot was quickly ruled out, but this could still have been a number of things. The most interesting observation was that the Fedora boot option didn't appear in the firmware boot menu at all, but Windows did. We spent a little while comparing the variable contents, gradually ruling out potential issues - Linux was writing an entry that had an extra 6 bytes in a structure, for instance[1], and a sufficiently paranoid firmware implementation may have been tripping up on that. Fixing that didn't help, though. Finally we tried just taking the Windows entry and changing the descriptive string. And it broke.
<iframe allowfullscreen="true" frameborder="0" height="479" mozallowfullscreen="true" src="https://docs.google.com/presentation/embed?id=1i30roVZp00Wvo46F4k5CT98sw2uMgaf5Lh3bSfiQ-Cg&start=false&loop=false&delayms=3000" webkitallowfullscreen="true" width="600"></iframe>
I sometimes ask other co-ops and social enterprises to publish things as web pages, PDFs, ODFs or basically any standard format instead of Microsoft Word Doc files.
Doc files have the practical problem that they look different even in different versions of Microsoft Word, but also, Microsoft is not a co-operative (a private-sector firm oft accused of bring a monopolist, in fact!), whereas PDF, ODF and so on are more co-operatively developed, so I feel that we should support the better alternatives.
The usual first move is to PDFs. I don t like them as much as web pages (it s a file to download and open in an app or plugin, rather than something I can just read without interrupting my flow), but the process is basically the same. Just upload a PDF where you would have uploaded a Doc.
Occasionally, I get a reply saying that PDF is no better than Word because Adobe are also a private-sector supplier and therefore not much different from Microsoft (not as big, though?), while doc files are an ISO standard too.
I m careful to suggest using ISO PDFs rather than Adobe PDFs. Although PDF started with Adobe, it was given to