]> git.lizzy.rs Git - plan9front.git/blob - sys/man/2/json
scribble(2): quashed false claims of quick-reference decadance.
[plan9front.git] / sys / man / 2 / json
1 .TH JSON 2
2 .SH NAME
3 jsonparse,
4 jsonfree,
5 jsonbyname,
6 jsonstr
7 \- JSON parser
8 .SH SYNOPSIS
9 .\" .ta 0.75i 1.5i 2.25i 3i 3.75i 4.5i
10 .ta 0.7i +0.7i +0.7i +0.7i +0.7i +0.7i +0.7i
11 .EX
12 #include <u.h>
13 #include <libc.h>
14 #include <json.h>
15
16 enum {
17         JSONNull,
18         JSONBool,
19         JSONNumber,
20         JSONString,
21         JSONArray,
22         JSONObject,
23 };
24
25 typedef struct JSONEl JSONEl;
26 struct JSONEl {
27         char *name;
28         JSON *val;
29         JSONEl *next;
30 };
31
32 typedef struct JSON JSON;
33 struct JSON
34 {
35         int t;
36         union {
37                 double n;
38                 char *s;
39                 JSONEl *first;
40         };
41 };
42
43 JSON*   jsonparse(char *);
44 void    jsonfree(JSON *);
45 JSON*   jsonbyname(JSON *, char *);
46 char*   jsonstr(JSON *);
47 .EE
48 .SH DESCRIPTION
49 The
50 .B JSON
51 structure represents a variant json value. The variant type
52 is stored in the
53 .I t
54 member of the structure. String values use
55 .BR s ,
56 booleans and numbers use the
57 .B n
58 members in the structure.
59 Arrays and objects (dictionaries) are represented by
60 a singly-linked list of
61 .B JSONEl
62 structures referred to from the
63 .B first
64 pointer in the
65 .B JSON
66 structure.
67 Each
68 .B JSONEl
69 has a
70 .B val
71 pointer to the associated value and a
72 .B next
73 pointer to the next element in the array or object.
74 Dictionary objects have the
75 .B name
76 member set to the key of the association.
77 .P
78 A json object is parsed by calling
79 .B jsonparse
80 with a
81 .B UTF-8
82 string of the json encoded data. On success, a non-nil pointer to a
83 newly allocated
84 .B JSON
85 structure is returned.
86 To free the parsed objects,
87 .B jsonfree
88 has to be called.
89 .P
90 The
91 .B jsonbyname
92 function returns the associated value of a dictionary item.
93 .P
94 The function
95 .B jsonstr
96 returns the string value of a json object or
97 .B nil
98 for any other object type.
99 .SH SOURCE
100 .B /sys/src/libjson
101 .SH DIAGNOSTICS
102 The functions
103 .IB jsonparse ,
104 .B jsonbyname
105 and
106 .B jsonstr
107 return
108 .B nil
109 on error and set an error string (see
110 .IR errstr (2)).