Render an object to text. */
| 549 | |
| 550 | /* Render an object to text. */ |
| 551 | static char *print_object(cJSON *item,int depth,int fmt,printbuffer *p) |
| 552 | { |
| 553 | char **entries=0,**names=0; |
| 554 | char *out=0,*ptr,*ret,*str;int len=7,i=0,j; |
| 555 | cJSON *child=item->child; |
| 556 | int numentries=0,fail=0; |
| 557 | size_t tmplen=0; |
| 558 | /* Count the number of entries. */ |
| 559 | while (child) numentries++,child=child->next; |
| 560 | /* Explicitly handle empty object case */ |
| 561 | if (!numentries) |
| 562 | { |
| 563 | if (p) out=ensure(p,fmt?depth+4:3); |
| 564 | else out=(char*)cJSON_malloc(fmt?depth+4:3); |
| 565 | if (!out) return 0; |
| 566 | ptr=out;*ptr++='{'; |
| 567 | if (fmt) {*ptr++='\n';for (i=0;i<depth-1;i++) *ptr++='\t';} |
| 568 | *ptr++='}';*ptr++=0; |
| 569 | return out; |
| 570 | } |
| 571 | if (p) |
| 572 | { |
| 573 | /* Compose the output: */ |
| 574 | i=p->offset; |
| 575 | len=fmt?2:1; ptr=ensure(p,len+1); if (!ptr) return 0; |
| 576 | *ptr++='{'; if (fmt) *ptr++='\n'; *ptr=0; p->offset+=len; |
| 577 | child=item->child;depth++; |
| 578 | while (child) |
| 579 | { |
| 580 | if (fmt) |
| 581 | { |
| 582 | ptr=ensure(p,depth); if (!ptr) return 0; |
| 583 | for (j=0;j<depth;j++) *ptr++='\t'; |
| 584 | p->offset+=depth; |
| 585 | } |
| 586 | print_string_ptr(child->string,p); |
| 587 | p->offset=update(p); |
| 588 | |
| 589 | len=fmt?2:1; |
| 590 | ptr=ensure(p,len); if (!ptr) return 0; |
| 591 | *ptr++=':';if (fmt) *ptr++='\t'; |
| 592 | p->offset+=len; |
| 593 | |
| 594 | print_value(child,depth,fmt,p); |
| 595 | p->offset=update(p); |
| 596 | |
| 597 | len=(fmt?1:0)+(child->next?1:0); |
| 598 | ptr=ensure(p,len+1); if (!ptr) return 0; |
| 599 | if (child->next) *ptr++=','; |
| 600 | if (fmt) *ptr++='\n';*ptr=0; |
| 601 | p->offset+=len; |
| 602 | child=child->next; |
| 603 | } |
| 604 | ptr=ensure(p,fmt?(depth+1):2); if (!ptr) return 0; |
| 605 | if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t'; |
| 606 | *ptr++='}';*ptr=0; |
| 607 | out=(p->buffer)+i; |
| 608 | } |
no test coverage detected