Anonymous user
JSON: Difference between revisions
Emacs Lisp: Clean up formatting, add Jansson examples
m (→{{header|Phix}}: added syntax colouring, marked p2js compatible) |
(Emacs Lisp: Clean up formatting, add Jansson examples) |
||
Line 1,340:
=={{header|Emacs Lisp}}==
{{libheader|Jansson}}
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}}
{
"foo": "bar",
"baz": [
1,
2,
3
}
=={{header|Erlang}}==
|