diff --git a/src/toon_format/encoders.py b/src/toon_format/encoders.py index 5d1022e..67b5056 100644 --- a/src/toon_format/encoders.py +++ b/src/toon_format/encoders.py @@ -447,9 +447,9 @@ def encode_object_as_list_item( # Now encode the array content at depth + 1 encode_array_content(first_arr, options, writer, depth + 1) else: - # If first value is an object, put "-" alone then encode normally - writer.push(depth, LIST_ITEM_PREFIX.rstrip()) - encode_key_value_pair(first_key, first_value, options, writer, depth + 1) + # If first value is an object, put "- key:" then content at depth + 2 + writer.push(depth, f"{LIST_ITEM_PREFIX}{first_key}:") + encode_value(first_value, options, writer, depth + 2) # Rest of the keys go normally indented for key, value in keys[1:]: diff --git a/tests/fixtures/encode/arrays-nested.json b/tests/fixtures/encode/arrays-nested.json index c7c47a4..a3b6a75 100644 --- a/tests/fixtures/encode/arrays-nested.json +++ b/tests/fixtures/encode/arrays-nested.json @@ -94,6 +94,14 @@ }, "expected": "items[2]:\n - a: 1\n - [2]: 1,2", "specSection": "7.3" + }, + { + "name": "encodes array of nested objects", + "input": { + "items": [{ "a": { "b": 1 }}, { "c": 2 }] + }, + "expected": "items[2]:\n - a:\n b: 1\n - c: 2", + "specSection": "10" } ] }