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:
mercurywaters
Date:
Thu Jun 02 06:08:24 2016 +0000
Revision:
3:fab591fca1e7
Child:
4:ae34010d87e5
Added the documentation of JSON class

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
mercurywaters 3:fab591fca1e7 32 int findKeyIndexIn ( const char * key, const int &parentIndex ) const;
mercurywaters 3:fab591fca1e7 33 int findChildIndexOf ( const int &parentIndex, const int &startingAt ) const;
mercurywaters 3:fab591fca1e7 34 bool matches ( const int & tokenIndex, const char * value ) const;
mercurywaters 3:fab591fca1e7 35
mercurywaters 3:fab591fca1e7 36 inline bool isValidJson () const;
mercurywaters 3:fab591fca1e7 37 inline jsmntype_t type ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 38 inline int parent ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 39 inline int childCount ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 40 inline int tokenLength ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 41 inline const char * tokenAddress ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 42
mercurywaters 3:fab591fca1e7 43 int tokenIntegerValue ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 44 float tokenNumberValue ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 45
mercurywaters 3:fab591fca1e7 46 inline bool tokenBooleanValue ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 47
mercurywaters 3:fab591fca1e7 48 // void print () const;
mercurywaters 3:fab591fca1e7 49 };
mercurywaters 3:fab591fca1e7 50
mercurywaters 3:fab591fca1e7 51 inline bool Json::isValidJson () const
mercurywaters 3:fab591fca1e7 52 {
mercurywaters 3:fab591fca1e7 53 return ( tokenCount >= 1 );
mercurywaters 3:fab591fca1e7 54 }
mercurywaters 3:fab591fca1e7 55
mercurywaters 3:fab591fca1e7 56 inline jsmntype_t Json::type ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 57 {
mercurywaters 3:fab591fca1e7 58 return tokens [ tokenIndex ].type;
mercurywaters 3:fab591fca1e7 59 }
mercurywaters 3:fab591fca1e7 60
mercurywaters 3:fab591fca1e7 61 inline int Json::parent ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 62 {
mercurywaters 3:fab591fca1e7 63 return tokens [ tokenIndex ].parent;
mercurywaters 3:fab591fca1e7 64 }
mercurywaters 3:fab591fca1e7 65
mercurywaters 3:fab591fca1e7 66 inline int Json::childCount ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 67 {
mercurywaters 3:fab591fca1e7 68 return tokens [ tokenIndex ].childCount;
mercurywaters 3:fab591fca1e7 69 }
mercurywaters 3:fab591fca1e7 70
mercurywaters 3:fab591fca1e7 71 inline int Json::tokenLength ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 72 {
mercurywaters 3:fab591fca1e7 73 return tokens [ tokenIndex ].end - tokens [ tokenIndex ].start;
mercurywaters 3:fab591fca1e7 74 }
mercurywaters 3:fab591fca1e7 75
mercurywaters 3:fab591fca1e7 76 inline const char * Json::tokenAddress ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 77 {
mercurywaters 3:fab591fca1e7 78 return source + tokens [ tokenIndex ].start;
mercurywaters 3:fab591fca1e7 79 }
mercurywaters 3:fab591fca1e7 80
mercurywaters 3:fab591fca1e7 81 #endif
mercurywaters 3:fab591fca1e7 82