src/json.c

changeset 941
9077724b75a0
parent 940
bbf41b9c2658
child 942
8a5bbdb7f87f
equal deleted inserted replaced
940:bbf41b9c2658 941:9077724b75a0
438 case CX_JSON_TOKEN_END_ARRAY: { 438 case CX_JSON_TOKEN_END_ARRAY: {
439 p->value_ready = 0; 439 p->value_ready = 0;
440 end_elm(p, CX_JSON_READER_ARRAY_END); 440 end_elm(p, CX_JSON_READER_ARRAY_END);
441 break; 441 break;
442 } 442 }
443 case CX_JSON_TOKEN_END_OBJECT: {
444 p->value_ready = 0;
445 end_elm(p, CX_JSON_READER_OBJECT_END);
446 break;
447 }
448 case CX_JSON_TOKEN_STRING: { 443 case CX_JSON_TOKEN_STRING: {
449 p->reader_type = CX_JSON_READER_STRING; 444 p->reader_type = CX_JSON_READER_STRING;
450 cxmutstr str = unescape_string(token.content, token.length); 445 cxmutstr str = unescape_string(token.content, token.length);
451 if (str.ptr) { 446 if (str.ptr) {
452 p->value_str = str.ptr; 447 p->value_str = str.ptr;
516 if (token.tokentype != CX_JSON_TOKEN_NAME_SEPARATOR) return -1; 511 if (token.tokentype != CX_JSON_TOKEN_NAME_SEPARATOR) return -1;
517 // next state 512 // next state
518 p->states[p->nstates] = 1; 513 p->states[p->nstates] = 1;
519 ret = json_read(p); 514 ret = json_read(p);
520 } else if (state == 7) { 515 } else if (state == 7) {
521 // expect ',' or '}]' 516 // expect ',' or '}'
522 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) { 517 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) {
523 p->states[p->nstates] = JP_STATE_OBJ_NAME; 518 p->states[p->nstates] = JP_STATE_OBJ_NAME;
524 ret = json_read(p); 519 ret = json_read(p);
525 } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) { 520 } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) {
526 end_elm(p, CX_JSON_READER_OBJECT_END); 521 end_elm(p, CX_JSON_READER_OBJECT_END);
672 } 667 }
673 668
674 void cxJsonDestroy(CxJson *p) { 669 void cxJsonDestroy(CxJson *p) {
675 free(p->states); 670 free(p->states);
676 free(p->readvalue_stack); 671 free(p->readvalue_stack);
672 cxJsonValueFree(p->read_value);
677 } 673 }
678 674
679 void cxJsonFill(CxJson *p, const char *buf, size_t size) { 675 void cxJsonFill(CxJson *p, const char *buf, size_t size) {
680 // TODO: implement rescue buffer like in CxProperties to allow subsequent fills 676 // TODO: implement rescue buffer like in CxProperties to allow subsequent fills
681 p->buffer = buf; 677 p->buffer = buf;
697 CxJsonValue *v = calloc(1, sizeof(CxJsonValue)); 693 CxJsonValue *v = calloc(1, sizeof(CxJsonValue));
698 if (!v) return -1; 694 if (!v) return -1;
699 695
700 if (p->readvalue_nelm > 0) { 696 if (p->readvalue_nelm > 0) {
701 if (add_to_parent(p, p->readvalue_stack[p->readvalue_nelm - 1], v)) { 697 if (add_to_parent(p, p->readvalue_stack[p->readvalue_nelm - 1], v)) {
698 free(v);
702 return -1; 699 return -1;
703 } 700 }
704 } else { 701 } else {
705 // set this value as root 702 // set this value as root
706 p->read_value = v; 703 p->read_value = v;

mercurial