Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/test_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ def test_encode_primitive_array():
result = encode(data)
assert result == 'items: [hello,"world, test",foo]'

def test_encode_tuple_array():
"""Test encoding of primitive arrays."""
# Number tuple
assert encode({'numbers': (1, 2, 3)}) == 'numbers: [1,2,3]'

# String tuple
assert encode({'names': ('Alice', 'Bob')}) == 'names: [Alice,Bob]'

# Mixed primitive tuple
assert encode({'mixed': (1, 'text', True, None)}) == 'mixed: [1,text,true,null]'


def test_encode_array_delimiter():
"""Test different array delimiters."""
Expand Down
16 changes: 14 additions & 2 deletions toon/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ def _encode_value(value: Any, level: int, opts: EncoderOptions) -> str:
return _encode_array(value, level, opts)
elif isinstance(value, dict):
return _encode_object(value, level, opts)
elif isinstance(value, tuple):
return _encode_tuple(value)
else:
# Handle other types as null
return 'null'
# Handle other types with NotImplementedError
raise NotImplementedError(f'Encoding for type {type(value)} is not implemented.')


def _encode_object(obj: dict, level: int, opts: EncoderOptions) -> str:
Expand Down Expand Up @@ -177,6 +179,16 @@ def _encode_array(arr: list, level: int, opts: EncoderOptions) -> str:
# Mixed array (list format)
return _encode_list_array(arr, level, opts, key=None)

def _encode_tuple(value: tuple) -> str:
"""Encode a tuple."""
if not value:
return '[]'

tuple_data = ','.join(_encode_primitive_value(v) for v in value)
tuple_string = f'[{tuple_data}]'

return tuple_string


def _encode_array_with_key(key: str, arr: list, level: int, opts: EncoderOptions) -> str:
"""Encode an array with its key prefix for object context."""
Expand Down