| 326 | } |
| 327 | |
| 328 | static void print_json(const char *str, const jsmntok_t *tok, const char *indent) |
| 329 | { |
| 330 | size_t i; |
| 331 | const jsmntok_t *t; |
| 332 | bool first; |
| 333 | char next_indent[strlen(indent) + 3 + 1]; |
| 334 | |
| 335 | memset(next_indent, ' ', sizeof(next_indent)-1); |
| 336 | next_indent[sizeof(next_indent)-1] = '\0'; |
| 337 | |
| 338 | switch (tok->type) { |
| 339 | case JSMN_PRIMITIVE: |
| 340 | case JSMN_STRING: |
| 341 | printf("%.*s", json_tok_full_len(tok), json_tok_full(str, tok)); |
| 342 | return; |
| 343 | |
| 344 | case JSMN_ARRAY: |
| 345 | first = true; |
| 346 | json_for_each_arr(i, t, tok) { |
| 347 | if (first) |
| 348 | printf("[\n%s", next_indent); |
| 349 | else |
| 350 | printf(",\n%s", next_indent); |
| 351 | print_json(str, t, next_indent); |
| 352 | first = false; |
| 353 | } |
| 354 | if (first) |
| 355 | printf("[]"); |
| 356 | else |
| 357 | printf("\n%s]", indent); |
| 358 | return; |
| 359 | |
| 360 | case JSMN_OBJECT: |
| 361 | first = true; |
| 362 | json_for_each_obj(i, t, tok) { |
| 363 | if (first) |
| 364 | printf("{\n%s", next_indent); |
| 365 | else |
| 366 | printf(",\n%s", next_indent); |
| 367 | print_json(str, t, next_indent); |
| 368 | printf(": "); |
| 369 | print_json(str, t + 1, next_indent); |
| 370 | first = false; |
| 371 | } |
| 372 | if (first) |
| 373 | printf("{}"); |
| 374 | else |
| 375 | printf("\n%s}", indent); |
| 376 | return; |
| 377 | case JSMN_UNDEFINED: |
| 378 | break; |
| 379 | } |
| 380 | abort(); |
| 381 | } |
| 382 | |
| 383 | /* Always returns a positive number < len. len must be > 0! */ |
| 384 | static size_t read_nofail(int fd, void *buf, size_t len) |
no test coverage detected