Coder Social home page Coder Social logo

Comments (3)

Zulu-Inuoe avatar Zulu-Inuoe commented on May 31, 2024 3

Sure.

Are you going CLOS->JSON, or JSON->CLOS?

Currently there's no built-in for JSON->CLOS, which is what #7 is about.

For CLOS->JSON, the options are in in the section custom serialization. specifically the section about specializing coerced-fields

You can either specialize that for each of your classes, or, if your classes all have a common parent class, you can do something like this:

(defclass foo ()
  ((a-field :initarg :a)
   (besides-this :initarg :b)
   (cant-hurt :initarg :c)))

;; The default method:
(jzon:coerced-fields (make-instance 'foo :a "hello" :b "world")) #| => ((A-FIELD "hello" T) (BESIDES-THIS "world" T)) |#

;; Our specialization
(defmethod jzon:coerced-fields ((obj foo))
  (let* (
         #| Call the base method which grabs all bound slots |#
         (fields (call-next-method))
         #| Alter them by replacing the name part (the `car') |#
         (fields (mapcar (lambda (f) (cons (cffi:translate-camelcase-name (car f)) (cdr f))) fields)))
    fields))

;; Now
(jzon:coerced-fields (make-instance 'foo :a "hello" :b "world")) #| => (("aField" "hello" T) ("besidesThis" "world" T)) |#

Any child class of foo will use this method, so you should get camel-case without having to write a method per class.
FYI here I used cffi:translate-camelcase-name just because it's the first thing that came to mind. You probably want to use something else

from jzon.

Zulu-Inuoe avatar Zulu-Inuoe commented on May 31, 2024 1

An examples section sounds like a good idea, thank you

from jzon.

daninus14 avatar daninus14 commented on May 31, 2024

Thank you so much! This is great! Yeah, CLOS to JSON.

I was about to close the issue, but I feel like it should be visible to people searching for the docs. Would you consider just adding a second coerced-fields example and simply copy pasting your explanation with the title Example: Changing Field Names From Hyphen Case to Camel Case?

Thank you again so much for the help!

from jzon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.