Skip to content

Commit

Permalink
Fix missing indent for YYJSON_TYPE_RAW in prettify function: #178
Browse files Browse the repository at this point in the history
  • Loading branch information
ibireme committed Jul 25, 2024
1 parent dcd0d7a commit ce22cdb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
All notable changes to this project will be documented in this file.


## Unreleased
#### Fixed
- Fix some warnings when directly including yyjson.c: #177
- Fix missing indent for `YYJSON_TYPE_RAW` in prettify function: #178


## 0.10.0 (2024-07-09)
#### Added
- Add `yyjson_locate_pos()` function to locate the line and column number for error position: #166
Expand Down
8 changes: 6 additions & 2 deletions src/yyjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -8824,10 +8824,12 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root,
goto val_end;
}
if (val_type == YYJSON_TYPE_RAW) {
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
str_len = unsafe_yyjson_get_len(val);
str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
check_str_len(str_len);
incr_len(str_len + 3);
incr_len(str_len + 3 + (no_indent ? 0 : level * 4));
cur = write_indent(cur, no_indent ? 0 : level, spaces);
cur = write_raw(cur, str_ptr, str_len);
*cur++ = ',';
*cur++ = '\n';
Expand Down Expand Up @@ -9389,10 +9391,12 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root,
goto val_end;
}
if (val_type == YYJSON_TYPE_RAW) {
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
str_len = unsafe_yyjson_get_len(val);
str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
check_str_len(str_len);
incr_len(str_len + 3);
incr_len(str_len + 3 + (no_indent ? 0 : level * 4));
cur = write_indent(cur, no_indent ? 0 : level, spaces);
cur = write_raw(cur, str_ptr, str_len);
*cur++ = ',';
*cur++ = '\n';
Expand Down
13 changes: 13 additions & 0 deletions test/test_json_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,19 @@ static void test_json_write(yyjson_alc *alc) {
" ]\n"
"}");

root = yyjson_mut_obj(doc);
yyjson_mut_doc_set_root(doc, root);
val = yyjson_mut_arr(doc);
yyjson_mut_arr_add_val(val, yyjson_mut_raw(doc, "123"));
yyjson_mut_obj_add_val(doc, root, "a", val);
validate_json_write(doc, alc,
"{\"a\":[123]}",
"{\n"
" \"a\": [\n"
" 123\n"
" ]\n"
"}");

root = yyjson_mut_obj(doc);
yyjson_mut_doc_set_root(doc, root);
val = yyjson_mut_obj(doc);
Expand Down

0 comments on commit ce22cdb

Please sign in to comment.