summaryrefslogtreecommitdiff
path: root/man/2/json
blob: 7443b0e405c034b584d41c4b369c483868394b14 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
.TH JSON 2
.SH NAME
json: readjson, writejson, JValue \- read, write and represent values in JavaScript Object Notation
.SH SYNOPSIS
.EX
include "json.m";
json := load JSON JSON->PATH;

JValue: adt {
    pick{
    Object =>
        mem:   cyclic list of (string, ref JValue);
    Array =>
        a:     cyclic array of ref JValue;
    String =>
        s:     string;
    Int =>
        value: big;
    Real =>
        value: real;
    True or
    False or
    Null =>
    }
    isarray:   fn(v: self ref JValue): int;
    isfalse:   fn(v: self ref JValue): int;
    isint:     fn(v: self ref JValue): int;
    isnull:    fn(v: self ref JValue): int;
    isnumber:  fn(v: self ref JValue): int;
    isobject:  fn(v: self ref JValue): int;
    isreal:    fn(v: self ref JValue): int;
    isstring:  fn(v: self ref JValue): int;
    istrue:    fn(v: self ref JValue): int;
    copy:      fn(v: self ref JValue): ref Jvalue;
    eq:        fn(v: self ref JValue, v: ref JValue): int;
    get:       fn(v: self ref JValue, mem: string): ref JValue;
    set:       fn(v: self ref JValue, mem: string, value: ref JValue);
    text:      fn(v: self ref JValue): string;
};

init:      fn(bufio: Bufio);
readjson:  fn(input: ref Bufio->Iobuf): (ref JValue, string);
writejson: fn(output: ref Bufio->Iobuf, val: ref JValue): int;

jvarray:   fn(a: array of ref JValue): ref JValue.Array;
jvbig:     fn(b: big): ref JValue.Int;
jvfalse:   fn(): ref JValue.False;
jvint:     fn(i: int): ref JValue.Int;
jvnull:    fn(): ref JValue.Null;
jvobject:  fn(m: list of (string, ref JValue)): ref JValue.Object;
jvreal:    fn(r: real): ref JValue.Real;
jvstring:  fn(s: string): ref JValue.String;
jvtrue:    fn(): ref JValue.True;
.EE
.SH DESCRIPTION
.B JSON
provides value representations, and encoding and decoding operations for the JavaScript Object Notation (JSON)
described by Internet RFC4627
and summarised in
.IR json (6).
.PP
.B Init
must be called before invoking any other operation of the module.
The
.I bufio
parameter must refer to the instance of
.IR bufio (2)
that provides the
.B Iobuf
parameters used for input and output.
.PP
.B JValue
is the internal representation of values transmitted by the JSON encoding.
They are distinguished in a pick adt:
.TP
.B JValue.False
Represents the value
.BR false .
.TP
.B JValue.Null
Represents a null value of any type.
.TP
.B JValue.True
Represents the value
.BR true .
.TP
.B JValue.Int
Represents as a Limbo
.B big
a JavaScript number with no fractional part.
.TP
.B JValue.Real
Represents as a Limbo
.B real
a JavaScript number with a fractional part.
.TP
.B JValue.String
Represents a JavaScript string of Unicode characters from
.B +U'0000'
to
.BR +U'FFFF' .
.TP
.B JValue.Array
Represents a JavaScript array of values of any type.
.TP
.B JValue.Object
Represents a tuple of (member/property name, value) pairs corresponding to the value of a JavaScript object,
or an associative array or table, indexed by strings.
The member names must be unique within a value.
.PP
.B Readjson
reads a single value in JSON
format from the
.I input
stream and returns a tuple
.BI ( val,\ err ).
On success,
.I val
is a
.B JValue
that represents the value successfully read.
If an error occurs,
.I val
is nil and
.I err
contains a diagnostic.
.PP
.B Writejson
writes to the
.I output
stream in JSON format a
representation of the value
.IR v .
It returns 0 on success and -1 on error (setting the system error string).
.PP
The easiest way to create a new
.B JValue
for subsequent output is with one of the module-level functions
.BR jvarray ,
.BR jvint ,
.BR jvobject ,
.BR jvstring ,
and so on.
As values of a pick adt, a
.B JValue
can be inspected using Limbo's
.B tagof
operator and the appropriate variant accessed using a
.B pick
statement.
.B JValue
also supports several groups of common operations, for smaller, tidier code.
First, the set of enquiry functions
.IB v .is X ()
return true if the value
.I v
is an instance of the JavaScript type
.I X
.RI ( array ,
.IR int ,
.IR object ,
.IR real ,
.IR string ,
etc).
A
.I numeric
value is either
.I int
or
.IR real .
The other operations are:
.TP
.IB v .copy()
Return a (reference to) a copy of value
.IR v .
The copying is not recursive.
.TP
.IB v .eq( w )
Return true if the values of
.I v
and
.I w
are equal, including the values of corresponding subcomponents, recursively
.TP
.IB v .get( mem )
Return the value associated with the member named
.I mem
in
.IR v ;
return null if there is none.
.TP
.IB v .set( mem,\ value )
Adds or changes the
.I value
associated with the member named
.I mem
in
.IR v ,
which must be a
.BR JValue.Object (
raises exception
.B "json: set non-object"
otherwise).
.TP
.IB v .text()
Return a string containing the JSON representation of
.IR v .
.SH SOURCE
.B /appl/lib/json.b
.SH SEE ALSO
.IR sexprs (2),
.IR ubfa (2) ,
.IR json (6),
.IR sexprs (6),
.IR ubfa (6)
.br
D Crockford, ``The application/json Media Type for JavaScript Object Notation (JSON)'',
.IR RFC4627 .