1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
/******************************************************************************
* Copyright (c) 2000-2021 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
*
* Contributors:
* Balasko, Jeno
* Baranyi, Botond
* Szabo, Bence Janos
*
******************************************************************************/
#include <cstring>
#include "JSON_Tokenizer.hh"
#include "memory.h"
#include <cstdio>
static const char TABS[] =
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
const size_t MAX_TABS = sizeof(TABS) - 1; // 64
void JSON_Tokenizer::init(const char* p_buf, const size_t p_buf_len)
{
if (p_buf != 0 && p_buf_len != 0) {
buf_ptr = mcopystrn(p_buf, p_buf_len);
} else {
buf_ptr = 0;
}
buf_len = p_buf_len;
buf_pos = 0;
depth = 0;
previous_token = JSON_TOKEN_NONE;
}
JSON_Tokenizer::~JSON_Tokenizer()
{
Free(buf_ptr);
}
void JSON_Tokenizer::put_c(const char c)
{
buf_ptr = mputprintf(buf_ptr, "%c", c);
++buf_len;
}
void JSON_Tokenizer::put_s(const char* s)
{
buf_ptr = mputstr(buf_ptr, s);
buf_len += strlen(s);
}
void JSON_Tokenizer::put_depth()
{
put_s(TABS + ((depth > MAX_TABS) ? 0 : MAX_TABS - depth));
}
bool JSON_Tokenizer::skip_white_spaces()
{
while(buf_pos < buf_len) {
switch(buf_ptr[buf_pos]) {
case ' ':
case '\r':
case '\n':
case '\t':
case '\f':
++buf_pos;
break;
default:
return true;
}
}
return false;
}
bool JSON_Tokenizer::check_for_string()
{
if ('\"' == buf_ptr[buf_pos]) {
++buf_pos;
} else {
return false;
}
while (buf_pos < buf_len) {
if ('\"' == buf_ptr[buf_pos]) {
return true;
}
else if ('\\' == buf_ptr[buf_pos]) {
// skip escaped character (so escaped quotes (\") are not mistaken for the ending quotes)
++buf_pos;
}
++buf_pos;
}
return false;
}
bool JSON_Tokenizer::check_for_number(bool* is_float /* = NULL */)
{
bool first_digit = false; // first non-zero digit reached
bool zero = false; // first zero digit reached
bool decimal_point = false; // decimal point (.) reached
bool exponent_mark = false; // exponential mark (e or E) reached
bool exponent_sign = false; // sign of the exponential (- or +) reached
if ('-' == buf_ptr[buf_pos]) {
++buf_pos;
}
while (buf_pos < buf_len) {
switch(buf_ptr[buf_pos]) {
case '.':
if (decimal_point || exponent_mark || (!first_digit && !zero)) {
return false;
}
decimal_point = true;
first_digit = false;
zero = false;
break;
case 'e':
case 'E':
if (exponent_mark || (!first_digit && !zero)) {
return false;
}
exponent_mark = true;
first_digit = false;
zero = false;
break;
case '0':
if (!first_digit && (exponent_mark || (!decimal_point && zero))) {
return false;
}
zero = true;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (!first_digit && zero && (!decimal_point || exponent_mark)) {
return false;
}
first_digit = true;
break;
case '-':
case '+':
if (exponent_sign || !exponent_mark || zero || first_digit) {
return false;
}
exponent_sign = true;
break;
default:
if (is_float != NULL) {
*is_float = decimal_point || exponent_mark;
}
return first_digit || zero;
}
++buf_pos;
}
if (is_float != NULL) {
*is_float = decimal_point || exponent_mark;
}
return first_digit || zero;
}
bool JSON_Tokenizer::check_for_separator()
{
if (buf_pos < buf_len) {
switch(buf_ptr[buf_pos]) {
case ',':
++buf_pos;
// no break
case ':':
case '{':
case '}':
case '[':
case ']':
return true;
default:
return false;
}
}
return true;
}
bool JSON_Tokenizer::check_for_literal(const char* p_literal)
{
size_t len = strlen(p_literal);
if (buf_len - buf_pos >= len &&
0 == strncmp(buf_ptr + buf_pos, p_literal, len)) {
size_t start_pos = buf_pos;
buf_pos += len;
if (!skip_white_spaces() || check_for_separator()) {
return true;
} else {
// must be followed by a separator (or only white spaces until the buffer ends) -> undo buffer action
buf_pos = start_pos;
}
}
return false;
}
size_t JSON_Tokenizer::get_next_token(json_token_t* p_token, char** p_token_str, size_t* p_str_len)
{
size_t start_pos = buf_pos;
*p_token = JSON_TOKEN_NONE;
if (0 != p_token_str && 0 != p_str_len) {
*p_token_str = 0;
*p_str_len = 0;
}
if (skip_white_spaces()) {
char c = buf_ptr[buf_pos];
switch (c) {
case '{':
case '[':
*p_token = ('{' == c) ? JSON_TOKEN_OBJECT_START : JSON_TOKEN_ARRAY_START;
++buf_pos;
break;
case '}':
case ']':
++buf_pos;
if (skip_white_spaces() && !check_for_separator()) {
// must be followed by a separator (or only white spaces until the buffer ends)
*p_token = JSON_TOKEN_ERROR;
} else {
*p_token = ('}' == c) ? JSON_TOKEN_OBJECT_END : JSON_TOKEN_ARRAY_END;
}
break;
case '\"': {
// string value or field name
size_t string_start_pos = buf_pos;
if(!check_for_string()) {
// invalid string value
*p_token = JSON_TOKEN_ERROR;
break;
}
size_t string_end_pos = ++buf_pos; // step over the string's ending quotes
if (skip_white_spaces() && ':' == buf_ptr[buf_pos]) {
// name token - don't include the starting and ending quotes
*p_token = JSON_TOKEN_NAME;
if (0 != p_token_str && 0 != p_str_len) {
*p_token_str = buf_ptr + string_start_pos + 1;
*p_str_len = string_end_pos - string_start_pos - 2;
}
++buf_pos;
} else if (check_for_separator()) {
// value token - include the starting and ending quotes
*p_token = JSON_TOKEN_STRING;
if (0 != p_token_str && 0 != p_str_len) {
*p_token_str = buf_ptr + string_start_pos;
*p_str_len = string_end_pos - string_start_pos;
}
} else {
// value token, but there is no separator after it -> error
*p_token = JSON_TOKEN_ERROR;
break;
}
break;
} // case: string value or field name
default:
if (('0' <= buf_ptr[buf_pos] && '9' >= buf_ptr[buf_pos]) ||
'-' == buf_ptr[buf_pos]) {
// number value
size_t number_start_pos = buf_pos;
if (!check_for_number()) {
// invalid number
*p_token = JSON_TOKEN_ERROR;
break;
}
size_t number_length = buf_pos - number_start_pos;
if (skip_white_spaces() && !check_for_separator()) {
// must be followed by a separator (or only white spaces until the buffer ends)
*p_token = JSON_TOKEN_ERROR;
break;
}
*p_token = JSON_TOKEN_NUMBER;
if (0 != p_token_str && 0 != p_str_len) {
*p_token_str = buf_ptr + number_start_pos;
*p_str_len = number_length;
}
break;
} // if (number value)
else if (check_for_literal("true")) {
*p_token = JSON_TOKEN_LITERAL_TRUE;
break;
}
else if (check_for_literal("false")) {
*p_token = JSON_TOKEN_LITERAL_FALSE;
break;
}
else if (check_for_literal("null")) {
*p_token = JSON_TOKEN_LITERAL_NULL;
break;
}
else {
*p_token = JSON_TOKEN_ERROR;
break;
}
} // switch (current char)
} // if (skip_white_spaces())
return buf_pos - start_pos;
}
void JSON_Tokenizer::put_separator()
{
if (JSON_TOKEN_NAME != previous_token && JSON_TOKEN_NONE != previous_token &&
JSON_TOKEN_ARRAY_START != previous_token && JSON_TOKEN_OBJECT_START != previous_token) {
put_c(',');
if (pretty) {
put_c('\n');
put_depth();
}
}
}
int JSON_Tokenizer::put_next_token(json_token_t p_token, const char* p_token_str)
{
size_t start_len = buf_len;
switch(p_token) {
case JSON_TOKEN_OBJECT_START:
case JSON_TOKEN_ARRAY_START: {
put_separator();
put_c( (JSON_TOKEN_OBJECT_START == p_token) ? '{' : '[' );
if (pretty) {
put_c('\n');
++depth;
put_depth();
}
break;
}
case JSON_TOKEN_OBJECT_END:
case JSON_TOKEN_ARRAY_END: {
if (pretty) {
if (JSON_TOKEN_OBJECT_START != previous_token && JSON_TOKEN_ARRAY_START != previous_token) {
put_c('\n');
--depth;
put_depth();
} else if (MAX_TABS >= depth) {
// empty object or array -> remove the extra tab added at the start token
--depth;
--buf_len;
buf_ptr[buf_len] = 0;
}
}
put_c( (JSON_TOKEN_OBJECT_END == p_token) ? '}' : ']' );
break;
}
case JSON_TOKEN_NUMBER:
case JSON_TOKEN_STRING:
put_separator();
put_s(p_token_str);
break;
case JSON_TOKEN_LITERAL_TRUE:
put_separator();
put_s("true");
break;
case JSON_TOKEN_LITERAL_FALSE:
put_separator();
put_s("false");
break;
case JSON_TOKEN_LITERAL_NULL:
put_separator();
put_s("null");
break;
case JSON_TOKEN_NAME:
put_separator();
put_c('\"');
put_s(p_token_str);
if (pretty) {
put_s("\" : ");
} else {
put_s("\":");
}
break;
default:
return 0;
}
previous_token = p_token;
return buf_len - start_len;
}
void JSON_Tokenizer::put_raw_data(const char* p_data, size_t p_len)
{
buf_ptr = mputstrn(buf_ptr, p_data, p_len);
buf_len += p_len;
}
char* convert_to_json_string(const char* str)
{
char* ret_val = mcopystrn("\"", 1);
// control characters (like \n) cannot be placed in a JSON string, replace
// them with JSON metacharacters
// double quotes and backslashes need to be escaped, too
size_t str_len = strlen(str);
for (size_t i = 0; i < str_len; ++i) {
switch (str[i]) {
case '\n':
ret_val = mputstrn(ret_val, "\\n", 2);
break;
case '\r':
ret_val = mputstrn(ret_val, "\\r", 2);
break;
case '\t':
ret_val = mputstrn(ret_val, "\\t", 2);
break;
case '\f':
ret_val = mputstrn(ret_val, "\\f", 2);
break;
case '\b':
ret_val = mputstrn(ret_val, "\\b", 2);
break;
case '\"':
ret_val = mputstrn(ret_val, "\\\"", 2);
break;
case '\\':
ret_val = mputstrn(ret_val, "\\\\", 2);
break;
default:
if (str[i] < 32 && str[i] > 0) {
// use the JSON \uHHHH notation for other control characters
// (this is just for esthetic reasons, these wouldn't break the JSON
// string format)
ret_val = mputprintf(ret_val, "\\u00%d%c", str[i] / 16,
(str[i] % 16 < 10) ? (str[i] % 16 + '0') : (str[i] % 16 - 10 + 'A'));
}
else {
ret_val = mputc(ret_val, str[i]);
}
break;
}
}
return mputstrn(ret_val, "\"", 1);
}
|