json lib

Dependents:   grove_stream_jpa_sd2 grove_stream_jpa_sd2 grove_stream_jpa_sd2-2 grove_stream_jpa_sd2-3 ... more

Committer:
faheem_chaudhary
Date:
Mon Aug 15 22:52:37 2016 +0000
Revision:
6:c1d2153da4ed
Parent:
5:dd98cf00ed9b
Child:
7:8aa4d0e98eb0
Changed the documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
faheem_chaudhary 5:dd98cf00ed9b 1 /* Json.h */
faheem_chaudhary 5:dd98cf00ed9b 2 /* Original Author: Faheem Inayat
faheem_chaudhary 5:dd98cf00ed9b 3 * Created by "Night Crue" Team @ TechShop San Jose, CA
faheem_chaudhary 5:dd98cf00ed9b 4 *
faheem_chaudhary 5:dd98cf00ed9b 5 * MIT License
faheem_chaudhary 5:dd98cf00ed9b 6 *
faheem_chaudhary 5:dd98cf00ed9b 7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
faheem_chaudhary 5:dd98cf00ed9b 8 * and associated documentation files (the "Software"), to deal in the Software without restriction,
faheem_chaudhary 5:dd98cf00ed9b 9 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
faheem_chaudhary 5:dd98cf00ed9b 10 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
faheem_chaudhary 5:dd98cf00ed9b 11 * furnished to do so, subject to the following conditions:
faheem_chaudhary 5:dd98cf00ed9b 12 *
faheem_chaudhary 5:dd98cf00ed9b 13 * The above copyright notice and this permission notice shall be included in all copies or
faheem_chaudhary 5:dd98cf00ed9b 14 * substantial portions of the Software.
faheem_chaudhary 5:dd98cf00ed9b 15 *
faheem_chaudhary 5:dd98cf00ed9b 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
faheem_chaudhary 5:dd98cf00ed9b 17 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
faheem_chaudhary 5:dd98cf00ed9b 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
faheem_chaudhary 5:dd98cf00ed9b 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
faheem_chaudhary 5:dd98cf00ed9b 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
faheem_chaudhary 5:dd98cf00ed9b 21 */
faheem_chaudhary 5:dd98cf00ed9b 22
mercurywaters 3:fab591fca1e7 23 #ifndef __JSON_LIB_CLASS_H_
mercurywaters 3:fab591fca1e7 24 #define __JSON_LIB_CLASS_H_
mercurywaters 3:fab591fca1e7 25
mercurywaters 3:fab591fca1e7 26 #include "jsmn.h"
mercurywaters 3:fab591fca1e7 27 #include <stdlib.h>
mercurywaters 3:fab591fca1e7 28 #include <string.h>
mercurywaters 3:fab591fca1e7 29
mercurywaters 3:fab591fca1e7 30 /**
faheem_chaudhary 5:dd98cf00ed9b 31 * C++ JSON wrapper over JSMN lib (https://github.com/zserge/jsmn).
faheem_chaudhary 5:dd98cf00ed9b 32 *
faheem_chaudhary 5:dd98cf00ed9b 33 * This C++ Class is a set of common tools/procedures as a C++ wrapper over JSMN
faheem_chaudhary 5:dd98cf00ed9b 34 * JSON parser library. It is intended to provide the boiler-plate code, with
faheem_chaudhary 5:dd98cf00ed9b 35 * intentions to reduce code clutter, in more of C++ fashion.
faheem_chaudhary 5:dd98cf00ed9b 36 *
faheem_chaudhary 5:dd98cf00ed9b 37 * In contrast to original library, Json is intended to work strictly with valid
faheem_chaudhary 5:dd98cf00ed9b 38 * JSON structures. Non-standard JSON structures should result in an error.
faheem_chaudhary 5:dd98cf00ed9b 39 *
faheem_chaudhary 5:dd98cf00ed9b 40 * This class works explicitly on the indices returned by underlying JSMN
faheem_chaudhary 5:dd98cf00ed9b 41 * library. In the scope of this class, its function parameters, return types,
faheem_chaudhary 5:dd98cf00ed9b 42 * and documentation, the term 'index' will always mean the index of JSMN
faheem_chaudhary 5:dd98cf00ed9b 43 * tokens, parsed by the Json constructor, unless and until explicitly mentioned
faheem_chaudhary 5:dd98cf00ed9b 44 * otherwise.
mercurywaters 3:fab591fca1e7 45 */
mercurywaters 3:fab591fca1e7 46
mercurywaters 3:fab591fca1e7 47 class Json
mercurywaters 3:fab591fca1e7 48 {
mercurywaters 3:fab591fca1e7 49 private:
faheem_chaudhary 5:dd98cf00ed9b 50 const unsigned int maxTokenCount;
mercurywaters 3:fab591fca1e7 51 const char * source;
mercurywaters 3:fab591fca1e7 52 const size_t sourceLength;
mercurywaters 3:fab591fca1e7 53 jsmntok_t * tokens;
mercurywaters 3:fab591fca1e7 54 int tokenCount;
faheem_chaudhary 5:dd98cf00ed9b 55
faheem_chaudhary 5:dd98cf00ed9b 56 // Copy COntructor is intentionally kept private to enforce the caller
faheem_chaudhary 5:dd98cf00ed9b 57 // to use pointers/reference, and never pass-by-value
faheem_chaudhary 5:dd98cf00ed9b 58 Json ( const Json & );
mercurywaters 3:fab591fca1e7 59
mercurywaters 3:fab591fca1e7 60 public:
faheem_chaudhary 5:dd98cf00ed9b 61 /** The only constructor allowed.
faheem_chaudhary 5:dd98cf00ed9b 62 As JSON object will create/allocate memory for its working, in favor of
faheem_chaudhary 5:dd98cf00ed9b 63 small memory footprints, it is not allowed to be passed-by-value. So
faheem_chaudhary 5:dd98cf00ed9b 64 there is no copy- or default-constructor
faheem_chaudhary 5:dd98cf00ed9b 65
faheem_chaudhary 6:c1d2153da4ed 66 @param jsonString char string containing JSON data
faheem_chaudhary 6:c1d2153da4ed 67 @param length length of the jsonString
faheem_chaudhary 6:c1d2153da4ed 68 @param maxTokens optional maximum count of Tokens. Default is 32.
mercurywaters 3:fab591fca1e7 69 */
faheem_chaudhary 5:dd98cf00ed9b 70 Json ( const char * jsonString, size_t length, unsigned int maxTokens = 32 );
faheem_chaudhary 5:dd98cf00ed9b 71
faheem_chaudhary 5:dd98cf00ed9b 72
faheem_chaudhary 5:dd98cf00ed9b 73 /** Although there is no virtual function to this class, destructor is
faheem_chaudhary 5:dd98cf00ed9b 74 still made virtual, for just-in-case use. Destructor will delete the
faheem_chaudhary 5:dd98cf00ed9b 75 'tokens' array, created in constructor.
faheem_chaudhary 5:dd98cf00ed9b 76 */
mercurywaters 3:fab591fca1e7 77 virtual ~Json ();
mercurywaters 3:fab591fca1e7 78
faheem_chaudhary 5:dd98cf00ed9b 79
faheem_chaudhary 5:dd98cf00ed9b 80 /** findKeyIndex will find and return the token index representing the
faheem_chaudhary 5:dd98cf00ed9b 81 'Key' in underlying JSON object. It is a strictly a linear key search
faheem_chaudhary 5:dd98cf00ed9b 82 and will return the first occurrence, without the JSON node structure
faheem_chaudhary 5:dd98cf00ed9b 83 semantics. For search in a specific node, refer to #findKeyIndex
faheem_chaudhary 5:dd98cf00ed9b 84
faheem_chaudhary 5:dd98cf00ed9b 85 @param key a char string to find as a 'Key' in JSON structure.
faheem_chaudhary 5:dd98cf00ed9b 86 @param startingAt the starting token-index for 'key' search. The
faheem_chaudhary 5:dd98cf00ed9b 87 search will NOT include this index, but instead will use the
faheem_chaudhary 5:dd98cf00ed9b 88 next one as the starting point. In case, a negative value is
faheem_chaudhary 5:dd98cf00ed9b 89 passed, search will start from '0'. It's caller's
faheem_chaudhary 5:dd98cf00ed9b 90 responsibility to make sure what values they're passing.
faheem_chaudhary 5:dd98cf00ed9b 91 Default value is set to '0', as the zero-th token index in any
faheem_chaudhary 5:dd98cf00ed9b 92 valid JSON object should always be starting object brace '{'.
faheem_chaudhary 5:dd98cf00ed9b 93 So default behavior is to always find the very first occurrence
faheem_chaudhary 5:dd98cf00ed9b 94 of key as represented by 'key' parameter.
faheem_chaudhary 5:dd98cf00ed9b 95
faheem_chaudhary 5:dd98cf00ed9b 96 @return a non-zero positive integer, if a key is found in the source
faheem_chaudhary 5:dd98cf00ed9b 97 JSON. If no key is found, -1 will be returned. There should be
faheem_chaudhary 5:dd98cf00ed9b 98 no '0' value returned in any valid case.
faheem_chaudhary 5:dd98cf00ed9b 99 */
faheem_chaudhary 5:dd98cf00ed9b 100 int findKeyIndex ( const char * key, const int &startingAt = 0 ) const;
faheem_chaudhary 5:dd98cf00ed9b 101
faheem_chaudhary 5:dd98cf00ed9b 102
faheem_chaudhary 5:dd98cf00ed9b 103 /** findKeyIndexIn will find and return the token index representing the
faheem_chaudhary 5:dd98cf00ed9b 104 'Key' in underlying JSON object node. It is strictly a single-level
faheem_chaudhary 5:dd98cf00ed9b 105 key search function, and will NOT look for the key in any child JSON
faheem_chaudhary 5:dd98cf00ed9b 106 nodes (JSON Object/Array).
faheem_chaudhary 5:dd98cf00ed9b 107
faheem_chaudhary 5:dd98cf00ed9b 108 @param key a char string to find as a 'Key' in JSON structure.
faheem_chaudhary 5:dd98cf00ed9b 109 @param parentIndex the starting token-index for 'key' search. The
faheem_chaudhary 5:dd98cf00ed9b 110 search will look for the key, only under the JSON node
faheem_chaudhary 5:dd98cf00ed9b 111 represented by this parentIndex. Default value is '0', making
faheem_chaudhary 5:dd98cf00ed9b 112 the default behavior to look for only root-level keys. The
faheem_chaudhary 5:dd98cf00ed9b 113 valid value range is 0 to [parsedTokenCount()-1] both inclusive.
faheem_chaudhary 5:dd98cf00ed9b 114
faheem_chaudhary 5:dd98cf00ed9b 115 @return a non-zero positive integer, if a key is found in the source
faheem_chaudhary 5:dd98cf00ed9b 116 JSON. If no key is found, -1 will be returned. There should be
faheem_chaudhary 5:dd98cf00ed9b 117 no '0' value returned in any valid case.
faheem_chaudhary 5:dd98cf00ed9b 118 */
faheem_chaudhary 5:dd98cf00ed9b 119 int findKeyIndexIn ( const char * key, const int &parentIndex = 0 ) const;
faheem_chaudhary 5:dd98cf00ed9b 120
faheem_chaudhary 5:dd98cf00ed9b 121
faheem_chaudhary 5:dd98cf00ed9b 122 /** findChildIndexOf will find and return the token index representing
faheem_chaudhary 5:dd98cf00ed9b 123 first child a JSON node represented by parentIndex (that is either a
faheem_chaudhary 5:dd98cf00ed9b 124 Key, an Object, or an Array), and exists after the startingAt value.
faheem_chaudhary 5:dd98cf00ed9b 125 This function is particularly handy in iterating over Array Objects, or
faheem_chaudhary 5:dd98cf00ed9b 126 getting the index for JSON 'Value' of a JSON 'Key'.
faheem_chaudhary 5:dd98cf00ed9b 127
faheem_chaudhary 5:dd98cf00ed9b 128 @param parentIndex token index representing the parent node in JSON
faheem_chaudhary 5:dd98cf00ed9b 129 source. The valid value range is 0 to [parsedTokenCount()-1]
faheem_chaudhary 5:dd98cf00ed9b 130 both inclusive.
faheem_chaudhary 5:dd98cf00ed9b 131 @param startingAt describes the starting index of the nodes to search.
faheem_chaudhary 5:dd98cf00ed9b 132 In other words, if caller wants to skip some nodes, they can
faheem_chaudhary 5:dd98cf00ed9b 133 provide this value. Default value is 0, which means search for
faheem_chaudhary 5:dd98cf00ed9b 134 all nodes in the parent.
faheem_chaudhary 5:dd98cf00ed9b 135
faheem_chaudhary 5:dd98cf00ed9b 136 @return a non-zero positive integer, if the child node is found in
faheem_chaudhary 5:dd98cf00ed9b 137 source JSON. If no child is found, -1 will be returned. There
faheem_chaudhary 5:dd98cf00ed9b 138 should be no '0' value returned in any valid case.
faheem_chaudhary 5:dd98cf00ed9b 139 */
faheem_chaudhary 5:dd98cf00ed9b 140 int findChildIndexOf ( const int &parentIndex, const int &startingAt = 0 ) const;
faheem_chaudhary 5:dd98cf00ed9b 141
faheem_chaudhary 5:dd98cf00ed9b 142
faheem_chaudhary 5:dd98cf00ed9b 143 /** matches will tell if the token data (either key or value) matches
faheem_chaudhary 5:dd98cf00ed9b 144 with the value provided. This function is particularly handy in
faheem_chaudhary 5:dd98cf00ed9b 145 iterating over the keys, and finding a specific key, in an object. The
faheem_chaudhary 5:dd98cf00ed9b 146 comparison is case-sensitive. i.e. 'Apple' will NOT match with 'apple'.
faheem_chaudhary 5:dd98cf00ed9b 147
faheem_chaudhary 5:dd98cf00ed9b 148 @param tokenIndex representing the token to compare. The valid value
faheem_chaudhary 5:dd98cf00ed9b 149 range is 0 to [parsedTokenCount()-1] both inclusive.
faheem_chaudhary 5:dd98cf00ed9b 150 @param value to compare the token data with.
faheem_chaudhary 5:dd98cf00ed9b 151
faheem_chaudhary 5:dd98cf00ed9b 152 @return true if the token data matches with value. false will be
faheem_chaudhary 5:dd98cf00ed9b 153 returned either the value doesn't match OR the tokenIndex is
faheem_chaudhary 5:dd98cf00ed9b 154 not valid.
faheem_chaudhary 5:dd98cf00ed9b 155 */
mercurywaters 3:fab591fca1e7 156 bool matches ( const int & tokenIndex, const char * value ) const;
mercurywaters 3:fab591fca1e7 157
faheem_chaudhary 5:dd98cf00ed9b 158
faheem_chaudhary 5:dd98cf00ed9b 159 /** parsedTokenCount will tell how many tokens have been parsed by JSMN
faheem_chaudhary 5:dd98cf00ed9b 160 parser. It is a utility function, for token validity.
faheem_chaudhary 5:dd98cf00ed9b 161
faheem_chaudhary 5:dd98cf00ed9b 162 @return non-negative integer number of tokens parsed by JSMN library.
faheem_chaudhary 5:dd98cf00ed9b 163 Negative value may be returned in case of error, but this
faheem_chaudhary 5:dd98cf00ed9b 164 behavior is not tested, yet.
faheem_chaudhary 5:dd98cf00ed9b 165 */
faheem_chaudhary 5:dd98cf00ed9b 166 inline int parsedTokenCount () const;
faheem_chaudhary 5:dd98cf00ed9b 167
faheem_chaudhary 5:dd98cf00ed9b 168
faheem_chaudhary 5:dd98cf00ed9b 169 /** isValidJson will tell the caller if the parsed JSON was valid,
faheem_chaudhary 5:dd98cf00ed9b 170 parsed, and accepted to further work on.
faheem_chaudhary 5:dd98cf00ed9b 171
faheem_chaudhary 5:dd98cf00ed9b 172 @return true if the JSON is valid, false otherwise.
faheem_chaudhary 5:dd98cf00ed9b 173 */
mercurywaters 3:fab591fca1e7 174 inline bool isValidJson () const;
faheem_chaudhary 5:dd98cf00ed9b 175
faheem_chaudhary 5:dd98cf00ed9b 176
faheem_chaudhary 5:dd98cf00ed9b 177 /** isValidToken will tell the caller if the tokenIndex is in valid
faheem_chaudhary 5:dd98cf00ed9b 178 range. The valid value range is 0 to [parsedTokenCount()-1] both
faheem_chaudhary 5:dd98cf00ed9b 179 inclusive.
faheem_chaudhary 5:dd98cf00ed9b 180
faheem_chaudhary 5:dd98cf00ed9b 181 @param tokenIndex representing the token in the JSON source
faheem_chaudhary 5:dd98cf00ed9b 182
faheem_chaudhary 5:dd98cf00ed9b 183 @return true if the JSON is valid, false otherwise.
faheem_chaudhary 5:dd98cf00ed9b 184 */
faheem_chaudhary 5:dd98cf00ed9b 185 inline bool isValidToken ( const int tokenIndex ) const;
faheem_chaudhary 5:dd98cf00ed9b 186
faheem_chaudhary 5:dd98cf00ed9b 187
faheem_chaudhary 5:dd98cf00ed9b 188 /** type will return the JSMN type represented by the tokenIndex.
faheem_chaudhary 5:dd98cf00ed9b 189
faheem_chaudhary 5:dd98cf00ed9b 190 @param tokenIndex representing the token in the JSON source. The valid
faheem_chaudhary 5:dd98cf00ed9b 191 value range is 0 to [parsedTokenCount()-1] both inclusive.
faheem_chaudhary 5:dd98cf00ed9b 192
faheem_chaudhary 5:dd98cf00ed9b 193 @return the type represented by tokenIndex. In case of invalid
faheem_chaudhary 5:dd98cf00ed9b 194 tokenIndex, JSMN_UNDEFINED is returned.
faheem_chaudhary 5:dd98cf00ed9b 195 */
mercurywaters 3:fab591fca1e7 196 inline jsmntype_t type ( const int tokenIndex ) const;
faheem_chaudhary 5:dd98cf00ed9b 197
faheem_chaudhary 5:dd98cf00ed9b 198
faheem_chaudhary 5:dd98cf00ed9b 199 /** parent is a utility function to get the parent index of the
faheem_chaudhary 5:dd98cf00ed9b 200 tokenIndex passed.
faheem_chaudhary 5:dd98cf00ed9b 201
faheem_chaudhary 5:dd98cf00ed9b 202 @param tokenIndex representing the token in the JSON source. The valid
faheem_chaudhary 5:dd98cf00ed9b 203 value range is 0 to [parsedTokenCount()-1] both inclusive.
faheem_chaudhary 5:dd98cf00ed9b 204
faheem_chaudhary 5:dd98cf00ed9b 205 @return the parentIndex if the node has a parent, and tokenIndex is a
faheem_chaudhary 5:dd98cf00ed9b 206 valid index. In case of no parent, or invalid tokenIndex, -1
faheem_chaudhary 5:dd98cf00ed9b 207 is returned.
faheem_chaudhary 5:dd98cf00ed9b 208 */
mercurywaters 3:fab591fca1e7 209 inline int parent ( const int tokenIndex ) const;
faheem_chaudhary 5:dd98cf00ed9b 210
faheem_chaudhary 5:dd98cf00ed9b 211
faheem_chaudhary 5:dd98cf00ed9b 212 /** childCount returns the number of children sharing the same parent.
faheem_chaudhary 5:dd98cf00ed9b 213 This utility function is handy for iterating over Arrays or Objects.
faheem_chaudhary 5:dd98cf00ed9b 214
faheem_chaudhary 5:dd98cf00ed9b 215 @param tokenIndex representing the token in the JSON source. The valid
faheem_chaudhary 5:dd98cf00ed9b 216 value range is 0 to [parsedTokenCount()-1] both inclusive.
faheem_chaudhary 5:dd98cf00ed9b 217
faheem_chaudhary 5:dd98cf00ed9b 218 @return non-negative integer representing the number of children
faheem_chaudhary 5:dd98cf00ed9b 219 tokenIndex node has. 0 is a valid number, in case the node has
faheem_chaudhary 5:dd98cf00ed9b 220 no child nodes. -1 will be returned if the tokenIndex is not
faheem_chaudhary 5:dd98cf00ed9b 221 valid.
faheem_chaudhary 5:dd98cf00ed9b 222 */
mercurywaters 3:fab591fca1e7 223 inline int childCount ( const int tokenIndex ) const;
faheem_chaudhary 5:dd98cf00ed9b 224
faheem_chaudhary 5:dd98cf00ed9b 225
faheem_chaudhary 5:dd98cf00ed9b 226 /** tokenLength returns the number of characters a node takes up in JSON
faheem_chaudhary 5:dd98cf00ed9b 227 source string.
faheem_chaudhary 5:dd98cf00ed9b 228
faheem_chaudhary 5:dd98cf00ed9b 229 @param tokenIndex representing the token in the JSON source. The valid
faheem_chaudhary 5:dd98cf00ed9b 230 value range is 0 to [parsedTokenCount()-1] both inclusive.
faheem_chaudhary 5:dd98cf00ed9b 231
faheem_chaudhary 5:dd98cf00ed9b 232 @return positive integer value representing the length of the token
faheem_chaudhary 5:dd98cf00ed9b 233 sub-string in the source JSON. The 0 value is an invalid state
faheem_chaudhary 5:dd98cf00ed9b 234 and should never occur. -1 will be returned in case of invalid
faheem_chaudhary 5:dd98cf00ed9b 235 tokenIndex.
faheem_chaudhary 5:dd98cf00ed9b 236 */
mercurywaters 3:fab591fca1e7 237 inline int tokenLength ( const int tokenIndex ) const;
faheem_chaudhary 5:dd98cf00ed9b 238
faheem_chaudhary 5:dd98cf00ed9b 239
faheem_chaudhary 5:dd98cf00ed9b 240 /** tokenAddress returns the pointer that marks as the start of token
faheem_chaudhary 5:dd98cf00ed9b 241 in JSON source string. This is a utility function for character/string
faheem_chaudhary 5:dd98cf00ed9b 242 manipulation by the caller.
faheem_chaudhary 5:dd98cf00ed9b 243
faheem_chaudhary 5:dd98cf00ed9b 244 @param tokenIndex representing the token in the JSON source. The valid
faheem_chaudhary 5:dd98cf00ed9b 245 value range is 0 to [parsedTokenCount()-1] both inclusive.
faheem_chaudhary 5:dd98cf00ed9b 246
faheem_chaudhary 5:dd98cf00ed9b 247 @return a non-NULL pointer will be returned if tokenIndex is valid, -1
faheem_chaudhary 5:dd98cf00ed9b 248 otherwise.
faheem_chaudhary 5:dd98cf00ed9b 249 */
mercurywaters 3:fab591fca1e7 250 inline const char * tokenAddress ( const int tokenIndex ) const;
mercurywaters 3:fab591fca1e7 251
faheem_chaudhary 5:dd98cf00ed9b 252
faheem_chaudhary 5:dd98cf00ed9b 253 /** tokenInterValue will convert the value as int represented by the
faheem_chaudhary 5:dd98cf00ed9b 254 tokenIndex. A typical use is that caller has found the Key-index, and
faheem_chaudhary 5:dd98cf00ed9b 255 then has retrieved the Value-index (by using findChildIndexOf function)
faheem_chaudhary 5:dd98cf00ed9b 256 , and now they want to read the value of Value-index, as integer value.
faheem_chaudhary 5:dd98cf00ed9b 257
faheem_chaudhary 5:dd98cf00ed9b 258 @param tokenIndex representing the "value" in the JSON source. The
faheem_chaudhary 5:dd98cf00ed9b 259 valid value range is 0 to [parsedTokenCount()-1] both inclusive.
faheem_chaudhary 5:dd98cf00ed9b 260
faheem_chaudhary 5:dd98cf00ed9b 261 @param returnValue is a return-parameter passed by reference to hold up
faheem_chaudhary 5:dd98cf00ed9b 262 the integer value parsed by this function. If the converted
faheem_chaudhary 5:dd98cf00ed9b 263 value would be out of the range of representable values by an
faheem_chaudhary 5:dd98cf00ed9b 264 int, it causes undefined behavior. It is caller's
faheem_chaudhary 5:dd98cf00ed9b 265 responsibility to check for these cases.
faheem_chaudhary 5:dd98cf00ed9b 266
faheem_chaudhary 5:dd98cf00ed9b 267 @return 0 if the operation is successful. -1 if tokenIndex is invalid.
faheem_chaudhary 5:dd98cf00ed9b 268 */
faheem_chaudhary 5:dd98cf00ed9b 269 int tokenIntegerValue ( const int tokenIndex, int &returnValue ) const;
faheem_chaudhary 5:dd98cf00ed9b 270
faheem_chaudhary 5:dd98cf00ed9b 271
faheem_chaudhary 5:dd98cf00ed9b 272 /** tokenNumberValue will convert the value as float represented by the
faheem_chaudhary 5:dd98cf00ed9b 273 tokenIndex. A typical use is that caller has found the Key-index, and
faheem_chaudhary 5:dd98cf00ed9b 274 then has retrieved the Value-index (by using findChildIndexOf function)
faheem_chaudhary 5:dd98cf00ed9b 275 , and now they want to read the value of Value-index, as floating-point
faheem_chaudhary 5:dd98cf00ed9b 276 value.
faheem_chaudhary 5:dd98cf00ed9b 277
faheem_chaudhary 5:dd98cf00ed9b 278 @param tokenIndex representing the "value" in the JSON source. The
faheem_chaudhary 5:dd98cf00ed9b 279 valid value range is 0 to [parsedTokenCount()-1] both inclusive.
faheem_chaudhary 5:dd98cf00ed9b 280
faheem_chaudhary 5:dd98cf00ed9b 281 @param returnValue is a return-parameter passed by reference to hold up
faheem_chaudhary 5:dd98cf00ed9b 282 the floating-point value parsed by this function. If the
faheem_chaudhary 5:dd98cf00ed9b 283 converted value would be out of the range of representable
faheem_chaudhary 5:dd98cf00ed9b 284 values by a float, it causes undefined behavior. It is caller's
faheem_chaudhary 5:dd98cf00ed9b 285 responsibility to check for these cases.
mercurywaters 3:fab591fca1e7 286
faheem_chaudhary 5:dd98cf00ed9b 287 @return 0 if the operation is successful. -1 if tokenIndex is invalid.
faheem_chaudhary 5:dd98cf00ed9b 288 */
faheem_chaudhary 5:dd98cf00ed9b 289 int tokenNumberValue ( const int tokenIndex, float &returnValue ) const;
faheem_chaudhary 5:dd98cf00ed9b 290
faheem_chaudhary 5:dd98cf00ed9b 291
faheem_chaudhary 5:dd98cf00ed9b 292 /** tokenBooleanValue will convert the value as bool represented by
faheem_chaudhary 5:dd98cf00ed9b 293 the tokenIndex. A typical use is that caller has found the Key-index,
faheem_chaudhary 5:dd98cf00ed9b 294 and then has retrieved the Value-index (by using findChildIndexOf
faheem_chaudhary 5:dd98cf00ed9b 295 function), and now they want to read the value of Value-index, as
faheem_chaudhary 5:dd98cf00ed9b 296 boolean value.
faheem_chaudhary 5:dd98cf00ed9b 297
faheem_chaudhary 5:dd98cf00ed9b 298 @param tokenIndex representing the "value" in the JSON source. The
faheem_chaudhary 5:dd98cf00ed9b 299 valid value range is 0 to [parsedTokenCount()-1] both inclusive.
faheem_chaudhary 5:dd98cf00ed9b 300
faheem_chaudhary 5:dd98cf00ed9b 301 @param returnValue is a return-parameter passed by reference to hold up
faheem_chaudhary 5:dd98cf00ed9b 302 the bool value parsed by this function.
mercurywaters 3:fab591fca1e7 303
faheem_chaudhary 5:dd98cf00ed9b 304 @return 0 if the operation is successful. -1 if tokenIndex is invalid.
faheem_chaudhary 5:dd98cf00ed9b 305 */
faheem_chaudhary 5:dd98cf00ed9b 306 int tokenBooleanValue ( const int tokenIndex, bool &returnValue ) const;
faheem_chaudhary 5:dd98cf00ed9b 307
faheem_chaudhary 5:dd98cf00ed9b 308
faheem_chaudhary 5:dd98cf00ed9b 309 /** unescape is a utility function to unescape a JSON string. This
faheem_chaudhary 5:dd98cf00ed9b 310 function does not change any state of Json object, and is a pure
faheem_chaudhary 5:dd98cf00ed9b 311 static utility function. This function is in-pace unescaping, and WILL
faheem_chaudhary 5:dd98cf00ed9b 312 modify the source parameter.
faheem_chaudhary 5:dd98cf00ed9b 313
faheem_chaudhary 5:dd98cf00ed9b 314 @param jsonString representing an escaped JSON string. This parameter
faheem_chaudhary 5:dd98cf00ed9b 315 is also the return parameter as well. All modifications will be
faheem_chaudhary 5:dd98cf00ed9b 316 reflected in this parameter.
faheem_chaudhary 5:dd98cf00ed9b 317
faheem_chaudhary 5:dd98cf00ed9b 318 @return pointer to unescaped JSON string. This is exactly the same
faheem_chaudhary 5:dd98cf00ed9b 319 pointer as jsonString parameter.
faheem_chaudhary 5:dd98cf00ed9b 320 */
faheem_chaudhary 4:ae34010d87e5 321 static char * unescape ( char * jsonString );
mercurywaters 3:fab591fca1e7 322 };
mercurywaters 3:fab591fca1e7 323
faheem_chaudhary 5:dd98cf00ed9b 324 inline int Json::parsedTokenCount () const
faheem_chaudhary 5:dd98cf00ed9b 325 {
faheem_chaudhary 5:dd98cf00ed9b 326 return tokenCount;
faheem_chaudhary 5:dd98cf00ed9b 327 }
faheem_chaudhary 5:dd98cf00ed9b 328
mercurywaters 3:fab591fca1e7 329 inline bool Json::isValidJson () const
mercurywaters 3:fab591fca1e7 330 {
mercurywaters 3:fab591fca1e7 331 return ( tokenCount >= 1 );
mercurywaters 3:fab591fca1e7 332 }
mercurywaters 3:fab591fca1e7 333
faheem_chaudhary 5:dd98cf00ed9b 334 inline bool Json::isValidToken ( const int tokenIndex ) const
faheem_chaudhary 5:dd98cf00ed9b 335 {
faheem_chaudhary 5:dd98cf00ed9b 336 return ( tokenIndex >= 0 && tokenIndex < tokenCount );
faheem_chaudhary 5:dd98cf00ed9b 337 }
faheem_chaudhary 5:dd98cf00ed9b 338
mercurywaters 3:fab591fca1e7 339 inline jsmntype_t Json::type ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 340 {
faheem_chaudhary 5:dd98cf00ed9b 341 jsmntype_t retVal = JSMN_UNDEFINED;
faheem_chaudhary 5:dd98cf00ed9b 342
faheem_chaudhary 5:dd98cf00ed9b 343 if ( isValidToken ( tokenIndex ) )
faheem_chaudhary 5:dd98cf00ed9b 344 {
faheem_chaudhary 5:dd98cf00ed9b 345 retVal = tokens [ tokenIndex ].type;
faheem_chaudhary 5:dd98cf00ed9b 346 }
faheem_chaudhary 5:dd98cf00ed9b 347
faheem_chaudhary 5:dd98cf00ed9b 348 return retVal;
mercurywaters 3:fab591fca1e7 349 }
mercurywaters 3:fab591fca1e7 350
mercurywaters 3:fab591fca1e7 351 inline int Json::parent ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 352 {
faheem_chaudhary 5:dd98cf00ed9b 353 int retVal = -1;
faheem_chaudhary 5:dd98cf00ed9b 354
faheem_chaudhary 5:dd98cf00ed9b 355 if ( isValidToken ( tokenIndex ) )
faheem_chaudhary 5:dd98cf00ed9b 356 {
faheem_chaudhary 5:dd98cf00ed9b 357 retVal = tokens [ tokenIndex ].parent;
faheem_chaudhary 5:dd98cf00ed9b 358 }
faheem_chaudhary 5:dd98cf00ed9b 359
faheem_chaudhary 5:dd98cf00ed9b 360 return retVal;
mercurywaters 3:fab591fca1e7 361 }
mercurywaters 3:fab591fca1e7 362
mercurywaters 3:fab591fca1e7 363 inline int Json::childCount ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 364 {
faheem_chaudhary 5:dd98cf00ed9b 365 int retVal = -1;
faheem_chaudhary 5:dd98cf00ed9b 366
faheem_chaudhary 5:dd98cf00ed9b 367 if ( isValidToken ( tokenIndex ) )
faheem_chaudhary 5:dd98cf00ed9b 368 {
faheem_chaudhary 5:dd98cf00ed9b 369 retVal = tokens [ tokenIndex ].childCount;
faheem_chaudhary 5:dd98cf00ed9b 370 }
faheem_chaudhary 5:dd98cf00ed9b 371
faheem_chaudhary 5:dd98cf00ed9b 372 return retVal;
mercurywaters 3:fab591fca1e7 373 }
mercurywaters 3:fab591fca1e7 374
mercurywaters 3:fab591fca1e7 375 inline int Json::tokenLength ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 376 {
faheem_chaudhary 5:dd98cf00ed9b 377 int retVal = -1;
faheem_chaudhary 5:dd98cf00ed9b 378
faheem_chaudhary 5:dd98cf00ed9b 379 if ( isValidToken ( tokenIndex ) )
faheem_chaudhary 5:dd98cf00ed9b 380 {
faheem_chaudhary 5:dd98cf00ed9b 381 retVal = tokens [ tokenIndex ].end - tokens [ tokenIndex ].start;
faheem_chaudhary 5:dd98cf00ed9b 382 }
faheem_chaudhary 5:dd98cf00ed9b 383
faheem_chaudhary 5:dd98cf00ed9b 384 return retVal;
mercurywaters 3:fab591fca1e7 385 }
mercurywaters 3:fab591fca1e7 386
mercurywaters 3:fab591fca1e7 387 inline const char * Json::tokenAddress ( const int tokenIndex ) const
mercurywaters 3:fab591fca1e7 388 {
faheem_chaudhary 5:dd98cf00ed9b 389 char * retVal = NULL;
mercurywaters 3:fab591fca1e7 390
faheem_chaudhary 5:dd98cf00ed9b 391 if ( isValidToken ( tokenIndex ) )
faheem_chaudhary 4:ae34010d87e5 392 {
faheem_chaudhary 5:dd98cf00ed9b 393 retVal = (char *) source + tokens [ tokenIndex ].start;
faheem_chaudhary 4:ae34010d87e5 394 }
faheem_chaudhary 5:dd98cf00ed9b 395
faheem_chaudhary 5:dd98cf00ed9b 396 return retVal;
faheem_chaudhary 4:ae34010d87e5 397 }
faheem_chaudhary 4:ae34010d87e5 398
mercurywaters 3:fab591fca1e7 399 #endif
mercurywaters 3:fab591fca1e7 400