-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
157 lines (101 loc) · 4.84 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
* chez-json
fork from <https://github.com/aconchillo/guile-json>
--------------------------------------------------------------
* guile-json
guile-json is a JSON module for Guile. It supports parsing and
building JSON documents according to the http://json.org
specification. These are the main features:
- Strictly complies to http://json.org specification.
- Build JSON documents programmatically via macros.
- Basic unicode support for strings.
- Allows JSON pretty printing.
* Installation
guile-json is freely available for download under the terms of the GNU
Lesser General Public License version 3 (LGPLv3+).
Download the latest tarball and untar it:
- [[http://download.savannah.gnu.org/releases/guile-json/guile-json-0.5.0.tar.gz][guile-json-0.5.0.tar.gz]]
Then, run the typical sequence:
: $ ./configure --prefix=<guile-prefix>
: $ make
: $ sudo make install
Where <guile-prefix> should preferably be the same as your system Guile
installation directory (e.g. /usr).
If everything installed successfully you should be up and running:
: $ guile
: scheme@(guile-user)> (use-modules (json))
: scheme@(guile-user)> (scm->json (json (array 1 2 3)))
: [1, 2, 3]
It might be that you installed guile-json somewhere differently than
your system's Guile. If so, you need to indicate Guile where to find
guile-json, for example:
: $ GUILE_LOAD_PATH=/usr/local/share/guile/site guile
A pkg-list.scm file is also provided for users of the
Guildhall/Dorodango packaging system.
* Usage
guile-json provides a few procedures to parse and build a JSON
document. A JSON document is transformed into or from native Guile
values according to the following table:
| JSON | Guile |
|--------+------------|
| string | string |
| number | number |
| object | hash-table |
| array | list |
| true | #t |
| false | #f |
| null | #nil |
To start using guile-json procedures and macros you first need to load
the module:
: scheme@(guile-user)> (use-modules (json))
** Procedures
- (*json->scm* #:optional port) : Reads a JSON document from the given
port, or from the current input port if none is given.
- /port/ : is optional, it defaults to the current input port.
- (*json-string->scm* str) : Reads a JSON document from the given
string.
- (*scm->json* native #:optional port #:key escape pretty) : Creates a
JSON document from the given native Guile value. The JSON document is
written into the given port, or to the current output port if non is
given.
- /port/ : it defaults to the current output port.
- /escape/ : if true, the slash (/ solidus) character will be escaped.
- /pretty/ : if true, the JSON document will be pretty printed.
- (*scm->json-string* native #:key escape pretty) : Creates a JSON
document from the given native Guile value into a string.
- /escape/ : if true, the slash (/ solidus) character will be escaped.
- /pretty/ : if true, the JSON document will be pretty printed.
** Exceptions
A /json-invalid/ exception is thrown if an error is found during the
JSON parsing. Since version 0.2.0, the /json-invalid/ exception has a
single parser argument (see predicate and accessors below). The line or
column where the error occured can be easily obtained from the parser
port (calling /port-line/ or /port-column/ on the port).
- (*json-parser?* parser) : Tells whether the given argument is a JSON
parser record type.
- (*json-parser-port* parser) : Get the port that the parser was reading
from.
** Macros
It is also possible to build JSON documents programmatically using the
main /json/ macro (and /object/ and /array/). Here are some examples:
- Build the string "hello world":
: scheme@(guile-user)> (scm->json (json "hello world"))
: "hello world"
- Build the [1, 2, 3] array:
: scheme@(guile-user)> (scm->json (json (array 1 2 3)))
: [1, 2, 3]
- Build the [1, 2, 3, 4] array using unquote-splicing:
: scheme@(guile-user)> (define values '(2 3))
: scheme@(guile-user)> (scm->json (json (array 1 ,@values 4)))
: [1, 2, 3, 4]
- Build the object { "project" : "foo", "author" : "bar" }:
: scheme@(guile-user)> (scm->json (json (object ("project" "foo")
: ("author" "bar"))))
: {"author" : "bar", "project" : "foo"}
- Build the object { "values" : [ 234, 98.56 ] }:
: scheme@(guile-user)> (scm->json (json (object ("values" (array 234 98.56)))))
: {"values" : [234, 98.56]}
- Build the object { "values" : [ 234, 98.56 ] } again, this time using
a variable:
: scheme@(guile-user)> (define values '(234 98.56))
: scheme@(guile-user)> (scm->json (json (object ("values" ,values))))
: {"values" : [1, 2, 3]}