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 02 20:21:04 2016 +0000
Revision:
4:ae34010d87e5
Parent:
3:fab591fca1e7
Child:
5:dd98cf00ed9b
Added Key search in flat format

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mercurywaters 3:fab591fca1e7 1 #ifndef __JSON_LIB_CLASS_H_
mercurywaters 3:fab591fca1e7 2 #define __JSON_LIB_CLASS_H_
mercurywaters 3:fab591fca1e7 3
mercurywaters 3:fab591fca1e7 4 #include "jsmn.h"
mercurywaters 3:fab591fca1e7 5 #include <stdlib.h>
mercurywaters 3:fab591fca1e7 6 #include <string.h>
mercurywaters 3:fab591fca1e7 7
mercurywaters 3:fab591fca1e7 8 /**
mercurywaters 3:fab591fca1e7 9 * JSON wrapper over JSMN lib
mercurywaters 3:fab591fca1e7 10 */
mercurywaters 3:fab591fca1e7 11
mercurywaters 3:fab591fca1e7 12 class Json
mercurywaters 3:fab591fca1e7 13 {
mercurywaters 3:fab591fca1e7 14 private:
mercurywaters 3:fab591fca1e7 15 const char * source;
mercurywaters 3:fab591fca1e7 16 const size_t sourceLength;
mercurywaters 3:fab591fca1e7 17 jsmntok_t * tokens;
mercurywaters 3:fab591fca1e7 18 int tokenCount;
mercurywaters 3:fab591fca1e7 19 Json ( const Json & other );
mercurywaters 3:fab591fca1e7 20
mercurywaters 3:fab591fca1e7 21 public:
mercurywaters 3:fab591fca1e7 22 /** The only constructor allowed. As JSON object will create/allocate
mercurywaters 3:fab591fca1e7 23 * memory for it's working, in favor of small memory fottprints, it
mercurywaters 3:fab591fca1e7 24 * is not allowed to be passed-by-value. So there is no copy or
mercurywaters 3:fab591fca1e7 25 * default constructor
mercurywaters 3:fab591fca1e7 26 * @param jsonString - char string containing JSON data
mercurywaters 3:fab591fca1e7 27 * @param length - length of the jsonString
mercurywaters 3:fab591fca1e7 28 */
mercurywaters 3:fab591fca1e7 29 Json ( const char * jsonString, size_t length );
mercurywaters 3:fab591fca1e7 30 virtual ~Json ();
mercurywaters 3:fab591fca1e7 31
faheem_chaudhary 4:ae34010d87e5 32 int findKeyIndex ( const char * key, const int &startingAt ) const;
mercurywaters 3:fab591fca1e7 33 int findKeyIndexIn ( const char * key, const int &parentIndex ) const;
mercurywaters 3:fab591fca1e7 34 int findChildIndexOf ( const int &parentIndex, const int &startingAt ) const;
mercurywaters 3:fab591fca1e7 35 bool matches ( const int & tokenIndex, const char * value ) const;
mercurywaters 3:fab591fca1e7 36
mercurywaters 3:fab591fca1e7 37 inline bool isValidJson () const;
mercurywaters 3:fab591fca1e7 38 inline jsmntype_t type ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 39 inline int parent ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 40 inline int childCount ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 41 inline int tokenLength ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 42 inline const char * tokenAddress ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 43
mercurywaters 3:fab591fca1e7 44 int tokenIntegerValue ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 45 float tokenNumberValue ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 46
mercurywaters 3:fab591fca1e7 47 inline bool tokenBooleanValue ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 48
mercurywaters 3:fab591fca1e7 49 // void print () const;
faheem_chaudhary 4:ae34010d87e5 50
faheem_chaudhary 4:ae34010d87e5 51 static char * unescape ( char * jsonString );
mercurywaters 3:fab591fca1e7 52 };
mercurywaters 3:fab591fca1e7 53
mercurywaters 3:fab591fca1e7 54 inline bool Json::isValidJson () const
mercurywaters 3:fab591fca1e7 55 {
mercurywaters 3:fab591fca1e7 56 return ( tokenCount >= 1 );
mercurywaters 3:fab591fca1e7 57 }
mercurywaters 3:fab591fca1e7 58
mercurywaters 3:fab591fca1e7 59 inline jsmntype_t Json::type ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 60 {
mercurywaters 3:fab591fca1e7 61 return tokens [ tokenIndex ].type;
mercurywaters 3:fab591fca1e7 62 }
mercurywaters 3:fab591fca1e7 63
mercurywaters 3:fab591fca1e7 64 inline int Json::parent ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 65 {
mercurywaters 3:fab591fca1e7 66 return tokens [ tokenIndex ].parent;
mercurywaters 3:fab591fca1e7 67 }
mercurywaters 3:fab591fca1e7 68
mercurywaters 3:fab591fca1e7 69 inline int Json::childCount ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 70 {
mercurywaters 3:fab591fca1e7 71 return tokens [ tokenIndex ].childCount;
mercurywaters 3:fab591fca1e7 72 }
mercurywaters 3:fab591fca1e7 73
mercurywaters 3:fab591fca1e7 74 inline int Json::tokenLength ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 75 {
mercurywaters 3:fab591fca1e7 76 return tokens [ tokenIndex ].end - tokens [ tokenIndex ].start;
mercurywaters 3:fab591fca1e7 77 }
mercurywaters 3:fab591fca1e7 78
mercurywaters 3:fab591fca1e7 79 inline const char * Json::tokenAddress ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 80 {
mercurywaters 3:fab591fca1e7 81 return source + tokens [ tokenIndex ].start;
mercurywaters 3:fab591fca1e7 82 }
mercurywaters 3:fab591fca1e7 83
faheem_chaudhary 4:ae34010d87e5 84 inline bool Json::tokenBooleanValue ( const int tokenIndex ) const
faheem_chaudhary 4:ae34010d87e5 85 {
faheem_chaudhary 4:ae34010d87e5 86 if ( type ( tokenIndex ) == JSMN_PRIMITIVE )
faheem_chaudhary 4:ae34010d87e5 87 {
faheem_chaudhary 4:ae34010d87e5 88 return matches ( tokenIndex, "true" );
faheem_chaudhary 4:ae34010d87e5 89 }
faheem_chaudhary 4:ae34010d87e5 90 return false;
faheem_chaudhary 4:ae34010d87e5 91 }
faheem_chaudhary 4:ae34010d87e5 92
mercurywaters 3:fab591fca1e7 93 #endif
mercurywaters 3:fab591fca1e7 94