src/json.c

changeset 940
bbf41b9c2658
parent 938
9d02bb5dcc3c
child 941
9077724b75a0
equal deleted inserted replaced
939:0bb7258366a0 940:bbf41b9c2658
425 p->states[p->nstates] = next_state_after_value(state); 425 p->states[p->nstates] = next_state_after_value(state);
426 p->value_ready = 1; 426 p->value_ready = 1;
427 switch (token.tokentype) { 427 switch (token.tokentype) {
428 case CX_JSON_TOKEN_BEGIN_ARRAY: { 428 case CX_JSON_TOKEN_BEGIN_ARRAY: {
429 p->reader_type = CX_JSON_READER_ARRAY_BEGIN; 429 p->reader_type = CX_JSON_READER_ARRAY_BEGIN;
430 if (add_state(p, JP_STATE_VALUE_BEGIN_AR)) return -1; 430 ret = add_state(p, JP_STATE_VALUE_BEGIN_AR) ? -1 : 1;
431 return 1; 431 break;
432 //return json_read(p);
433 } 432 }
434 case CX_JSON_TOKEN_BEGIN_OBJECT: { 433 case CX_JSON_TOKEN_BEGIN_OBJECT: {
435 p->reader_type = CX_JSON_READER_OBJECT_BEGIN; 434 p->reader_type = CX_JSON_READER_OBJECT_BEGIN;
436 if (add_state(p, JP_STATE_OBJ_NAME_OR_CLOSE)) return -1; 435 ret = add_state(p, JP_STATE_OBJ_NAME_OR_CLOSE) ? -1 : 1;
437 return 1; 436 break;
438 //return json_read(p);
439 } 437 }
440 case CX_JSON_TOKEN_END_ARRAY: { 438 case CX_JSON_TOKEN_END_ARRAY: {
441 p->value_ready = 0; 439 p->value_ready = 0;
442 end_elm(p, CX_JSON_READER_ARRAY_END); 440 end_elm(p, CX_JSON_READER_ARRAY_END);
443 break; 441 break;
452 cxmutstr str = unescape_string(token.content, token.length); 450 cxmutstr str = unescape_string(token.content, token.length);
453 if (str.ptr) { 451 if (str.ptr) {
454 p->value_str = str.ptr; 452 p->value_str = str.ptr;
455 p->value_str_len = str.length; 453 p->value_str_len = str.length;
456 } else { 454 } else {
457 return -1; 455 ret = -1;
458 } 456 }
459 break; 457 break;
460 } 458 }
461 case CX_JSON_TOKEN_INTEGER: { 459 case CX_JSON_TOKEN_INTEGER: {
462 p->reader_type = CX_JSON_READER_INTEGER; 460 p->reader_type = CX_JSON_READER_INTEGER;
463 int64_t value; 461 int64_t value;
464 if (parse_integer(token.content, token.length, &value)) { 462 if (parse_integer(token.content, token.length, &value)) {
465 return -1; 463 ret = -1;
466 } 464 } else {
467 p->value_int = value; 465 p->value_int = value;
468 p->value_double = (double) value; 466 p->value_double = (double) value;
467 }
469 break; 468 break;
470 } 469 }
471 case CX_JSON_TOKEN_NUMBER: { 470 case CX_JSON_TOKEN_NUMBER: {
472 p->reader_type = CX_JSON_READER_NUMBER; 471 p->reader_type = CX_JSON_READER_NUMBER;
473 double value; 472 double value;
474 if (parse_number(token.content, token.length, &value)) { 473 if (parse_number(token.content, token.length, &value)) {
475 return -1; 474 ret = -1;
476 } 475 } else {
477 p->value_double = value; 476 p->value_double = value;
478 p->value_int = (int64_t) value; 477 p->value_int = (int64_t) value;
478 }
479 break; 479 break;
480 } 480 }
481 case CX_JSON_TOKEN_LITERAL: { 481 case CX_JSON_TOKEN_LITERAL: {
482 p->reader_type = CX_JSON_READER_LITERAL; 482 p->reader_type = CX_JSON_READER_LITERAL;
483 break; 483 break;
484 } 484 }
485 default: 485 default: ret = -1;
486 return -1;
487 } 486 }
488 } else if (state == JP_STATE_ARRAY_SEP_OR_CLOSE) { 487 } else if (state == JP_STATE_ARRAY_SEP_OR_CLOSE) {
489 // expect ',' or ']' 488 // expect ',' or ']'
490 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) { 489 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) {
491 p->states[p->nstates] = JP_STATE_VALUE_BEGIN_AR; 490 p->states[p->nstates] = JP_STATE_VALUE_BEGIN_AR;
492 return json_read(p); 491 ret = json_read(p);
493 } else if (token.tokentype == CX_JSON_TOKEN_END_ARRAY) { 492 } else if (token.tokentype == CX_JSON_TOKEN_END_ARRAY) {
494 end_elm(p, CX_JSON_READER_ARRAY_END); 493 end_elm(p, CX_JSON_READER_ARRAY_END);
495 } else { 494 } else {
496 return -1; 495 ret = -1;
497 } 496 }
498 } else if (state == JP_STATE_OBJ_NAME_OR_CLOSE || state == JP_STATE_OBJ_NAME) { 497 } else if (state == JP_STATE_OBJ_NAME_OR_CLOSE || state == JP_STATE_OBJ_NAME) {
499 if (state == JP_STATE_OBJ_NAME_OR_CLOSE && token.tokentype == CX_JSON_TOKEN_END_OBJECT) { 498 if (state == JP_STATE_OBJ_NAME_OR_CLOSE && token.tokentype == CX_JSON_TOKEN_END_OBJECT) {
500 clear_valuename(p); 499 clear_valuename(p);
501 end_elm(p, CX_JSON_READER_OBJECT_END); 500 end_elm(p, CX_JSON_READER_OBJECT_END);
508 p->value_name = valname.ptr; 507 p->value_name = valname.ptr;
509 p->value_name_len = valname.length; 508 p->value_name_len = valname.length;
510 509
511 // next state 510 // next state
512 p->states[p->nstates] = JP_STATE_OBJ_COLON; 511 p->states[p->nstates] = JP_STATE_OBJ_COLON;
513 return json_read(p); 512 ret = json_read(p);
514 } 513 }
515 } else if (state == JP_STATE_OBJ_COLON) { 514 } else if (state == JP_STATE_OBJ_COLON) {
516 // expect ':' 515 // expect ':'
517 if (token.tokentype != CX_JSON_TOKEN_NAME_SEPARATOR) return -1; 516 if (token.tokentype != CX_JSON_TOKEN_NAME_SEPARATOR) return -1;
518 // next state 517 // next state
519 p->states[p->nstates] = 1; 518 p->states[p->nstates] = 1;
520 return json_read(p); 519 ret = json_read(p);
521 } else if (state == 7) { 520 } else if (state == 7) {
522 // expect ',' or '}]' 521 // expect ',' or '}]'
523 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) { 522 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) {
524 p->states[p->nstates] = JP_STATE_OBJ_NAME; 523 p->states[p->nstates] = JP_STATE_OBJ_NAME;
525 return json_read(p); 524 ret = json_read(p);
526 } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) { 525 } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) {
527 end_elm(p, CX_JSON_READER_OBJECT_END); 526 end_elm(p, CX_JSON_READER_OBJECT_END);
528 } else { 527 } else {
529 return -1; 528 ret = -1;
530 } 529 }
530 }
531
532 if (token.alloc > 0) {
533 free((char*)token.content);
531 } 534 }
532 535
533 return ret; 536 return ret;
534 } 537 }
535 538

mercurial