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.
Fork of MbedJSONValue_copy by
MbedJSONValue.cpp@6:f8bec9cada34, 2016-04-05 (annotated)
- Committer:
- MidnightCow
- Date:
- Tue Apr 05 11:37:53 2016 +0000
- Revision:
- 6:f8bec9cada34
- Parent:
- 5:e15fd7be9e74
- Child:
- 7:62ce68f5a38c
1st Release
; - MbedJson Modified (Dynamic alloc)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:0cf0e27feaad | 1 | #include "MbedJSONValue.h" |
samux | 0:0cf0e27feaad | 2 | |
samux | 0:0cf0e27feaad | 3 | # include <stdlib.h> |
samux | 0:0cf0e27feaad | 4 | # include <stdio.h> |
samux | 0:0cf0e27feaad | 5 | |
MidnightCow | 6:f8bec9cada34 | 6 | bool default_json_cb(void* param) |
MidnightCow | 6:f8bec9cada34 | 7 | { |
MidnightCow | 6:f8bec9cada34 | 8 | printf("No callback function\n"); |
MidnightCow | 6:f8bec9cada34 | 9 | return false; |
MidnightCow | 6:f8bec9cada34 | 10 | } |
MidnightCow | 6:f8bec9cada34 | 11 | |
MidnightCow | 6:f8bec9cada34 | 12 | |
samux | 0:0cf0e27feaad | 13 | // Clean up |
samux | 0:0cf0e27feaad | 14 | void MbedJSONValue::clean() { |
samux | 0:0cf0e27feaad | 15 | switch (_type) { |
samux | 0:0cf0e27feaad | 16 | case TypeString: |
samux | 0:0cf0e27feaad | 17 | delete _value.asString; |
samux | 0:0cf0e27feaad | 18 | break; |
samux | 0:0cf0e27feaad | 19 | case TypeArray: |
MidnightCow | 6:f8bec9cada34 | 20 | //for (int i = 0; i < index_array; i++) |
MidnightCow | 6:f8bec9cada34 | 21 | //delete array[i]; |
samux | 0:0cf0e27feaad | 22 | index_array = 0; |
samux | 0:0cf0e27feaad | 23 | break; |
samux | 0:0cf0e27feaad | 24 | case TypeObject: |
samux | 0:0cf0e27feaad | 25 | for (int i = 0; i < index_token; i++) { |
samux | 0:0cf0e27feaad | 26 | delete token[i]; |
samux | 0:0cf0e27feaad | 27 | delete token_name[i]; |
samux | 0:0cf0e27feaad | 28 | } |
samux | 0:0cf0e27feaad | 29 | index_token = 0; |
samux | 0:0cf0e27feaad | 30 | break; |
samux | 0:0cf0e27feaad | 31 | default: |
samux | 0:0cf0e27feaad | 32 | break; |
samux | 0:0cf0e27feaad | 33 | } |
samux | 0:0cf0e27feaad | 34 | _type = TypeNull; |
samux | 0:0cf0e27feaad | 35 | _type = TypeNull; |
samux | 0:0cf0e27feaad | 36 | } |
samux | 0:0cf0e27feaad | 37 | |
samux | 0:0cf0e27feaad | 38 | bool MbedJSONValue::hasMember(char * name) |
samux | 0:0cf0e27feaad | 39 | { |
samux | 0:0cf0e27feaad | 40 | for(int i = 0; i < index_token; i++) |
samux | 0:0cf0e27feaad | 41 | if( !strcmp(name, (*(token_name[i])).c_str() )) |
samux | 0:0cf0e27feaad | 42 | return true; |
samux | 0:0cf0e27feaad | 43 | return false; |
samux | 0:0cf0e27feaad | 44 | } |
samux | 0:0cf0e27feaad | 45 | |
samux | 0:0cf0e27feaad | 46 | |
samux | 0:0cf0e27feaad | 47 | void copy(const std::string& s, std::back_insert_iterator<std::string> oi) { |
samux | 0:0cf0e27feaad | 48 | std::copy(s.begin(), s.end(), oi); |
samux | 0:0cf0e27feaad | 49 | } |
samux | 0:0cf0e27feaad | 50 | |
samux | 0:0cf0e27feaad | 51 | void serialize_str(const std::string& s, std::back_insert_iterator<std::string> oi) { |
samux | 0:0cf0e27feaad | 52 | *oi++ = '"'; |
samux | 0:0cf0e27feaad | 53 | for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { |
samux | 0:0cf0e27feaad | 54 | switch (*i) { |
samux | 0:0cf0e27feaad | 55 | #define MAP(val, sym) case val: copy(sym, oi); break |
samux | 0:0cf0e27feaad | 56 | MAP('"', "\\\""); |
samux | 0:0cf0e27feaad | 57 | MAP('\\', "\\\\"); |
samux | 0:0cf0e27feaad | 58 | MAP('/', "\\/"); |
samux | 0:0cf0e27feaad | 59 | MAP('\b', "\\b"); |
samux | 0:0cf0e27feaad | 60 | MAP('\f', "\\f"); |
samux | 0:0cf0e27feaad | 61 | MAP('\n', "\\n"); |
samux | 0:0cf0e27feaad | 62 | MAP('\r', "\\r"); |
samux | 0:0cf0e27feaad | 63 | MAP('\t', "\\t"); |
samux | 0:0cf0e27feaad | 64 | #undef MAP |
samux | 0:0cf0e27feaad | 65 | default: |
samux | 0:0cf0e27feaad | 66 | if ((unsigned char)*i < 0x20 || *i == 0x7f) { |
samux | 0:0cf0e27feaad | 67 | char buf[7]; |
samux | 0:0cf0e27feaad | 68 | sprintf(buf, "\\u%04x", *i & 0xff); |
samux | 0:0cf0e27feaad | 69 | copy(buf, buf + 6, oi); |
samux | 0:0cf0e27feaad | 70 | } else { |
samux | 0:0cf0e27feaad | 71 | *oi++ = *i; |
samux | 0:0cf0e27feaad | 72 | } |
samux | 0:0cf0e27feaad | 73 | break; |
samux | 0:0cf0e27feaad | 74 | } |
samux | 0:0cf0e27feaad | 75 | } |
samux | 0:0cf0e27feaad | 76 | *oi++ = '"'; |
samux | 0:0cf0e27feaad | 77 | } |
samux | 0:0cf0e27feaad | 78 | |
samux | 0:0cf0e27feaad | 79 | std::string MbedJSONValue::serialize(){ |
samux | 0:0cf0e27feaad | 80 | std::string s; |
samux | 0:0cf0e27feaad | 81 | serialize(std::back_inserter(s)); |
samux | 0:0cf0e27feaad | 82 | return s; |
samux | 0:0cf0e27feaad | 83 | } |
samux | 0:0cf0e27feaad | 84 | |
samux | 0:0cf0e27feaad | 85 | std::string MbedJSONValue::to_str(){ |
samux | 0:0cf0e27feaad | 86 | switch (_type) { |
samux | 0:0cf0e27feaad | 87 | case TypeNull: |
samux | 0:0cf0e27feaad | 88 | return "null"; |
samux | 0:0cf0e27feaad | 89 | case TypeBoolean: |
samux | 0:0cf0e27feaad | 90 | return _value.asBool ? "true" : "false"; |
samux | 0:0cf0e27feaad | 91 | case TypeInt: { |
samux | 0:0cf0e27feaad | 92 | char buf[10]; |
samux | 0:0cf0e27feaad | 93 | sprintf(buf, "%d", _value.asInt); |
samux | 0:0cf0e27feaad | 94 | return buf; |
samux | 0:0cf0e27feaad | 95 | } |
samux | 0:0cf0e27feaad | 96 | case TypeDouble: { |
samux | 0:0cf0e27feaad | 97 | char buf[10]; |
samux | 0:0cf0e27feaad | 98 | sprintf(buf, "%f", _value.asDouble); |
samux | 0:0cf0e27feaad | 99 | return buf; |
samux | 0:0cf0e27feaad | 100 | } |
samux | 0:0cf0e27feaad | 101 | default: |
samux | 0:0cf0e27feaad | 102 | break; |
samux | 0:0cf0e27feaad | 103 | } |
samux | 0:0cf0e27feaad | 104 | return NULL; |
samux | 0:0cf0e27feaad | 105 | } |
samux | 0:0cf0e27feaad | 106 | |
samux | 0:0cf0e27feaad | 107 | |
samux | 0:0cf0e27feaad | 108 | |
samux | 0:0cf0e27feaad | 109 | void MbedJSONValue::serialize(std::back_insert_iterator<std::string> oi) { |
MidnightCow | 6:f8bec9cada34 | 110 | printf("SP:%X _type=%d, index=%d\r\n",__current_sp(),_type, index_token); |
MidnightCow | 6:f8bec9cada34 | 111 | |
samux | 0:0cf0e27feaad | 112 | switch (_type) { |
samux | 0:0cf0e27feaad | 113 | case TypeString: |
MidnightCow | 6:f8bec9cada34 | 114 | printf("typestring\r\n"); |
samux | 0:0cf0e27feaad | 115 | serialize_str(*_value.asString, oi); |
samux | 0:0cf0e27feaad | 116 | break; |
samux | 0:0cf0e27feaad | 117 | case TypeArray: { |
MidnightCow | 6:f8bec9cada34 | 118 | printf("typearray\r\n"); |
samux | 0:0cf0e27feaad | 119 | *oi++ = '['; |
samux | 0:0cf0e27feaad | 120 | for (int i = 0; i < index_array; i++) { |
samux | 0:0cf0e27feaad | 121 | if (i) |
samux | 0:0cf0e27feaad | 122 | *oi++ = ','; |
samux | 0:0cf0e27feaad | 123 | (*this)[i].serialize(oi); |
samux | 0:0cf0e27feaad | 124 | } |
samux | 0:0cf0e27feaad | 125 | *oi++ = ']'; |
samux | 0:0cf0e27feaad | 126 | break; |
samux | 0:0cf0e27feaad | 127 | } |
samux | 0:0cf0e27feaad | 128 | case TypeObject: { |
MidnightCow | 6:f8bec9cada34 | 129 | printf("typeObject\r\n"); |
samux | 0:0cf0e27feaad | 130 | *oi++ = '{'; |
samux | 0:0cf0e27feaad | 131 | for (int i = 0; i < index_token; i++) { |
MidnightCow | 6:f8bec9cada34 | 132 | printf("i=%d ",i); |
samux | 0:0cf0e27feaad | 133 | if (i) |
samux | 0:0cf0e27feaad | 134 | *oi++ = ','; |
MidnightCow | 6:f8bec9cada34 | 135 | printf("token_name[%d]=%s\r\n",i,token_name[i]->c_str()); |
samux | 0:0cf0e27feaad | 136 | serialize_str(*(token_name[i]), oi); |
samux | 0:0cf0e27feaad | 137 | *oi++ = ':'; |
samux | 0:0cf0e27feaad | 138 | (*(token[i])).serialize(oi); |
samux | 0:0cf0e27feaad | 139 | } |
samux | 0:0cf0e27feaad | 140 | *oi++ = '}'; |
samux | 0:0cf0e27feaad | 141 | break; |
samux | 0:0cf0e27feaad | 142 | } |
samux | 0:0cf0e27feaad | 143 | default: |
MidnightCow | 6:f8bec9cada34 | 144 | printf("default\r\n"); |
samux | 0:0cf0e27feaad | 145 | copy(to_str(), oi); |
samux | 0:0cf0e27feaad | 146 | break; |
samux | 0:0cf0e27feaad | 147 | } |
samux | 0:0cf0e27feaad | 148 | } |
samux | 0:0cf0e27feaad | 149 | |
samux | 0:0cf0e27feaad | 150 | |
samux | 0:0cf0e27feaad | 151 | |
samux | 0:0cf0e27feaad | 152 | MbedJSONValue& MbedJSONValue::operator[](int i) { |
samux | 0:0cf0e27feaad | 153 | _type = TypeArray; |
MidnightCow | 6:f8bec9cada34 | 154 | //if (i < NB_TOKEN && index_array == i ) { |
MidnightCow | 6:f8bec9cada34 | 155 | if( index_array == i){ |
samux | 0:0cf0e27feaad | 156 | #ifdef DEBUG |
samux | 0:0cf0e27feaad | 157 | printf("will add an element to the array\r\n"); |
samux | 0:0cf0e27feaad | 158 | #endif |
MidnightCow | 6:f8bec9cada34 | 159 | //array[i] = new MbedJSONValue(); |
MidnightCow | 6:f8bec9cada34 | 160 | //array.push_back(new MbedJSONValue()); |
MidnightCow | 6:f8bec9cada34 | 161 | token.push_back(new MbedJSONValue()); |
samux | 0:0cf0e27feaad | 162 | index_array++; |
MidnightCow | 6:f8bec9cada34 | 163 | //return *(array[i]); |
MidnightCow | 6:f8bec9cada34 | 164 | return *(token[i]); |
samux | 0:0cf0e27feaad | 165 | } |
MidnightCow | 6:f8bec9cada34 | 166 | //if (i < NB_TOKEN && index_array > i) |
MidnightCow | 6:f8bec9cada34 | 167 | if(index_array > i) |
MidnightCow | 6:f8bec9cada34 | 168 | // return *(array[i]); |
MidnightCow | 6:f8bec9cada34 | 169 | return *(token[i]); |
samux | 0:0cf0e27feaad | 170 | |
samux | 0:0cf0e27feaad | 171 | //if the user is not doing something wrong, this code is never executed!! |
samux | 0:0cf0e27feaad | 172 | return *(new MbedJSONValue()); |
MidnightCow | 6:f8bec9cada34 | 173 | //return 0; |
samux | 0:0cf0e27feaad | 174 | } |
samux | 0:0cf0e27feaad | 175 | |
samux | 0:0cf0e27feaad | 176 | MbedJSONValue& MbedJSONValue::operator[](std::string k) { |
samux | 0:0cf0e27feaad | 177 | _type = TypeObject; |
samux | 0:0cf0e27feaad | 178 | for (int i = 0; i < index_token; i++) { |
samux | 0:0cf0e27feaad | 179 | #ifdef DEBUG |
samux | 0:0cf0e27feaad | 180 | printf("k: %s\r\n", k.c_str()); |
samux | 0:0cf0e27feaad | 181 | printf("str: %s\r\n", token_name[i]->c_str()); |
samux | 0:0cf0e27feaad | 182 | #endif |
samux | 0:0cf0e27feaad | 183 | //existing token |
MidnightCow | 6:f8bec9cada34 | 184 | |
samux | 0:0cf0e27feaad | 185 | if (!strcmp(k.c_str(), token_name[i]->c_str())) { |
samux | 0:0cf0e27feaad | 186 | #ifdef DEBUG |
samux | 0:0cf0e27feaad | 187 | printf("token found: %d\r\n", i); |
samux | 0:0cf0e27feaad | 188 | #endif |
samux | 0:0cf0e27feaad | 189 | return *(token[i]); |
samux | 0:0cf0e27feaad | 190 | } |
samux | 0:0cf0e27feaad | 191 | } |
samux | 0:0cf0e27feaad | 192 | |
MidnightCow | 6:f8bec9cada34 | 193 | //if(index_token >= NB_TOKEN) |
MidnightCow | 6:f8bec9cada34 | 194 | // index_token = NB_TOKEN - 1; |
samux | 0:0cf0e27feaad | 195 | //non existing token |
MidnightCow | 6:f8bec9cada34 | 196 | //token_name[index_token] = new std::string(k); |
MidnightCow | 6:f8bec9cada34 | 197 | token_name.push_back(new std::string(k)); |
MidnightCow | 6:f8bec9cada34 | 198 | printf("New token_name[%d]=%X, %s\r\n", index_token, token_name[index_token], token_name[index_token]->c_str()); |
MidnightCow | 6:f8bec9cada34 | 199 | //token[index_token] = new MbedJSONValue(); |
MidnightCow | 6:f8bec9cada34 | 200 | token.push_back(new MbedJSONValue()); |
MidnightCow | 6:f8bec9cada34 | 201 | printf("New token[%d]=%X, %d\r\n", index_token, token[index_token], token[index_token]->getType()); |
samux | 0:0cf0e27feaad | 202 | index_token++; |
samux | 0:0cf0e27feaad | 203 | return *(token[index_token - 1]); |
samux | 0:0cf0e27feaad | 204 | } |
samux | 0:0cf0e27feaad | 205 | |
samux | 0:0cf0e27feaad | 206 | MbedJSONValue& MbedJSONValue::operator[](std::string k) const |
samux | 0:0cf0e27feaad | 207 | { |
samux | 0:0cf0e27feaad | 208 | for (int i = 0; i < index_token; i++) { |
samux | 0:0cf0e27feaad | 209 | #ifdef DEBUG |
samux | 0:0cf0e27feaad | 210 | printf("k: %s\r\n", k.c_str()); |
MidnightCow | 6:f8bec9cada34 | 211 | //printf("str: %s\r\n", token_name[i]->c_str()); |
samux | 0:0cf0e27feaad | 212 | #endif |
MidnightCow | 6:f8bec9cada34 | 213 | printf("[%s]", k.c_str()); |
MidnightCow | 6:f8bec9cada34 | 214 | printf(" comp "); |
MidnightCow | 6:f8bec9cada34 | 215 | printf("[%s]\r\n", token_name[i]->c_str()); |
samux | 0:0cf0e27feaad | 216 | if (!strcmp(k.c_str(), token_name[i]->c_str())) { |
samux | 0:0cf0e27feaad | 217 | #ifdef DEBUG |
samux | 0:0cf0e27feaad | 218 | printf("token found: %d\r\n", i); |
samux | 0:0cf0e27feaad | 219 | #endif |
samux | 0:0cf0e27feaad | 220 | return *(token[i]); |
samux | 0:0cf0e27feaad | 221 | } |
samux | 0:0cf0e27feaad | 222 | } |
samux | 0:0cf0e27feaad | 223 | |
samux | 0:0cf0e27feaad | 224 | //if the user is not doing something wrong, this code is never executed!! |
samux | 0:0cf0e27feaad | 225 | return *(new MbedJSONValue()); |
MidnightCow | 6:f8bec9cada34 | 226 | //return 0; |
samux | 0:0cf0e27feaad | 227 | } |
samux | 0:0cf0e27feaad | 228 | |
samux | 0:0cf0e27feaad | 229 | |
samux | 0:0cf0e27feaad | 230 | // Operators |
samux | 0:0cf0e27feaad | 231 | MbedJSONValue& MbedJSONValue::operator=(MbedJSONValue const& rhs) { |
MidnightCow | 6:f8bec9cada34 | 232 | //printf("=json\r\n"); |
samux | 0:0cf0e27feaad | 233 | if (this != &rhs) { |
samux | 0:0cf0e27feaad | 234 | clean(); |
samux | 0:0cf0e27feaad | 235 | _type = rhs._type; |
samux | 0:0cf0e27feaad | 236 | switch (_type) { |
samux | 0:0cf0e27feaad | 237 | case TypeBoolean: |
samux | 0:0cf0e27feaad | 238 | _value.asBool = rhs._value.asBool; |
samux | 0:0cf0e27feaad | 239 | break; |
samux | 0:0cf0e27feaad | 240 | case TypeInt: |
samux | 0:0cf0e27feaad | 241 | _value.asInt = rhs._value.asInt; |
samux | 0:0cf0e27feaad | 242 | break; |
samux | 0:0cf0e27feaad | 243 | case TypeDouble: |
samux | 0:0cf0e27feaad | 244 | _value.asDouble = rhs._value.asDouble; |
samux | 0:0cf0e27feaad | 245 | break; |
samux | 0:0cf0e27feaad | 246 | case TypeString: |
MidnightCow | 6:f8bec9cada34 | 247 | //printf("=json=%s\r\n",rhs._value.asString->c_str()); |
samux | 0:0cf0e27feaad | 248 | _value.asString = new std::string(*rhs._value.asString); |
samux | 0:0cf0e27feaad | 249 | break; |
samux | 0:0cf0e27feaad | 250 | case TypeArray: |
samux | 0:0cf0e27feaad | 251 | for (int i = 0; i < rhs.index_array; i++) |
samux | 0:0cf0e27feaad | 252 | (*this)[i] = rhs[i]; |
samux | 0:0cf0e27feaad | 253 | case TypeObject: |
samux | 0:0cf0e27feaad | 254 | for (int i = 0; i < rhs.index_token; i++) |
samux | 0:0cf0e27feaad | 255 | (*this)[*(rhs.token_name[i])] = rhs[*(rhs.token_name[i])]; |
samux | 0:0cf0e27feaad | 256 | default: |
samux | 0:0cf0e27feaad | 257 | break; |
samux | 0:0cf0e27feaad | 258 | } |
samux | 0:0cf0e27feaad | 259 | } |
samux | 0:0cf0e27feaad | 260 | return *this; |
samux | 0:0cf0e27feaad | 261 | } |
samux | 0:0cf0e27feaad | 262 | |
samux | 0:0cf0e27feaad | 263 | |
samux | 0:0cf0e27feaad | 264 | // Works for strings, arrays, and structs. |
samux | 0:0cf0e27feaad | 265 | int MbedJSONValue::size() const { |
samux | 0:0cf0e27feaad | 266 | switch (_type) { |
samux | 0:0cf0e27feaad | 267 | case TypeString: |
samux | 0:0cf0e27feaad | 268 | return int(_value.asString->size()); |
samux | 0:0cf0e27feaad | 269 | case TypeArray: |
samux | 0:0cf0e27feaad | 270 | return index_array; |
samux | 0:0cf0e27feaad | 271 | case TypeObject: |
samux | 0:0cf0e27feaad | 272 | return index_token; |
samux | 0:0cf0e27feaad | 273 | default: |
samux | 0:0cf0e27feaad | 274 | break; |
samux | 0:0cf0e27feaad | 275 | } |
samux | 0:0cf0e27feaad | 276 | return -1; |
samux | 0:0cf0e27feaad | 277 | } |
samux | 0:0cf0e27feaad | 278 | |
MidnightCow | 6:f8bec9cada34 | 279 |