Justin BedoInitial implementation06a376f14 years agoHistory
190 lines · 3.2 KB · Text
RawDownload
.TH JSON 2.SH NAMEJinit, Jtokenise, Jfind, Jnext, Jtokstr \- JSON parser.SH SYNOPSIS.B #include <u.h>.br.B #include <libc.h>.br.B #include <json.h>.PP.nf.ft Ltypedef enum{	JBool,	JNum,	JNil,	JObj,	JArr,	JStr,} Jtype;.fi.PP.nf.ft Ltypedef struct{	Jtype type;	char *start;	char *end;	uint nsub;} Jtok;.fi.PP.nf.ft Ltypedef struct{	uint ntok, mtok;	Jtok *tokens;	uint stktop;	uint tokstk[JStksz];} Jparser;.fi.PP.Bvoid Jinit(Jparser *p).PP.Bvoid Jterm(Jparser *p);.PP.Bint Jtokenise(Jparser *p, char *s).PP.Bchar *Jtokstr(Jtok *t).PP.Bint Jfind(Jparser *p, uint i, char *s).PP.Buint Jnext(Jparser *p, uint i).PP.SH DESCRIPTIONThese routines implement a fast, in-memory JSON parser.  The parseroperates on a string which is expected to contain the full JSON text.The string is not altered in any way..PPA parser object.I pis initialised using.IR Jinit .The parser object contains the number of tokens found.I ntokand an array of tokens.IR tokens .The other fields are for internal use.  As memory is dynamicallyallocated, an initialised parser.I pmust be terminated with.I Jtermafter use..PPAn initialised parser object is populated with tokens using.IR Jtokenise ..I Jtokeniseaccepts parser.I pand a string.I sand returns a filled.I Jparserstructure containing tokens found in.IR s ..I Sis unaltered..PPEach token.I Jtokenhas a type.IR Jtype ,pointers to the.I startand .I endof the token's location in the JSON string,and a count.I nsubof the number of subtokens.  The types of a token are.TP.I JBoolBoolean value.  Token must be either 't' or 'f'..TP.I JNumA real number..TP.I JNilNill token matching 'n'..TP.I JObjAn object (dictionary) containing key:value pairs..TP.I JArrAn array..TP.I JStrA string..PPThe pointers.I startand.I endpoint to the beginning character of the token and to one characterafter the end of the token respectively.  The exception are.I JStrtokens, where.I startpoints to the first character after the double quote and.I endpoints to the terminating double quote.  As arrays.I JArrand objects.I JObjare the only types allowing subelements, .I nsubis necessarily 0 for .IR JBool ,.IR JNum ,.IR JNil ,and.IR JStr ..PPThe functions.IR Jtokstr ,.IR Jfindand.I Jnext are functions for working with the tokens..I Jtokstrtakes a token and extracts out the string pointed to by the token.The string is null terminated and escaped characters are handledappropriately.  As.I Jtokstruses a static buffer, the string returned is destroyed on the nextcall..PP.I Jfindis used to find specifically named attributes within a .IR JObj .Given an index into the.I tokensarray of.IR p –which must be a.I JObj– and the name of an attribute.IR s ,.I Jfindreturns the index of the token corresponding to the value matching theattribute name.  If no attribute is found then.I Jfindreturns -1..PPFinally,.I Jnexttakes and index and calculates the index of the next element at thatdepth.  It is used for skipping over.IR JObj sand.IR JArr swhich can contain a number of subelements..SH BUGSThe parser does not implement a full grammar so some JSON errors arenot detected when parsing.