src/json.c

changeset 1601
e47078681a6f
parent 1600
a1a728d7ebfd
child 1602
7329821fd768
equal deleted inserted replaced
1600:a1a728d7ebfd 1601:e47078681a6f
408 408
409 if (all_printable && escape) { 409 if (all_printable && escape) {
410 size_t capa = str.length + 32; 410 size_t capa = str.length + 32;
411 char *space = cxMallocDefault(capa); 411 char *space = cxMallocDefault(capa);
412 if (space == NULL) return cx_mutstrn(NULL, 0); 412 if (space == NULL) return cx_mutstrn(NULL, 0);
413 cxBufferInit(&buf, space, capa, NULL, CX_BUFFER_AUTO_EXTEND); 413 cxBufferInit(&buf, NULL, space, capa, CX_BUFFER_AUTO_EXTEND);
414 cxBufferWrite(str.ptr, 1, i, &buf); 414 cxBufferWrite(str.ptr, 1, i, &buf);
415 all_printable = false; 415 all_printable = false;
416 } 416 }
417 if (escape) { 417 if (escape) {
418 cxBufferPut(&buf, '\\'); 418 cxBufferPut(&buf, '\\');
573 int cxJsonFilln(CxJson *json, const char *buf, size_t size) { 573 int cxJsonFilln(CxJson *json, const char *buf, size_t size) {
574 if (cxBufferEof(&json->buffer)) { 574 if (cxBufferEof(&json->buffer)) {
575 // reinitialize the buffer 575 // reinitialize the buffer
576 cxBufferDestroy(&json->buffer); 576 cxBufferDestroy(&json->buffer);
577 if (buf == NULL) buf = ""; // buffer must not be initialized with NULL 577 if (buf == NULL) buf = ""; // buffer must not be initialized with NULL
578 cxBufferInit(&json->buffer, (char*) buf, size, 578 cxBufferInit(&json->buffer, NULL, (char*) buf,
579 NULL, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_COPY_ON_WRITE); 579 size, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_COPY_ON_WRITE);
580 json->buffer.size = size; 580 json->buffer.size = size;
581 return 0; 581 return 0;
582 } else { 582 } else {
583 return size != cxBufferAppend(buf, 1, size, &json->buffer); 583 return size != cxBufferAppend(buf, 1, size, &json->buffer);
584 } 584 }
1422 } 1422 }
1423 1423
1424 static cxmutstr cx_json_to_string(CxJsonValue *value, const CxAllocator *allocator, CxJsonWriter *writer) { 1424 static cxmutstr cx_json_to_string(CxJsonValue *value, const CxAllocator *allocator, CxJsonWriter *writer) {
1425 if (allocator == NULL) allocator = cxDefaultAllocator; 1425 if (allocator == NULL) allocator = cxDefaultAllocator;
1426 CxBuffer buffer; 1426 CxBuffer buffer;
1427 if (cxBufferInit(&buffer, NULL, 128, allocator, 1427 if (cxBufferInit(&buffer, allocator, NULL, 128,
1428 CX_BUFFER_AUTO_EXTEND | CX_BUFFER_DO_NOT_FREE)) { 1428 CX_BUFFER_AUTO_EXTEND | CX_BUFFER_DO_NOT_FREE)) {
1429 return (cxmutstr){NULL, 0}; 1429 return (cxmutstr){NULL, 0};
1430 } 1430 }
1431 if (cx_json_write_rec(&buffer, value, cxBufferWriteFunc, writer, 0) 1431 if (cx_json_write_rec(&buffer, value, cxBufferWriteFunc, writer, 0)
1432 || cxBufferTerminate(&buffer)) { 1432 || cxBufferTerminate(&buffer)) {
1433 // LCOV_EXCL_START 1433 // LCOV_EXCL_START

mercurial