How do I encode booleans using yason? - json

I'm trying to use the yason library to encode some data for an API endpoint. Some parts of the return value here are going to be booleans. The problem is
cl-user> (yason:encode nil)
null
NIL
cl-user>
According to their docs, there is a flag named *parse-json-booleans-as-symbols*, but there doesn't seem to be an inverse for encoding. (And the appropriate symbols don't seem to auto-encode appropriately).
cl-user> (yason:encode 'true)
"true" ;; expected `true` rather than `"true"`
"true"
cl-user> (yason:encode 'false)
"false" ;; expected `false` rather than `"false"`
"false"
cl-user>

This isn't a complete answer, but note that nil is the canonical false value in Common Lisp, but also used for lots of other things. The symbol t is the canonical true value, even though any-non nil value will is also a true value. The examples from the YASON documentation show that t is serialized as the JSON true literal:
CL-USER> (yason:encode
(list (alexandria:plist-hash-table
'("foo" 1 "bar" (7 8 9))
:test #'equal)
2 3 4
'(5 6 7)
t nil)
*standard-output*)
[{"foo":1,"bar":[7,8,9]},2,3,4,[5,6,7],true,null]
(#<HASH-TABLE :TEST EQUAL :COUNT 2 {59942D21}> 2 3 4 (5 6 7) T NIL)
If null is treated as a false value by the JSON consumer, the standard behavior with t and nil may be sufficient for your use.
As another option, you could map your false booleans (i.e., nil when used as a boolean) to yason:false in advance. The symbols yason:true and yason:false map to the booleans as you'd expect:
CL-USER> (yason:encode t)
true
;=> T
CL-USER> (yason:encode nil)
null
;=> NIL
CL-USER> (yason:encode 'yason:true)
true
;=> YASON:TRUE
CL-USER> (yason:encode 'yason:false)
false
;=> YASON:FALSE
If you're interested in how I discovered this (I've never used YASON before), I installed YASON with Quicklisp, and then using Slime inspected yason:encode. I discovered that it's a generic function with a number of methods defined:
(DEFGENERIC YASON:ENCODE (YASON::OBJECT &OPTIONAL STREAM))
(DEFMETHOD YASON:ENCODE (EQL ()))
(DEFMETHOD YASON:ENCODE (EQL T))
(DEFMETHOD YASON:ENCODE (EQL NULL))
(DEFMETHOD YASON:ENCODE (EQL YASON:FALSE))
(DEFMETHOD YASON:ENCODE (EQL YASON:TRUE))
(DEFMETHOD YASON:ENCODE LIST)
(DEFMETHOD YASON:ENCODE VECTOR)
(DEFMETHOD YASON:ENCODE HASH-TABLE)
(DEFMETHOD YASON:ENCODE INTEGER)
(DEFMETHOD YASON:ENCODE FLOAT)
(DEFMETHOD YASON:ENCODE RATIO)
(DEFMETHOD YASON:ENCODE STRING)
You can inspect the source of these individually, but the source of the methods on (eql ()) (i.e., (eql NIL)), and of (eql YASON:FALSE) were the important ones.

Related

Where is cons defined in cljs?

I'm having trouble finding where cons has been defined in Clojurescript as it is not there in the ISeq protocol.
https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L616
Is it a compile time or runtime thing?
https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L3297
(defn cons
"Returns a new seq where x is the first element and coll is the rest."
[x coll]
(cond
(nil? coll) (List. nil x nil 1 nil)
(implements? ISeq coll) (Cons. nil x coll nil)
:default (Cons. nil x (seq coll) nil)))
https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L3237
(deftype Cons [meta first rest ^:mutable __hash]
ASeq
ISeq
(-first [coll] first)
(-rest [coll] (if (nil? rest) () rest)))

Is there a Clojure macro equivalent to def?

I want to write a macro, sym-def, which has the same behavior as the special form def but uses (symbol "c"), say, as the first argument.
My first step was
(def (symbol "c") 4)
but this returned the error First argument to def must be a Symbol.
My second step was
(eval `(def ~(symbol "c") 4))
and this succeeded in defining c to be 4 in the global environment.
Why did my first step fail while the second step succeeded?
Finally, I attempted to write the desired macro
(defmacro sym-def [sym value] `(def ~sym ~value))
but this has a "bad" macroexpand
(macroexpand '(sym-def (symbol "c") 4)) => (def (symbol "c") 4)
so that
(sym-def (symbol "c") 4)
fails with the same error as my first step.
What is the correct way to write the macro?
def does not evaluate its first argument. Imagine the chaos if it did! You couldn't write
(def x 1)
because it would first try to evaluate x, and fail because x is not yet defined! Now, since it doesn't evaluate its arguments, clearly it makes sense that
(def (symbol "c") 4)
doesn't work, just as
(def 'c 4)
wouldn't. def requires its first argument to be a literal symbol. You don't have a literal symbol, so you can't use def.
But there is a lower-level mechanism to interact with the mappings in a namespace. In this case, you want clojure.core/intern:
(intern *ns* (symbol "c") 4)
intern is an ordinary function, so it evaluates all its arguments, meaning you can construct your var name in whatever crazy way you want. Then it adds to the given namespace a var mapping your symbol to its desired value.
A correct form for the macro you want to write is the following:
(defmacro sym-def [s v] `(def ~(eval s) ~v))
... or equivalently:
(defmacro sym-def [s v] (list `def (eval s) v))
You just need to evaluate the first argument inside the macro, because a macro's arguments are not evaluated when it is applied. If the only symbol-producing expressions you are going to use are calls to symbol, you may prefer the following macro:
(defmacro defsym [s v] (list `def (symbol s) v))
... and a companion to it:
(defmacro sym [s] (symbol s))
These macros translate a string or symbol to a symbol. Here are some examples of their use:
(defsym "the first natural number" 0)
;=> #'user/the first natural number
(sym "the first natural number")
;=> 0
(defsym pi 3.14159) ;same as: (def pi 3.14159)
;=> #'user/pi
(sym pi) ;same as: pi
;=> 3.14159
The variations given below might also be usefull:
(defmacro defsym* [s v] (list `def (symbol (eval s)) v))
(defmacro sym* [s] (symbol (eval s)))
They translate a string/symbol-producing expression to a symbol after evaluating it. Here are some examples of defsym*'s use:
(defsym* "abc" "xyz")
(defsym* (str \a \b \c) "xyz")
(defsym* (symbol "abc") "xyz")
(defsym* 'abc "xyz")
;all the previous are equivalent and what follows is valid for any of them
;=> #'user/abc
abc
;=> "xyz"
(defsym* abc 0)
;=> #'user/xyz
abc
;=> "xyz"
xyz
;=> 0

LISP - Program to search a specific function through its parameters

For a course project I got to write a program in lisp.
The program should contain the most important lisp functions, their input and output parameters and maybe optional parameters.
For example: function - first, input - list, output - object (first member of list).
The program should work in 2 different ways:
You give the program the name of a function and it should return the function parameters.
You enter function parameters and if a function with these parameters exists, it should return the name of the function.
My questions:
What would be the right way to approach a task like this in lisp? I think maybe a tree would be a way to handle it? (make a tree with all functions and parameters and then write a program which handles it).
Does anyone have a better idea than that to approach this task? Or some suggestions where / how to start? Or Tutorials containing any info?
At the moment I'm a little lost how to start. Any help you can give would be highly appreciated.
English isn't my first language, so I hope everything is understandable.
Greetings.
First of all take a look to prepare your common lisp development environment. After that I think that you should, investigate:
create functions with defun,
declare types.
and things like that. Ffter that take a look to two common lisp functions:
documentation
describe
Here is a little example:
CL-USER> (defun my-sum (a b) "Add my-sum parameters A and B." (+ a b))
MY-SUM
CL-USER> (my-sum 2 3)
5 (3 bits, #x5, #o5, #b101)
CL-USER> (describe #'my-sum)
#<FUNCTION MY-SUM>
[compiled function]
Lambda-list: (A B)
Derived type: (FUNCTION (T T) (VALUES NUMBER &OPTIONAL))
Documentation:
Add my-sum parameters A and B.
Source form:
(SB-INT:NAMED-LAMBDA MY-SUM
(A B)
"Add my-sum parameters A and B."
(BLOCK MY-SUM (+ A B)))
; No values
CL-USER> (documentation 'my-sum 'function)
"Add my-sum parameters A and B."
CL-USER> (defun my-sum (a b) "Add my-sum parameters A and B." (declare (type fixnum a b)) (+ a b))
WARNING: redefining COMMON-LISP-USER::MY-SUM in DEFUN
MY-SUM
CL-USER> (describe #'my-sum)
#<FUNCTION MY-SUM>
[compiled function]
Lambda-list: (A B)
Derived type: (FUNCTION (FIXNUM FIXNUM)
(VALUES
(INTEGER -9223372036854775808 9223372036854775806)
&OPTIONAL))
Documentation:
Add my-sum parameters A and B.
Source form:
(SB-INT:NAMED-LAMBDA MY-SUM
(A B)
"Add my-sum parameters A and B."
(DECLARE (TYPE FIXNUM A B))
(BLOCK MY-SUM (+ A B)))
; No values
Finally, one last tip to work with strings from the output of describe:
CL-USER> (with-output-to-string (*standard-output*)
(describe #'my-sum))
"#<FUNCTION MY-SUM>
[compiled function]
Lambda-list: (A B)
Derived type: (FUNCTION (FIXNUM FIXNUM)
(VALUES
(INTEGER -9223372036854775808 9223372036854775806)
&OPTIONAL))
Documentation:
Add my-sum parameters A and B.
Source form:
(SB-INT:NAMED-LAMBDA MY-SUM
(A B)
\"Add my-sum parameters A and B.\"
(DECLARE (TYPE FIXNUM A B))
(BLOCK MY-SUM (+ A B)))
"
At face value, the task seems to be the construction of a simple symbolic database in memory, which is searchable in two ways. Entries in the database are understood to be functions. The "output parameters" can probably be understood as one or more return values. These things are not named in ANSI Lisp. A useful interpretation of the task is to give return values symbolic labels anyway. Moreover, we can perhaps use type symbols for the return values as well as parameters. So for instance, a database entry for the cons function might look like:
(cons (t t) cons) ;; function named cons takes two objects, returns a cons
The type t is the supertype of all types in ANSI Lisp; it means "any value".
A list of such records can be put into some global variable. Then we write a function that is perhaps named get-params-by-name such that:
(get-params-by-name 'cons) -> (t t)
and another one: get-names-by-params:
(get-names-by-params '(t t)) -> (cons)
This function returns all the matching functions, as a list. More than one function could have this signature.
The trick is then finding a good representation of optional and rest parameters. It could just be the same notation that the language uses:
(list (&rest t) list) ;; list takes rest arguments of any type, returns list
Since we are only interested in exact matches, we don't have to actually parse the &rest notation. When the user queries by parameter, their query object will be literally (&rest t), in that same syntax.
The equal function can be used to tell whether two lists of symbols are identical:
(equal '(&rest t) '(&rest t)) -> t
(equal '(t t) '(t t)) -> nil
So the exercise is not difficult: just mapping through lists, looking for matches.
(defun get-name-by-params (database params)
(let ((matching-entries (remove-if-not (lambda (entry)
(equal (second entry) params))
database)))
(mapcar #'first matching-entries))) ;; just the names, please
Here, the function takes the database list as a parameter, instead of referring to a global variable. The overall program into which we integrate this can provide alternative interfaces, but this is our low-level lookup function.
Test:
[1]> (get-name-by-params '((cons (t t) cons) (list (&rest t) list)) '(integer string))
NIL
[3]> (get-name-by-params '((cons (t t) cons) (list (&rest t) list)) '(t t))
(CONS)
[4]> (get-name-by-params '((cons (t t) cons) (list (&rest t) list)) '(&rest t))
(LIST)
I'd get clarification from the instructor whether this is the right interpretation of the vague requirements, before the assignment is due.
Given that this is a course project I'm going to provide an incomplete answer, and leave you to fill in the blanks.
What the program should do
My interpretation of what you're being asked to do is to provide a utility which will
given the name of a function return its argument list (called a 'lambda list' below);
given a lambda list return all the functions with that lambda list.
So, first of all you need to decide whether two lambda lists are the same or not. As an example is (x) the same as (y), as a lambda list? Yes, it is: the names of formal parameters only matter in the implementation of a function and you generally won't know them: both of these lambda lists mean 'function of one argument'.
The interestring thing is optional arguments of various kinds: (a &optional b) is clearly not the same as (a), but is the same as (b &optional c) but is it the same as (a &optional (b 1 bp))? In this code I say that yes, it is the same: default values and present parameters for optional arguments don't alter whether lambda lists are the same. That's because very often these are implementation details of functions.
A package
We'll put it into a package so it's clear what the interface is:
(defpackage :com.stackoverflow.lisp.fdesc-search
(:use :cl)
(:export
#:defun/recorded
#:record-function-description
#:clear-recorded-functions
#:name->lambda-list
#:lambda-list->names))
(in-package :com.stackoverflow.lisp.fdesc-search)
Recording information
So, to start with we need a mechanism of recording information about functions. We'll do this with a macro which is like defun but records information, which I'll call defun/recorded. We want to be able to record information about things even before the program exists & we do this by having defun/recorded stash 'pending' records on a list which, once the program exists, it will pull off and record properly. That lets us use defun/recorded throughout this code.
;;; These define whether there is a recorder, and if not where pending
;;; records should be stashed
;;;
(defvar *function-description-recorder* nil)
(defvar *pending-function-records* '())
(defmacro defun/recorded (name lambda-list &body forms)
"Like DEFUN but record function information."
;; This deals with bootstrapping by, if there is not yet a recording
;; function, stashing pending records in *PENDING-FUNCTION-RECORDS*,
;; which gets replayed into the recorder at the point it becomes
;; available.
`(progn
;; do the DEFUN first, which ensures that the LAMBDA-LIST is OK
(defun ,name ,lambda-list ,#forms)
(if *function-description-recorder*
(progn
(dolist (p (reverse *pending-function-records*))
(funcall *function-description-recorder*
(car p) (cdr p)))
(setf *pending-function-records* '())
(funcall *function-description-recorder*
',name ',lambda-list))
(push (cons ',name ',lambda-list)
*pending-function-records*))
',name))
Matching lambda lists, first steps
Now we want to be able to match lambda lists. Since we're obviously going to store things indexed by lambda list in some kind of tree we only really need to be able to deal with matching elements of them. And (see above) we don't care about things like default values. I've chosen to do this by first of all simplifying lambda lists to remove them and then matching the simplifies elements: there are other approaches.
simplify-lambda-list does the simplification and argument-matches-p tells you if two arguments match: the interesting bit is that it needs to know about lambda list keywords, which must match exactly, while everything else matches anything. The lambda-list-keywords constant is conveniently provided by the CL standard.
(defun/recorded simplify-lambda-list (ll)
;; Simplify a lambda list by replacing optional arguments with inits
;; by their names. This does not validate the list
(loop for a in ll
collect (etypecase a
(symbol a)
(list (first a)))))
(defun/recorded argument-matches-p (argument prototype)
;; Does an argument match a prototype.
(unless (symbolp argument)
(error "argument ~S isn't a symbol" argument))
(unless (symbolp prototype)
(error "prototype ~S isn't a symbol" prototype))
(if (find-if (lambda (k)
(or (eq argument k) (eq prototype k)))
lambda-list-keywords)
(eq argument prototype)
t))
Function descriptions (partial)
Information about functions is stored in objects called fdescs: the definition of these objects is not given here, but one question we need to answer is 'do two fdescs refer to versions of the same function?' Well, they do if the names of the functions are the same. Remember that function names do not have to be symbols ((defun (setf x) (...) ...) is allowed), so we must compare with equal not eql:
(defun/recorded fdescs-equivalent-p (fd1 fd2)
;; do FD1 & FD2 refer to the same function?
(equal (fdesc-name fd1)
(fdesc-name fd2)))
Storing fdescs indexed by lambda list (partial)
To index things efficiently by lambda list we build a tree. The nodes in this tree are called lambda-list-tree-nodes and their definition is not given here.
There are functions which intern a fdesc in a tree, and which return a list of fdescs indexed by a given lambda list. Neither have an implementation here, but this is what they look like:
(defun/recorded intern-lambda-list (lambda-list tree-node fdesc)
;; return the node where it was interned
...)
(defun/recorded lambda-list-fdescs (lambda-list tree-node)
;; Return a list of fdescs for a lambda list & T if there were any
;; or NIL & NIL if there were not (I don't think () & T is possible,
;; but it might be in some future version)
...)
The implementation of these functions will probably need to use use argument-matches-p and fdescs-equivalent-p.
The top-level databases (slightly partial)
Now we can define the top-level database objects: the root of the tree for indexing by lambda list, and a hashtable for indexing by name
(defvar *lambda-list-tree* (make-lambda-list-tree-node))
(defvar *tree-nodes-by-name* (make-hash-table :test #'equal))
Note that *tree-nodes-by-name* maps from names to the node where the information about that function is stored: that's done to make redefinition easier, as seen in the following function:
(defun/recorded record-function-description (name lambda-list)
"Record information about a function called NAME with lambda list LAMBDA-LIST.
Replace any existing information abot NAME. Return NAME."
(let ((fdesc (make-fdesc :name name :lambda-list lambda-list)))
;; First of all remove any existing information
(multiple-value-bind (node foundp) (gethash name *tree-nodes-by-name*)
(when foundp
(setf (lambda-list-tree-node-values node)
(delete fdesc (lambda-list-tree-node-values node)
:test #'fdescs-equivalent-p))))
(setf (gethash name *tree-nodes-by-name*)
(intern-lambda-list lambda-list *lambda-list-tree* fdesc)))
name)
Note that this function first of all looks up any existing information for name, and if it exists it removes it from the node where it was found. This makes sure that function redefinition does not leave obsolete information in the tree.
This function is the actual recorder which defun/recorded wants to know about, so tell it that:
(setf *function-description-recorder*
#'record-function-description)
Now the next time we invoke defun/recorded it will bootstrap the system by inserting all the the pending definitions.
record-function-description is part of the API to the package: it can be used to record information about functions we don't define.
User-interface functions
Apart from defun/recorded & record-function-description we want some functions which let us make enquiries into the database, as well as one which resets things:
(defun/recorded clear-recorded-functions ()
"Clear function description records. Return no values"
(setf *lambda-list-tree* (make-lambda-list-tree-node)
*tree-nodes-by-name* (make-hash-table :test #'equal))
(values))
(defun/recorded name->lambda-list (name)
"Look up a function by name.
Return either its lambda list & T if it is found, or NIL & NIL if not."
(multiple-value-bind (node foundp) (gethash name *tree-nodes-by-name*)
(if foundp
(values
(fdesc-lambda-list
(find-if (lambda (fd)
(equal (fdesc-name fd) name))
(lambda-list-tree-node-values node)))
t)
(values nil nil))))
(defun/recorded lambda-list->names (lambda-list)
"find function names matching a lambda-list.
Return a list of name & T if there are any, or NIL & NIL if none.
Note that lambda lists are matched so that argument names do not match, and arguments with default values or presentp parameters match just on the argument."
(multiple-value-bind (fdescs foundp) (lambda-list-fdescs lambda-list
*lambda-list-tree*)
(if foundp
(values (mapcar #'fdesc-name fdescs) t)
(values nil nil))))
And that's it.
Examples
After compiling, loading & using the package (with the missing bits added) we can first inject some useful extra functions into it (this is just a random scattering)
> (dolist (x '(car cdr null))
(record-function-description x '(thing)))
nil
> (dolist (x '(car cdr))
(record-function-description `(setf ,x) '(new thing)))
nil
> (record-function-description 'cons '(car cdr))
cons
> (record-function-description 'list '(&rest args))
Now we can make some enquiries:
> (lambda-list->names '(x))
(null cdr
car
lambda-list->names
name->lambda-list
com.stackoverflow.lisp.fdesc-search::simplify-lambda-list)
t
> (lambda-list->names '(&rest anything))
(list)
t
> (name->lambda-list 'cons)
(car cdr)
t
An example of storing things in trees
Below is some code which demonstrates one approach to storing information in trees (often known as tries). This is not usable above for a lot of reasons, but reading it might help implement the missing parts.
;;;; Storing things in trees of nodes
;;;
;;; Node protocol
;;;
;;; Nodes have values which may or may not be bound, and which may be
;;; assigned. Things may be interned in (trees of) nodes with a
;;; value, and the value associated with a thing may be retrieved
;;; along with an indicator as to whether it is present in the tree
;;; under the root.
;;;
(defgeneric node-value (node)
;; the immediate value of a node
)
(defgeneric (setf node-value) (new node)
;; Set the immediate value of a node
)
(defgeneric node-value-boundp (node)
;; Is a node's value bound?
)
(defgeneric intern-thing (root thing value)
;; intern a thing in a root, returning the value
(:method :around (root thing value)
;; Lazy: this arround method just makes sure that primary methods
;; don't need to beother returning the value
(call-next-method)
value))
(defgeneric thing-value (root thing)
;; return two values: the value of THING in ROOT and T if is it present, or
;; NIL & NIL if not
)
;;; Implementatation for STRING-TRIE-NODEs, which store strings
;;;
;;; The performance of these will be bad if large numbers of strings
;;; with characters from a large alphabet are stored: how might you
;;; fix this without making the nodes enormous?
;;;
(defclass string-trie-node ()
;; a node in a string trie. This is conceptually some kind of
;; special case of an abstract 'node' class, but that doesn't
;; actually exist.
((children-map :accessor string-trie-node-children-map
:initform '())
(value :accessor node-value)))
(defmethod node-value-boundp ((node string-trie-node))
(slot-boundp node 'value))
(defmethod intern-thing ((root string-trie-node) (thing string) value)
;; intern a string into a STRING-TRIE-NODE, storing VALUE
(let ((pmax (length thing)))
(labels ((intern-loop (node p)
(if (= p pmax)
(setf (node-value node) value)
(let ((next-maybe (assoc (char thing p)
(string-trie-node-children-map node)
:test #'char=)))
(if next-maybe
(intern-loop (cdr next-maybe) (1+ p))
(let ((next (cons (char thing p)
(make-instance (class-of node)))))
(push next (string-trie-node-children-map node))
(intern-loop (cdr next) (1+ p))))))))
(intern-loop root 0))))
(defmethod thing-value ((root string-trie-node) (thing string))
;; Return the value associated with a string in a node & T or NIL &
;; NIL if there is no value for this string
(let ((pmax (length thing)))
(labels ((value-loop (node p)
(if (= p pmax)
(if (node-value-boundp node)
(values (node-value node) t)
(values nil nil))
(let ((next (assoc (char thing p)
(string-trie-node-children-map node)
:test #'char=)))
(if next
(value-loop (cdr next) (1+ p))
(values nil nil))))))
(value-loop root 0))))
;;; Draw node trees in LW
;;;
#+LispWorks
(defgeneric graph-node-tree (node))
(:method ((node string-trie-node))
(capi:contain
(make-instance 'capi:graph-pane
:roots `((nil . ,node))
:children-function (lambda (e)
(string-trie-node-children-map (cdr e)))
:edge-pane-function (lambda (pane parent child)
(declare (ignore pane parent))
(make-instance
'capi:labelled-line-pinboard-object
:text (format nil "~A" (car child))))
:print-function (lambda (n)
(let ((node (cdr n)))
(format nil "~A"
(if (node-value-boundp node)
(node-value node)
""))))))))

write json object in lisp

Good morning i need help to write this function in lisp.
this is the description.
The json-write function writes the JSON object to the filename file in JSON syntax. If
filename does not exist, it is created and if it exists it is overwritten. Of course it is expected that
CL-PROMPT> (json-load (json-write '(json-obj # | stuff | #) "foo.json"))
(json-obj # | stuff | #)
and this is my function but it isn't correct
(defun json-write (json filename)
(with-open-file (out filename
:direction :output
:if-exists :overwrite
:if-does-not-exist :create)
(pprint (json out))))
thanks
Edit 1:
i try to write var json (that is a json object) into filename.
but pprint doesn't write anything to filename
Edit 2:
(defun json-write (json filename)
(with-open-file (out filename
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(cond
((equal (first json) ‘json-array) (write-arr json))
((equal (first json) ‘json-obj) (write-obj json)))))
so i try this now
if json is json-array lisp call write-arr json
if json is json-obj call write-obj
so my idea is that write-arr trasform
(json-array 1 2 3) in `"[1, 2, 3]"` to make it parsable
and write-obj trasform
(json-obj ("nome" "Arthur") ("cognome" "Dent"))
in
"{\"nome\" : \"Arthur\",
\"cognome\" : \"Dent\"}"
and then write everythings into filename with format stream.
how can i format (json-array 1 2 3) in "[1, 2, 3]".
with format function? and then calling recursively even this function?
thank you
When you write (pprint (json out)) you are trying to call a global function called json. I think you mean (pprint json out).
Now here is roughly how to write a simple json printer:
(defun write-json (object stream)
(etypecase object
(number (format stream “~a” object))
(string (print object stream))
(null (format stream “null”))
(cons
(ecase (car object)
(json-array
(write-char #\[ stream)
(loop for (x . r) on (cdr object)
do (write-json x stream)
(when r (write-char #\, stream)))
(write-char #\] stream))))))
There are plenty of gaps to fill in. And note that this will print strings wrong if they contain new lines. And you probably want to do something less stupid for printing numbers.
Here is how I would implement a json pretty printer with CLOS:
(defvar *json-pretty* nil)
(defvar *json-indent-level* 0)
(defvar *json-indent-spaces* 2)
(defun json-fresh-line (stream)
(if *json-pretty*
(progn
(fresh-line stream)
(loop repeat (* *json-indent-level* *json-indent-spaces*)
do (write-char #\Space stream)))
(write-char #\Space stream))
(defgeneric write-json (object stream))
(defgeneric write-json-collection (type data stream))
(defmethod write-json ((object cons) stream)
(write-json-collection (car object) (cdr object) stream))
(defmethod write-json-collection ((tag (eql json-array)) data stream)
...)
There are plenty of methods left to write.

How to output false while using cl-json

In Common Lisp, I'm using cl-json to output json format, but how can I output a false instead of null?
This is the set of utilities I use when I need to properly handle false with cl-json:
(defclass json-false ()
())
(defmethod json:encode-json ((object json-false) &optional stream)
(princ "false" stream)
nil)
(defvar *json-false* (make-instance 'json-false))
(defun json-bool (val)
(if val t *json-false*))
(defun json-bool-handler (token)
(or (string= token "true")
(and (string= token "false") *json-false*)))
(defmacro preserving-json-boolean (opts &body body)
(declare (ignore opts))
`(let ((json:*boolean-handler* #'json-bool-handler))
,#body))
Now, to encode a literal false, I would way
* (json:encode-json-to-string `((foo . nil) (bar . t) (baz . ,*json-false*)))
"{\"foo\":null,\"bar\":true,\"baz\":false}"
Or, to encode a LISP boolean into a json boolean:
* (let ((something nil))
(json:encode-json-to-string `((bool . ,(json-bool something)))))
"{\"bool\":false}"
Or, to read in JSON data preserving the distinction between null and false:
* (preserving-json-boolean ()
(json:decode-json-from-string "{\"foo\":null,\"bar\":true,\"baz\":false}"))
((:FOO) (:BAR . T) (:BAZ . #<JSON-FALSE #x21029D2E4D>))
Of course, some care must be taken when reading in data like that;
* (when (cdr (assoc :baz *))
'yep)
YEP