2013-12-19 18:56:04 +00:00
|
|
|
.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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-12-22 17:12:56 +00:00
|
|
|
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);
|
2013-12-19 18:56:04 +00:00
|
|
|
.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
|
2013-12-19 19:59:50 +00:00
|
|
|
structures referred to from the
|
2013-12-19 18:56:04 +00:00
|
|
|
.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
|
2015-12-22 17:12:56 +00:00
|
|
|
.I jsonparse
|
2013-12-19 18:56:04 +00:00
|
|
|
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,
|
2015-12-22 17:12:56 +00:00
|
|
|
.I jsonfree
|
2013-12-19 18:56:04 +00:00
|
|
|
has to be called.
|
|
|
|
.P
|
|
|
|
The
|
2015-12-22 17:12:56 +00:00
|
|
|
.I jsonbyname
|
2013-12-19 18:56:04 +00:00
|
|
|
function returns the associated value of a dictionary item.
|
|
|
|
.P
|
|
|
|
The function
|
2015-12-22 17:12:56 +00:00
|
|
|
.I jsonstr
|
2013-12-19 18:56:04 +00:00
|
|
|
returns the string value of a json object or
|
|
|
|
.B nil
|
|
|
|
for any other object type.
|
2015-12-22 17:12:56 +00:00
|
|
|
.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.
|
2013-12-25 19:09:00 +00:00
|
|
|
.SH SOURCE
|
|
|
|
.B /sys/src/libjson
|
2013-12-19 18:56:04 +00:00
|
|
|
.SH DIAGNOSTICS
|
|
|
|
The functions
|
2015-12-22 17:12:56 +00:00
|
|
|
.I jsonparse,
|
|
|
|
.I jsonbyname
|
2013-12-19 18:56:04 +00:00
|
|
|
and
|
2015-12-22 17:12:56 +00:00
|
|
|
.I jsonstr
|
2013-12-19 18:56:04 +00:00
|
|
|
return
|
|
|
|
.B nil
|
|
|
|
on error and set an error string (see
|
|
|
|
.IR errstr (2)).
|