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.cpp@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.cpp */ |
| 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 | #include "Json.h" |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 24 | #include <stdio.h> |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 25 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 26 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 27 | // Json::Json() : maxTokenCount ( 0 ), sourceLength ( 0 ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 28 | // { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 29 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 30 | // } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 31 | // Json::Json() :maxTokenCount ( 0 ), source ( NULL ), sourceLength ( 0 ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 32 | // { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 33 | // tokenCount = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 34 | // tokens = NULL; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 35 | // } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 36 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 37 | Json::Json ( const char * jsonString, size_t length, unsigned int maxTokens ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 38 | : maxTokenCount ( maxTokens ), source ( jsonString ), sourceLength ( length ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 39 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 40 | jsmn_parser parser; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 41 | tokens = new jsmntok_t [ maxTokenCount ]; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 42 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 43 | jsmn_init ( &parser ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 44 | tokenCount = jsmn_parse ( &parser, jsonString, length, tokens, maxTokenCount ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 45 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 46 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 47 | Json::Json ( const Json & ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 48 | : maxTokenCount ( 0 ), source ( NULL ), sourceLength ( 0 ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 49 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 50 | tokenCount = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 51 | tokens = NULL; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 52 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 53 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 54 | Json::~Json () |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 55 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 56 | delete [] tokens; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 57 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 58 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 59 | int Json::findKeyIndex ( const char * key, const int &startingAt ) const |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 60 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 61 | int retVal = -1; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 62 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 63 | int i = startingAt + 1; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 64 | if ( i < 0 ) { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 65 | i = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 66 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 67 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 68 | for ( ; i < tokenCount; i++ ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 69 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 70 | jsmntok_t t = tokens [ i ]; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 71 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 72 | if ( t.type == JSMN_KEY ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 73 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 74 | size_t keyLength = (size_t) ( t.end - t.start ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 75 | if ( ( strlen ( key ) == keyLength ) && ( strncmp ( source + t.start, key, keyLength ) == 0 ) ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 76 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 77 | retVal = i; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 78 | break; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 79 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 80 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 81 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 82 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 83 | return retVal; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 84 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 85 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 86 | int Json::findKeyIndexIn ( const char * key, const int &parentIndex ) const |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 87 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 88 | int retVal = -1; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 89 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 90 | if ( isValidToken ( parentIndex ) ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 91 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 92 | for ( int i = parentIndex + 1; i < tokenCount; i++ ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 93 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 94 | jsmntok_t t = tokens [ i ]; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 95 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 96 | if ( t.end >= tokens [ parentIndex ].end ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 97 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 98 | break; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 99 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 100 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 101 | if ( ( t.type == JSMN_KEY ) && ( t.parent == parentIndex ) ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 102 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 103 | size_t keyLength = (size_t) ( t.end - t.start ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 104 | if ( ( strlen ( key ) == keyLength ) && ( strncmp ( source + t.start, key, keyLength ) == 0 ) ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 105 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 106 | retVal = i; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 107 | break; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 108 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 109 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 110 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 111 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 112 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 113 | return retVal; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 114 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 115 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 116 | int Json::findChildIndexOf ( const int &parentIndex, const int &startingAt ) const |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 117 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 118 | int retVal = -1; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 119 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 120 | if ( isValidToken ( parentIndex ) ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 121 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 122 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 123 | jsmntype_t type = tokens [ parentIndex ].type; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 124 | if ( ( type == JSMN_KEY ) || ( type == JSMN_OBJECT ) || ( type == JSMN_ARRAY ) ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 125 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 126 | int i = startingAt + 1; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 127 | if ( startingAt < 0 ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 128 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 129 | i = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 130 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 131 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 132 | for ( i += parentIndex; i < tokenCount; i++ ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 133 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 134 | if ( tokens [ i ].parent == parentIndex ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 135 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 136 | retVal = i; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 137 | break; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 138 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 139 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 140 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 141 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 142 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 143 | return retVal; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 144 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 145 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 146 | bool Json::matches ( const int & tokenIndex, const char * value ) const |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 147 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 148 | bool retVal = false; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 149 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 150 | if ( isValidToken ( tokenIndex ) ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 151 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 152 | jsmntok_t token = tokens [ tokenIndex ]; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 153 | retVal = ( strncmp ( source + token.start, value, ( token.end - token.start ) ) == 0 ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 154 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 155 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 156 | return retVal; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 157 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 158 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 159 | int Json::tokenIntegerValue ( const int tokenIndex, int &returnValue ) const |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 160 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 161 | int retVal = -1; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 162 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 163 | if ( type ( tokenIndex ) == JSMN_PRIMITIVE ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 164 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 165 | int len = tokenLength ( tokenIndex ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 166 | char * tok = new char [ len + 1 ]; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 167 | strncpy ( tok, tokenAddress ( tokenIndex ), len ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 168 | tok [ len ] = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 169 | returnValue = atoi ( tok ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 170 | delete [] tok; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 171 | retVal = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 172 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 173 | return retVal; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 174 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 175 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 176 | int Json::tokenNumberValue ( const int tokenIndex, float &returnValue ) const |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 177 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 178 | int retVal = -1; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 179 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 180 | if ( type ( tokenIndex ) == JSMN_PRIMITIVE ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 181 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 182 | int len = tokenLength ( tokenIndex ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 183 | char * tok = new char [ len + 1 ]; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 184 | strncpy ( tok, tokenAddress ( tokenIndex ), len ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 185 | tok [ len ] = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 186 | returnValue = atof ( tok ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 187 | delete [] tok; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 188 | retVal = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 189 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 190 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 191 | return retVal; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 192 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 193 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 194 | int Json::tokenBooleanValue ( const int tokenIndex, bool &returnValue ) const |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 195 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 196 | int retVal = -1; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 197 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 198 | if ( type ( tokenIndex ) == JSMN_PRIMITIVE ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 199 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 200 | returnValue = matches ( tokenIndex, "true" ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 201 | retVal = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 202 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 203 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 204 | return retVal; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 205 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 206 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 207 | char * Json::unescape ( char * jsonString ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 208 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 209 | if ( jsonString != NULL ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 210 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 211 | int stringIndex = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 212 | int indentLevel = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 213 | int quoteCount = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 214 | for ( int i = 0; jsonString [ i ] != 0; i ++ ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 215 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 216 | switch ( jsonString [ i ] ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 217 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 218 | case '{': |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 219 | indentLevel ++; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 220 | break; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 221 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 222 | case '}': |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 223 | indentLevel --; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 224 | if ( indentLevel == 0 ) { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 225 | // Just close and return the first valid JSON object. No need to handle complex cases. |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 226 | jsonString [ stringIndex ++ ] = '}'; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 227 | jsonString [ stringIndex ] = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 228 | return jsonString; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 229 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 230 | break; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 231 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 232 | case '\\': |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 233 | i ++; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 234 | break; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 235 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 236 | case '"': |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 237 | quoteCount ++; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 238 | break; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 239 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 240 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 241 | if ( indentLevel > 0 ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 242 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 243 | if ( quoteCount == 0 ) { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 244 | return jsonString; //No need to unescape. JsonString needs to be already escaped |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 245 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 246 | jsonString [ stringIndex ++ ] = jsonString [ i ]; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 247 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 248 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 249 | jsonString [ stringIndex ] = 0; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 250 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 251 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 252 | return jsonString; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 253 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 254 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 255 | const char * Json::JsonParse(const char * jsonString, const char * key) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 256 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 257 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 258 | Json json ( jsonString, strlen ( jsonString)); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 259 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 260 | if ( !json.isValidJson () ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 261 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 262 | printf( "Invalid JSON: %s", jsonString ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 263 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 264 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 265 | if ( json.type (0) != JSMN_OBJECT ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 266 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 267 | printf ( "Invalid JSON. ROOT element is not Object: %s", jsonString ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 268 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 269 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 270 | char *info1 = new char[32]; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 271 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 272 | // ROOT object should have '0' tokenIndex, and -1 parentIndex |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 273 | int KeyIndex = json.findKeyIndexIn(key,0); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 274 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 275 | if (KeyIndex == -1) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 276 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 277 | // Error handling part ... |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 278 | printf ( "Key does not exist"); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 279 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 280 | else |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 281 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 282 | // Find the first child index of key-node "info" |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 283 | int ValueIndex = json.findChildIndexOf (KeyIndex, -1 ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 284 | if (ValueIndex > 0 ) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 285 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 286 | const char * valueStart = json.tokenAddress (ValueIndex ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 287 | int valueLength = json.tokenLength (ValueIndex ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 288 | strncpy ( info1, valueStart, valueLength ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 289 | // info[valueLength] = 0; // NULL-terminate the string |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 290 | // strcpy(info1, info); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 291 | //let's print the value. It should be "San Jose" |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 292 | // printf( "info: %s", info); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 293 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 294 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 295 | return info1; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 296 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 297 | // plublic: |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 298 | // const char* getBar() const { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 299 | // char * str = new char[_sBar.length()+1]; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 300 | // strcpy(str, _sBar.c_str()); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 301 | // return str;} |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 302 | // In main: |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 303 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 304 | // foo object; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 305 | // ///some code |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 306 | // const char* bar = object.getBar(); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 307 | // ///some code |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 308 | // delete [] bar; |