json.3 (3380B)
1 .TH JSON 3 2 .SH NAME 3 json_init, json_next, json_close \- JSON parsing library 4 .SH SYNOPSIS 5 .B #include <u.h> 6 .br 7 .B #include <libc.h> 8 .br 9 .B #include <bio.h> 10 .br 11 .B #include <str.h> 12 .br 13 .B #include <vec.h> 14 .br 15 .B #include <json.h> 16 .PP 17 .EX 18 .ta 6n +\w'JsonType 'u +\w'JStr = 1 << 1, 'u 19 typedef enum { 20 JNone, 21 JNum, 22 JStr = 1 << 1, 23 JObj = 1 << 2, 24 JErr = 1 << 3 25 } JsonType; 26 27 typedef union { 28 String s; 29 double n; 30 char c; 31 } JsonVal; 32 33 typedef struct { 34 String k; 35 JsonVal v; 36 JsonType t; 37 } JsonObj; 38 39 typedef struct { 40 ulong st[8]; /* obj stack */ 41 JsonObj *o; /* obj vector */ 42 JsonObj *cur; /* cur obj */ 43 ulong n; /* stack number */ 44 ... 45 } Json; 46 .EE 47 .PP 48 .ta \w'\fLJsonType 'u 49 .B 50 int json_init(Json *j, char *file) 51 .PP 52 .B 53 int json_next(Json *j, JsonType t) 54 .PP 55 .B 56 void json_close(Json *j) 57 .SH DESCRIPTION 58 These functions provide a simple streaming JSON parser. 59 .PP 60 .I Json_init 61 initializes parser 62 .I j 63 to read from 64 .IR file . 65 If 66 .I file 67 is 68 .BR nil , 69 input is read from standard input. 70 Returns 1 on success, or 0 if the file cannot be opened. 71 .PP 72 .I Json_next 73 returns a positive 74 .I JsonType 75 value if a JSON element matching filter 76 .I t 77 if found, or 0 if no more matching elements exist, or the parser is in an error state. 78 The filter uses bitwise OR to combine types, or 0 to accept any type. 79 If an element doesn't match the filter, it is skipped and the next element is processed. 80 .PP 81 The current element is stored in 82 .IB cur 83 with type 84 .IB t , 85 key 86 .IB k , 87 and value 88 .IB v . 89 For containers, the character '{' or '[' is stored in 90 .IB v.c 91 to respectively differentiate between a object or array. 92 .PP 93 .I Json_close 94 frees all memory associated with parser 95 .IR j . 96 .SS Types 97 .TP 98 .B JNone 99 No value or end of input 100 .TP 101 .B JNum 102 Numeric value stored in 103 .IB v.n 104 .TP 105 .B JStr 106 String value stored in 107 .IB v.s 108 .TP 109 .B JObj 110 Object or array start, character stored in 111 .IB v.c 112 .TP 113 .B JErr 114 Parse error, message in 115 .IB k , 116 line number in 117 .IB v.n 118 .SH EXAMPLES 119 Parse a JSON file and print all key-value pairs: 120 .IP 121 .EX 122 Json j; 123 JsonObj *o; 124 125 if(!json_init(&j, nil)) 126 sysfatal("open(): %r"); 127 while(json_next(&j, 0)){ 128 o = j.cur; 129 if(o->t & JObj) 130 continue; 131 if(o->k.s) 132 print("%s = ", o->k.s); 133 if(o->t == JStr) 134 print("%s\\n", o->v.s.s); 135 else 136 print("%g\\n", o->v.n); 137 } 138 if(j.cur->t == JErr) 139 fprint(2, "%s at line %d", o->k.s, (int)o->v.n); 140 json_close(&j); 141 .EE 142 .PP 143 Count key-value pairs in a JSON file: 144 .IP 145 .EX 146 Json j; 147 JsonObj *o; 148 int n; 149 150 json_init(&j, nil); 151 for(n = 0; json_next(&j, 0);) 152 if (j.cur->t & (JStr|JNum)) 153 ++n; 154 o = j.cur; 155 if(o->t == JErr) 156 sysfatal("%s at line %d", o->k.s, (int)o->v.n); 157 print("%d pairs\\n", n); 158 json_close(&j); 159 .EE 160 .SH SOURCE 161 /src/libjson 162 .SH SEE ALSO 163 .IR str (3), 164 .IR vec (3), 165 .IR sdb (7) 166 .SH DIAGNOSTICS 167 .I Json_init 168 returns 0 if the file cannot be opened. 169 Parse errors are returned as 170 .B JErr 171 elements with error messages stored in 172 .IB k 173 and line numbers stored in 174 .IB v.n . 175 .SH NOTES 176 String escape sequences are processed minimally, handling only 177 quote and backslash characters. 178 Non-JSON tokens are parsed as literal strings. 179 .SH BUGS 180 The parser does not validate JSON structure completely. 181 Malformed input may cause unexpected results.