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