summaryrefslogtreecommitdiff
path: root/sys/man/2/json
blob: d24adace38577d903ba17c63afa8c48cb5ad18f2 (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
.TH JSON 2
.SH NAME
jsonparse,
jsonfree,
jsonbyname,
jsonstr
\- JSON parser
.SH SYNOPSIS
.\" .ta 0.75i 1.5i 2.25i 3i 3.75i 4.5i
.ta 0.7i +0.7i +0.7i +0.7i +0.7i +0.7i +0.7i
.EX
#include <u.h>
#include <libc.h>
#include <json.h>

enum {
	JSONNull,
	JSONBool,
	JSONNumber,
	JSONString,
	JSONArray,
	JSONObject,
};

typedef struct JSONEl JSONEl;
struct JSONEl {
	char *name;
	JSON *val;
	JSONEl *next;
};

typedef struct JSON JSON;
struct JSON
{
	int t;
	union {
		double n;
		char *s;
		JSONEl *first;
	};
};

JSON*	jsonparse(char *s);
void	jsonfree(JSON *j);
JSON*	jsonbyname(JSON *j, char *s);
char*	jsonstr(JSON *j);
int	JSONfmt(Fmt *f)
void	JSONfmtinstall(void);
.EE
.SH DESCRIPTION
The
.B JSON
structure represents a variant json value. The variant type
is stored in the
.I t
member of the structure. String values use
.BR s ,
booleans and numbers use the
.B n
members in the structure.
Arrays and objects (dictionaries) are represented by
a singly-linked list of
.B JSONEl
structures referred to from the
.B first
pointer in the
.B JSON
structure.
Each
.B JSONEl
has a
.B val
pointer to the associated value and a
.B next
pointer to the next element in the array or object.
Dictionary objects have the
.B name
member set to the key of the association.
.P
A json object is parsed by calling
.I jsonparse
with a
.B UTF-8
string of the json encoded data. On success, a non-nil pointer to a
newly allocated
.B JSON
structure is returned.
To free the parsed objects,
.I jsonfree
has to be called.
.P
The
.I jsonbyname
function returns the associated value of a dictionary item.
.P
The function
.I jsonstr
returns the string value of a json object or
.B nil
for any other object type.
.P
.I JSONfmt
is a
.IR print (2)
formatting routine that prints a well-formatted JSON structure.
It can be installed by hand but
.I JSONfmtinstall
installs it under the standard format character J. The header
.B <json.h>
contains a #pragma statement so the compiler can
type-check uses of
.B %J
in
.IR print (2)
format strings.
.SH SOURCE
.B /sys/src/libjson
.SH DIAGNOSTICS
The functions
.I jsonparse,
.I jsonbyname
and
.I jsonstr
return
.B nil
on error and set an error string (see
.IR errstr (2)).