Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support json-type (:hash-table object) #11

Merged
merged 1 commit into from
Oct 9, 2023

Conversation

riktam
Copy link
Contributor

@riktam riktam commented Feb 17, 2023

Allow json-type of (:hash-table object)

@kilianmh
Copy link
Contributor

For a project of mine, I developed a bit more advanced solution where you can specify the hash-table key-type as well: (:hash-table key-type value-type).
E.g. (:hash-table :string :number), or (:hash-table :any object)

This is my version:

(defmethod to-lisp-value ((value hash-table) (json-type cons))
  (destructuring-bind (type key-type value-type)
      json-type
    (ecase type
      (:hash-table
       (let ((hash-table
               (make-hash-table :size (hash-table-size value)
                                :test (case key-type
                                        ((:string :any)
                                         (function equal))
                                        (otherwise
                                         (function eql))))))
         (maphash #'(lambda (key value)
                      (setf (gethash (to-lisp-value key
                                                    key-type)
                                     hash-table)
                            (to-lisp-value value
                                           value-type)))
                  value)
         hash-table)))))

@riktam
Copy link
Contributor Author

riktam commented Feb 17, 2023

Are you sure that the "spec" allows for keys other than strings?
I'm ok with with either implementation. My case only had string keys.

@kilianmh
Copy link
Contributor

Actually you're right: keys in JSON Objects can only be strings. So your version is superior.

@gschjetne
Copy link
Owner

This looks good to me, but would you consider adding a unit test?

@riktam
Copy link
Contributor Author

riktam commented Aug 27, 2023

Added decoding, tests and updated the documentation.

@kilianmh
Copy link
Contributor

kilianmh commented Aug 30, 2023

You should probably squash the changes into one commit and rebase on master.

@riktam
Copy link
Contributor Author

riktam commented Aug 30, 2023

Done

@gschjetne gschjetne merged commit 4080855 into gschjetne:master Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants