Skip to content

Commit 8af4586

Browse files
committed
fix: remove trailing newlines from serialized output
1 parent 0589acd commit 8af4586

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/lib/serialize/encoders.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,4 +656,5 @@ fn writeIndentedListItem(
656656
}
657657
try writer.writeAll(LIST_ITEM_PREFIX);
658658
try writer.writeAll(content);
659+
try writer.writeByte('\n');
659660
}

src/lib/serialize/root.zig

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ pub fn stringify(
3030
else => |e| return e,
3131
};
3232

33-
return try list.toOwnedSlice();
33+
// Per SPEC §12: No trailing newline at the end of the document
34+
const slice = try list.toOwnedSlice();
35+
if (slice.len > 0 and slice[slice.len - 1] == '\n') {
36+
const trimmed = try allocator.dupe(u8, slice[0 .. slice.len - 1]);
37+
allocator.free(slice);
38+
return trimmed;
39+
}
40+
return slice;
3441
}
3542

3643
/// Stringifies a Value to TOON format.
@@ -49,7 +56,14 @@ pub fn stringifyValue(
4956
else => |e| return e,
5057
};
5158

52-
return try list.toOwnedSlice();
59+
// Per SPEC §12: No trailing newline at the end of the document
60+
const slice = try list.toOwnedSlice();
61+
if (slice.len > 0 and slice[slice.len - 1] == '\n') {
62+
const trimmed = try allocator.dupe(u8, slice[0 .. slice.len - 1]);
63+
allocator.free(slice);
64+
return trimmed;
65+
}
66+
return slice;
5367
}
5468

5569
/// Stringifies a std.json.Value to TOON format, writing to a writer.

0 commit comments

Comments
 (0)