JSON library based on JSMN lib

Dependents:   ATT_WNCInterface_Info WNCInterface_HTTP_example NerfUS-Coord Mbed_Prototype_copy_4_INNO_day_15_6_2017 ... more

C++ JSON wrapper over JSMN lib (https://github.com/zserge/jsmn).

This C++ Class is a set of common tools/procedures as a C++ wrapper over JSMN JSON parser library. It is intended to provide the boiler-plate code, with intentions to reduce code clutter, in more of C++ fashion.

In contrast to original library, Json is intended to work strictly with valid JSON structures. Non-standard JSON structures should result in an error.

This class works explicitly on the indices returned by underlying JSMN library. In the scope of this class, its function parameters, return types, and documentation, the term 'index' will always mean the index of JSMN tokens, parsed by the Json constructor, unless and until explicitly mentioned otherwise.

Committer:
faheem_chaudhary
Date:
Tue Aug 16 22:26:36 2016 +0000
Revision:
7:8aa4d0e98eb0
Parent:
5:dd98cf00ed9b
Added a very basic example in documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
faheem_chaudhary 5:dd98cf00ed9b 1 /* Author: Faheem Inayat
faheem_chaudhary 5:dd98cf00ed9b 2 * Created by "Night Crue" Team @ TechShop San Jose, CA
faheem_chaudhary 5:dd98cf00ed9b 3 *
faheem_chaudhary 5:dd98cf00ed9b 4 * --- DISCLAIMER ---
faheem_chaudhary 5:dd98cf00ed9b 5 * This code is a modified version of original JSMN lirary, written by
faheem_chaudhary 5:dd98cf00ed9b 6 * *** Serge A. Zaitsev ***
faheem_chaudhary 5:dd98cf00ed9b 7 * and hosted at https://github.com/zserge/jsmn
faheem_chaudhary 5:dd98cf00ed9b 8 * Any modification to the original source is not guaranteed to be included
faheem_chaudhary 5:dd98cf00ed9b 9 * in this version. As of writing of this file, the original source is
faheem_chaudhary 5:dd98cf00ed9b 10 * licensed under MIT License
faheem_chaudhary 5:dd98cf00ed9b 11 * (http://www.opensource.org/licenses/mit-license.php).
faheem_chaudhary 5:dd98cf00ed9b 12 */
faheem_chaudhary 5:dd98cf00ed9b 13
mercurywaters 3:fab591fca1e7 14 #ifndef __JSMN_H_
mercurywaters 3:fab591fca1e7 15 #define __JSMN_H_
mercurywaters 3:fab591fca1e7 16
mercurywaters 3:fab591fca1e7 17 #include <stddef.h>
mercurywaters 3:fab591fca1e7 18
mercurywaters 3:fab591fca1e7 19 #ifdef __cplusplus
mercurywaters 3:fab591fca1e7 20 extern "C"
mercurywaters 3:fab591fca1e7 21 {
mercurywaters 3:fab591fca1e7 22 #endif
faheem_chaudhary 5:dd98cf00ed9b 23 /*
faheem_chaudhary 5:dd98cf00ed9b 24 Modified version of original jsmn lib ... added a type "JSMN_KEY" and enabled
faheem_chaudhary 5:dd98cf00ed9b 25 parent-pointers and strict JSON check.
faheem_chaudhary 5:dd98cf00ed9b 26 */
mercurywaters 3:fab591fca1e7 27 /**
mercurywaters 3:fab591fca1e7 28 * JSON type identifier. Basic types are:
mercurywaters 3:fab591fca1e7 29 * o Object
mercurywaters 3:fab591fca1e7 30 * o Array
mercurywaters 3:fab591fca1e7 31 * o String
mercurywaters 3:fab591fca1e7 32 * o Other primitive: number, boolean (true/false) or null
mercurywaters 3:fab591fca1e7 33 */
mercurywaters 3:fab591fca1e7 34 typedef enum
mercurywaters 3:fab591fca1e7 35 {
mercurywaters 3:fab591fca1e7 36 JSMN_UNDEFINED = 0,
mercurywaters 3:fab591fca1e7 37 JSMN_OBJECT = 1,
mercurywaters 3:fab591fca1e7 38 JSMN_ARRAY = 2,
mercurywaters 3:fab591fca1e7 39 JSMN_STRING = 3,
mercurywaters 3:fab591fca1e7 40 JSMN_PRIMITIVE = 4,
mercurywaters 3:fab591fca1e7 41 JSMN_KEY = 5
mercurywaters 3:fab591fca1e7 42 } jsmntype_t;
mercurywaters 3:fab591fca1e7 43
mercurywaters 3:fab591fca1e7 44 enum jsmnerr
mercurywaters 3:fab591fca1e7 45 {
mercurywaters 3:fab591fca1e7 46 /* Not enough tokens were provided */
mercurywaters 3:fab591fca1e7 47 JSMN_ERROR_NOMEM = -1,
mercurywaters 3:fab591fca1e7 48 /* Invalid character inside JSON string */
mercurywaters 3:fab591fca1e7 49 JSMN_ERROR_INVAL = -2,
mercurywaters 3:fab591fca1e7 50 /* The string is not a full JSON packet, more bytes expected */
mercurywaters 3:fab591fca1e7 51 JSMN_ERROR_PART = -3
mercurywaters 3:fab591fca1e7 52 };
mercurywaters 3:fab591fca1e7 53
mercurywaters 3:fab591fca1e7 54 /**
mercurywaters 3:fab591fca1e7 55 * JSON token description.
mercurywaters 3:fab591fca1e7 56 * @param type type (object, array, string etc.)
mercurywaters 3:fab591fca1e7 57 * @param start start position in JSON data string
mercurywaters 3:fab591fca1e7 58 * @param end end position in JSON data string
mercurywaters 3:fab591fca1e7 59 */
mercurywaters 3:fab591fca1e7 60 typedef struct
mercurywaters 3:fab591fca1e7 61 {
mercurywaters 3:fab591fca1e7 62 jsmntype_t type;
mercurywaters 3:fab591fca1e7 63 int start;
mercurywaters 3:fab591fca1e7 64 int end;
mercurywaters 3:fab591fca1e7 65 int parent;
mercurywaters 3:fab591fca1e7 66 int childCount;
mercurywaters 3:fab591fca1e7 67 } jsmntok_t;
mercurywaters 3:fab591fca1e7 68
mercurywaters 3:fab591fca1e7 69 /**
mercurywaters 3:fab591fca1e7 70 * JSON parser. Contains an array of token blocks available. Also stores
mercurywaters 3:fab591fca1e7 71 * the string being parsed now and current position in that string
mercurywaters 3:fab591fca1e7 72 */
mercurywaters 3:fab591fca1e7 73 typedef struct
mercurywaters 3:fab591fca1e7 74 {
mercurywaters 3:fab591fca1e7 75 unsigned int pos; /* offset in the JSON string */
mercurywaters 3:fab591fca1e7 76 unsigned int toknext; /* next token to allocate */
mercurywaters 3:fab591fca1e7 77 int toksuper; /* superior token node, e.g parent object or array */
mercurywaters 3:fab591fca1e7 78 } jsmn_parser;
mercurywaters 3:fab591fca1e7 79
mercurywaters 3:fab591fca1e7 80 /**
mercurywaters 3:fab591fca1e7 81 * Create JSON parser over an array of tokens
mercurywaters 3:fab591fca1e7 82 */
mercurywaters 3:fab591fca1e7 83 void jsmn_init ( jsmn_parser *parser );
mercurywaters 3:fab591fca1e7 84
mercurywaters 3:fab591fca1e7 85 /**
mercurywaters 3:fab591fca1e7 86 * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
mercurywaters 3:fab591fca1e7 87 * a single JSON object.
mercurywaters 3:fab591fca1e7 88 */
mercurywaters 3:fab591fca1e7 89 int jsmn_parse ( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens );
mercurywaters 3:fab591fca1e7 90
mercurywaters 3:fab591fca1e7 91 #ifdef __cplusplus
mercurywaters 3:fab591fca1e7 92 }
mercurywaters 3:fab591fca1e7 93 #endif
mercurywaters 3:fab591fca1e7 94
mercurywaters 3:fab591fca1e7 95 #endif /* __JSMN_H_ */
mercurywaters 3:fab591fca1e7 96