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