gschjetne / json-mop

A metaclass for bridging CLOS and JSON objects
MIT License
61 stars 9 forks source link

json-mop and inheritance #1

Closed jfmcbrayer closed 3 years ago

jfmcbrayer commented 7 years ago

Hi. I'm kind of a CL newbie, but I'm trying to use json-mop and having an issue with inheritance.

Suppose I have two classes:

(defclass parent ()
  ((foo :accessor foo :initarg :foo 
          :initform "Foo"
          :json-key "foo"))
  (:metaclass json-serializable-class))

(defclass child (parent)
    ((bar :accessor bar :initarg :bar
      :json-key "bar"))
    (:metaclass json-serializable-class))

(encode (make-instance 'parent)) prints exactly what I expect: {"foo":"Foo"}

I would expect (encode (make-instance 'child)) to print exactly the same thing. However, it prints {}.

But:

 (describe (make-instance 'child))
#<CHILD {1004ABEC93}>
  [standard-object]

Slots with :INSTANCE allocation:
  FOO  = "Foo"
  BAR  = #<unbound slot>

Maybe I'm misunderstanding a basic CLOS thing? Shouldn't encoding an instance of child include the values it gets from parent's initforms?

gschjetne commented 7 years ago

You're right, it should. I ended up not using this for my own project, and it never occurred to me to test for this case.

I'm afraid I don't have much time to work on it at the moment and I can't promise I'll get to it soon, but you're welcome to give it a try. A good start might be to create a test case for inheritance. And feel free to ask any questions in the thread.

jfmcbrayer commented 7 years ago

I'll give it a try, but as I said, I'm kind of a newbie. A test case is probably within my capabilities, though.

For reference, what did you end up using for your own project, assuming you still needed to generate JSON for CLOS objects?

gschjetne commented 7 years ago

Just defining the method yason:encode-slots on the objects. That way the internal implementation of the classes won't depend on the external JSON representation or vice versa.

jfmcbrayer commented 7 years ago

Ah, thanks. I will probably do that as well. But I guess I will still write you a failing test case for json-mop.