jsmn.h revision 275970
1117395Skan#ifndef __JSMN_H_ 2169689Skan#define __JSMN_H_ 3117395Skan 4117395Skan/** 5132718Skan * JSON type identifier. Basic types are: 6117395Skan * o Object 7132718Skan * o Array 8132718Skan * o String 9132718Skan * o Other primitive: number, boolean (true/false) or null 10132718Skan */ 11117395Skantypedef enum { 12132718Skan JSMN_PRIMITIVE = 0, 13132718Skan JSMN_OBJECT = 1, 14132718Skan JSMN_ARRAY = 2, 15132718Skan JSMN_STRING = 3 16117395Skan} jsmntype_t; 17117395Skan 18132718Skantypedef enum { 19169689Skan /* Not enough tokens were provided */ 20169689Skan JSMN_ERROR_NOMEM = -1, 21117395Skan /* Invalid character inside JSON string */ 22132718Skan JSMN_ERROR_INVAL = -2, 23169689Skan /* The string is not a full JSON packet, more bytes expected */ 24169689Skan JSMN_ERROR_PART = -3, 25169689Skan /* Everything was fine */ 26169689Skan JSMN_SUCCESS = 0 27169689Skan} jsmnerr_t; 28169689Skan 29169689Skan/** 30169689Skan * JSON token description. 31169689Skan * @param type type (object, array, string etc.) 32169689Skan * @param start start position in JSON data string 33169689Skan * @param end end position in JSON data string 34169689Skan */ 35169689Skantypedef struct { 36169689Skan jsmntype_t type; 37169689Skan int start; 38169689Skan int end; 39169689Skan int size; 40169689Skan#ifdef JSMN_PARENT_LINKS 41169689Skan int parent; 42169689Skan#endif 43169689Skan} jsmntok_t; 44169689Skan 45169689Skan/** 46169689Skan * JSON parser. Contains an array of token blocks available. Also stores 47169689Skan * the string being parsed now and current position in that string 48169689Skan */ 49169689Skantypedef struct { 50169689Skan unsigned int pos; /* offset in the JSON string */ 51169689Skan int toknext; /* next token to allocate */ 52169689Skan int toksuper; /* superior token node, e.g parent object or array */ 53169689Skan} jsmn_parser; 54169689Skan 55169689Skan/** 56169689Skan * Create JSON parser over an array of tokens 57169689Skan */ 58169689Skanvoid jsmn_init(jsmn_parser *parser); 59169689Skan 60169689Skan/** 61169689Skan * Run JSON parser. It parses a JSON data string into and array of tokens, each describing 62169689Skan * a single JSON object. 63169689Skan */ 64169689Skanjsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, 65169689Skan jsmntok_t *tokens, unsigned int num_tokens); 66169689Skan 67169689Skan#endif /* __JSMN_H_ */ 68169689Skan