Comments (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.
An examples section sounds like a good idea, thank you
from jzon.
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)
- CCL/LW/ACL: DEFCONSTANT needs an EVAL-ALWAYS HOT 5
- FLOAT-FEATURES are loaded in a very very dramatic manner HOT 2
- Investigate better binary data support HOT 1
- Better subsequence support
- Tests failed on ECL for v1.0.0 HOT 11
- Automated tests for ECL
- Automated tests for CCL
- quicklisp's jzon does not compile in allegro common lisp HOT 2
- Non-square multi-dimensional arrays stringifying incorrectly
- bug when `stringifying` exponent markers HOT 5
- cannot encode a dotted pair HOT 5
- STRINGIFY a class doesn't work with LispWorks 8.0.1 HOT 3
- Add with-writer-to-string HOT 3
- Can't load it into CLISP HOT 6
- Cannot stringify double-floats in LispWorks 8.0.1 HOT 7
- Cannot `jzon:parse` from pathname on LispWorks
- Shouldn't test precision of short-float on non-integer-divided-by-power-of-2 (maybe intended to test single-float) HOT 1
- Add installation instructions to README HOT 1
- ECL test failure: type mismatch `character` vs `base-char` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jzon.