Encode a value at a given indentation level.
(value: Any, level: int, opts: EncoderOptions)
| 82 | |
| 83 | |
| 84 | def _encode_value(value: Any, level: int, opts: EncoderOptions) -> str: |
| 85 | """Encode a value at a given indentation level.""" |
| 86 | if value is None: |
| 87 | return 'null' |
| 88 | elif isinstance(value, bool): |
| 89 | return 'true' if value else 'false' |
| 90 | elif isinstance(value, datetime): |
| 91 | # Convert datetime to ISO 8601 string |
| 92 | iso_string = value.isoformat() |
| 93 | if needs_quoting(iso_string): |
| 94 | return quote_string(iso_string) |
| 95 | return iso_string |
| 96 | elif isinstance(value, date): |
| 97 | # Convert date to ISO 8601 date string |
| 98 | iso_string = value.isoformat() |
| 99 | if needs_quoting(iso_string): |
| 100 | return quote_string(iso_string) |
| 101 | return iso_string |
| 102 | elif isinstance(value, (int, float)): |
| 103 | # Handle special float values |
| 104 | if isinstance(value, float): |
| 105 | if value != value: # NaN |
| 106 | return 'null' |
| 107 | elif value == float('inf') or value == float('-inf'): |
| 108 | return 'null' |
| 109 | # Use format_float to suppress scientific notation |
| 110 | return format_float(value) |
| 111 | return str(value) |
| 112 | elif isinstance(value, str): |
| 113 | if needs_quoting(value): |
| 114 | return quote_string(value) |
| 115 | return value |
| 116 | elif isinstance(value, list): |
| 117 | return _encode_array(value, level, opts) |
| 118 | elif isinstance(value, dict): |
| 119 | return _encode_object(value, level, opts) |
| 120 | elif isinstance(value, tuple): |
| 121 | return _encode_tuple(value) |
| 122 | else: |
| 123 | # Handle other types with NotImplementedError |
| 124 | raise NotImplementedError(f'Encoding for type {type(value)} is not implemented.') |
| 125 | |
| 126 | |
| 127 | def _encode_object(obj: dict, level: int, opts: EncoderOptions) -> str: |
no test coverage detected
searching dependent graphs…