Skip to content

Commit 389e564

Browse files
committed
fix: Refined encoding logic for array of nested objects
1 parent 9c4f0c0 commit 389e564

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/toon_format/encoders.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,9 @@ def encode_object_as_list_item(
447447
# Now encode the array content at depth + 1
448448
encode_array_content(first_arr, options, writer, depth + 1)
449449
else:
450-
# If first value is an object, put "-" alone then encode normally
451-
writer.push(depth, LIST_ITEM_PREFIX.rstrip())
452-
encode_key_value_pair(first_key, first_value, options, writer, depth + 1)
450+
# If first value is an object, put "- key:" then content at depth + 2
451+
writer.push(depth, f"{LIST_ITEM_PREFIX}{first_key}:")
452+
encode_value(first_value, options, writer, depth + 2)
453453

454454
# Rest of the keys go normally indented
455455
for key, value in keys[1:]:

tests/fixtures/encode/arrays-nested.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@
9494
},
9595
"expected": "items[2]:\n - a: 1\n - [2]: 1,2",
9696
"specSection": "7.3"
97+
},
98+
{
99+
"name": "encodes array of nested objects",
100+
"input": {
101+
"items": [{ "a": { "b": 1 }}, { "c": 2 }]
102+
},
103+
"expected": "items[2]:\n - a:\n b: 1\n - c: 2",
104+
"specSection": "10"
97105
}
98106
]
99107
}

0 commit comments

Comments
 (0)