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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
.\" $NetBSD: libmj.3,v 1.7 2014/02/17 07:23:18 agc Exp $
.\"
.\" Copyright (c) 2010 Alistair Crooks <agc@NetBSD.org>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd February 16, 2014
.Dt LIBMJ 3
.Os
.Sh NAME
.Nm libmj
.Nd minimalist JSON lightweight data interchange library
.Sh LIBRARY
.Lb libmj
.Sh SYNOPSIS
.In mj.h
.Ft int
.Fo mj_create
.Fa "mj_t *atom" "const char *text" "..."
.Fc
.Ft int
.Fo mj_parse
.Fa "mj_t *atom" "const char *text" "int *tokfrom" "int *tokto" "int *toktype"
.Fc
.Ft int
.Fo mj_append
.Fa "mj_t *atom" "const char *text" "..."
.Fc
.Ft int
.Fo mj_append_field
.Fa "mj_t *atom" "const char *fieldname" "const char *text" "..."
.Fc
.Ft int
.Fo mj_deepcopy
.Fa "mj_t *dest" "mj_t *src"
.Fc
.Ft void
.Fo mj_delete
.Fa "mj_t *atom"
.Fc
.Pp
Access to objects and array entries is made using the following functions:
.Ft int
.Fo mj_arraycount
.Fa "mj_t *atom"
.Fc
.Ft int
.Fo mj_object_find
.Fa "mj_t *atom" "const char *name" "const unsigned startpoint"
.Fa "const unsigned incr"
.Fc
.Ft mj_t *
.Fo mj_get_atom
.Fa "mj_t *atom" "..."
.Fc
.Pp
JSON object output functions:
.Ft int
.Fo mj_snprint
.Fa "char *buffer" "size_t size" "mj_t *atom"
.Fc
.Ft int
.Fo mj_asprint
.Fa "char **buffer" "mj_t *atom"
.Fc
.Ft int
.Fo mj_string_size
.Fa "mj_t *atom"
.Fc
.Ft int
.Fo mj_pretty
.Fa "mj_t *atom" "void *stream" "unsigned depth" "const char *trailer"
.Fc
.Ft const char *
.Fo mj_string_rep
.Fa "mj_t *atom"
.Fc
.Sh DESCRIPTION
.Nm
is a small library interface to allow JSON text to be created and parsed.
JSON is the Java Script Object Notation,
a lightweight data-interchange format, standardised by the ECMA.
The library name
.Nm
is derived from a further acronym of
.Dq minimalist JSON .
.\" Hey, Mary!
.Pp
The
.Nm
library can be used to create a string in memory which contains a textual
representation of a number of objects, arbitrarily structured.
The library can also be used to reconstruct the structure.
Data can thus be serialised easily and efficiently, and data structures
rebuilt to produce the original structure of the data.
.Pp
JSON contains basic units called atoms, the two
basic atoms being strings and numbers.
Three other useful atomic values are provided:
.Dq null ,
.Dq false ,
and
.Dq true .
Atoms can be grouped together as key/value pairs in an
.Dq object ,
and as individual, ordered atoms, in an
.Dq array .
.Pp
To create a new object, the
.Fn mj_create
function is used.
It can be deleted using the
.Fn mj_delete
function.
.Pp
Atoms, objects and arrays can be appended
to arrays and objects using the
.Fn mj_append
function.
.Pp
Objects can be printed out
by using the
.Fn mj_snprint
function.
The size of a string of JSON text can be calculated
using the
.Fn mj_string_size
function.
A utility function
.Fn mj_asprint
is provided which will allocate space dynamically,
using
.Xr calloc 3 ,
and the JSON serialised text is copied into it.
This memory can later be de-allocated using
.Xr free 3 .
For formatted output to a
.Vt FILE *
stream, the
.Fn mj_pretty
function is used.
The calling interface gives the ability to indent the
output to a given
.Fa depth
and for the formatted output to be followed by a
.Fa trailer
string, which is usually
.Dv NULL
for external calls, but can be any valid string.
Output is sent to the
.Fa stream
file stream.
.Pp
The
.Fa type
argument given to the
.Fn mj_create ,
.Fn mj_append ,
and
.Fn mj_append_field
functions is taken from a list of
.Dq false
.Dq true
.Dq null
.Dq number
.Dq integer
.Dq string
.Dq array
and
.Dq object
types.
An integer differs from a number in that it cannot take on
any floating point values.
It is implemented internally using a signed 64-bit integer type.
This restriction of values for an integer type may be removed at a later date.
.Pp
Within a JSON object, the key values can be iterated over using an integer
index to access the individual JSON objects.
The index can also be found using the
.Fn mj_object_find
function.
.Pp
The way objects arrays are implemented in
.Nm
is by using varying-sized arrays internally.
Objects have the field name as the even entry in this internal array,
with the value being the odd entry.
Arrays are implemented as a simple array.
Thus, to find an object in an array using
.Fn mj_object_find ,
a value of 1 should be used as the increment value.
This means that every entry in the internal array will be examined,
and the first match after the starting point will be returned.
For objects, an incremental value of 2 should be used,
and an even start value should be specified.
.Pp
String values should be created and appended using two parameters in
the stdarg fields, that of the string itself, and its length in bytes
immediately after the string.
A value of
.Dv \-1
may be used if the string length is not known.
.Sh EXAMPLES
The following code fragment will make a JSON object
out of the string
.Dq Hello <USERNAME>\en
in the
buffer called
.Va buf
where
.Dq USERNAME
is the name of the user taken from the runtime environment.
The encoded text will be in an allocated buffer called
.Va s
.Bd -literal -offset indent
mj_t atom;
char buf[BUFSIZ];
char *s;
int cc;
(void) memset(\*[Am]atom, 0x0, sizeof(atom));
cc = snprintf(buf, sizeof(buf), "Hello %s\en", getenv("USER"));
mj_create(\*[Am]atom, "string", buf, cc);
cc = mj_asprint(\*[Am]s, \*[Am]atom, MJ_JSON_ENCODE);
.Ed
.Pp
Next, the following example will take the (binary) text which has been encoded into
JSON and is in the buffer
.Va buf ,
such as in the previous example, and re-create the original text:
.Bd -literal -offset indent
int from, to, tok, cc;
char *s;
mj_t atom;
(void) memset(\*[Am]atom, 0x0, sizeof(atom));
from = to = tok = 0;
mj_parse(\*[Am]atom, buf, \*[Am]from, \*[Am]to, \*[Am]tok);
cc = mj_asprint(\*[Am]s, \*[Am]atom, MJ_HUMAN);
printf("%.*s", cc, s);
.Ed
.Pp
The
.Va s
pointer points to allocated storage with the original NUL-terminated string
in it.
.Sh SEE ALSO
.Xr calloc 3 ,
.Xr free 3
.Rs
.%Q Ecma International
.%D December 2009
.%T ECMA-262: ECMAScript Language Specification
.%U http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf
.%O 5th Edition
.Re
.Sh HISTORY
The
.Nm
library first appeared in
.Nx 6.0 .
.Sh AUTHORS
.An Alistair Crooks Aq Mt agc@NetBSD.org
wrote this implementation and manual page.
|