JSON: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
(Emacs Lisp: Clean up formatting, add Jansson examples)
Line 1,340: Line 1,340:


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
A JSON encoder and decoder is in package json, so load it first:
<lang Lisp>(require 'json)</lang>
===Decoding===
<lang Lisp>(setq example "{\"foo\": \"bar\", \"baz\": [1, 2, 3]}")
(json-read-from-string example)
</lang>
{{out}}
<lang Lisp>((foo . "bar")
(baz .
[1 2 3]))</lang>
===Decoding, representing data as property lists===
<lang Lisp>(let ((json-object-type 'plist))
(json-read-from-string))</lang>
{{out}}
<lang Lisp>(:foo "bar" :baz
[1 2 3])</lang>
===Decoding, representing data as hash table===
<lang Lisp>(let ((json-object-type 'hash-table))
(json-read-from-string example))</lang>
{{out}}
<pre>#<hash-table equal 2/65 0x1563c39805fb></pre>


{{libheader|Jansson}}
===Encoding===

<Lang Lisp>(json-encode example-object)</Lang>
Emacs 27.1 offers native JSON processing using the Jansson library.

<lang Lisp>(require 'cl-lib)

(cl-assert (fboundp 'json-parse-string))
(cl-assert (fboundp 'json-serialize))

(defvar example "{\"foo\": \"bar\", \"baz\": [1, 2, 3]}")
(defvar example-object '((foo . "bar") (baz . [1 2 3])))

;; decoding
(json-parse-string example) ;=> #s(hash-table [...]))
;; using json.el-style options
(json-parse-string example :object-type 'alist :null-object nil :false-object :json-false)
;;=> ((foo . "bar") (baz . [1 2 3]))
;; using plists for objects
(json-parse-string example :object-type 'plist) ;=> (:foo "bar" :baz [1 2 3])

;; encoding
(json-serialize example-object) ;=> "{\"foo\":\"bar\",\"baz\":[1,2,3]}"</lang>

{{libheader|json.el}}

<lang Lisp>(require 'json)

(defvar example "{\"foo\": \"bar\", \"baz\": [1, 2, 3]}")
(defvar example-object '((foo . "bar") (baz . [1 2 3])))

;; decoding
(json-read-from-string example) ;=> ((foo . "bar") (baz . [1 2 3]))
;; using plists for objects
(let ((json-object-type 'plist))
(json-read-from-string)) ;=> (:foo "bar" :baz [1 2 3])
;; using hash tables for objects
(let ((json-object-type 'hash-table))
(json-read-from-string example)) ;=> #<hash-table equal 2/65 0x1563c39805fb>

;; encoding
(json-encode example-object) ;=> "{\"foo\":\"bar\",\"baz\":[1,2,3]}"
;; pretty-printing
(let ((json-encoding-pretty-print t))
(message "%s" (json-encode example-object)))</lang>

{{out}}
{{out}}

<pre>"{\"list\":{\"cons\":[[\"quote\",\"x\"],2],\"cons\":[[\"quote\",\"y\"],\"a string\"]}}"</pre>
{
===Encoding, pretty printing output===
"foo": "bar",
<lang Lisp>(let ((json-encoding-pretty-print t))
"baz": [
(json-encode example-object))</lang>
1,
{{out}}
2,
<pre>
3
"{
\"list\": {
]
}
\"cons\": [
[
\"quote\",
\"x\"
],
2
],
\"cons\": [
[
\"quote\",
\"y\"
],
\"a string\"
]
}
}"
</pre>
===Encoding, with options===
<lang Lisp>(let* ((json-encoding-pretty-print t)
(json-object-type 'alist)
(json-array-type 'vector)
(json-key-type 'symbol)
(vec (make-vector 3 "element")))
(aset vec 1 "second")
(aset vec 2 -3)
(json-encode (list (cons 'x 2)
(cons 'y "a string")
(cons 'z vec))))</lang>
{{out}}
<pre>"{
\"x\": 2,
\"y\": \"a string\",
\"z\": [
\"element\",
\"second\",
-3
]
}"</pre>


=={{header|Erlang}}==
=={{header|Erlang}}==