Encode a primitive value for use in arrays.
(value: Any)
| 272 | |
| 273 | |
| 274 | def _encode_primitive_value(value: Any) -> str: |
| 275 | """Encode a primitive value for use in arrays.""" |
| 276 | if value is None: |
| 277 | return 'null' |
| 278 | elif isinstance(value, bool): |
| 279 | return 'true' if value else 'false' |
| 280 | elif isinstance(value, datetime): |
| 281 | # Convert datetime to ISO 8601 string |
| 282 | iso_string = value.isoformat() |
| 283 | if needs_quoting(iso_string): |
| 284 | return quote_string(iso_string) |
| 285 | return iso_string |
| 286 | elif isinstance(value, date): |
| 287 | # Convert date to ISO 8601 date string |
| 288 | iso_string = value.isoformat() |
| 289 | if needs_quoting(iso_string): |
| 290 | return quote_string(iso_string) |
| 291 | return iso_string |
| 292 | elif isinstance(value, (int, float)): |
| 293 | if isinstance(value, float): |
| 294 | if value != value or value == float('inf') or value == float('-inf'): |
| 295 | return 'null' |
| 296 | # Use format_float to suppress scientific notation |
| 297 | return format_float(value) |
| 298 | return str(value) |
| 299 | elif isinstance(value, str): |
| 300 | if needs_quoting(value): |
| 301 | return quote_string(value) |
| 302 | return value |
| 303 | else: |
| 304 | return 'null' |
| 305 | |
| 306 | |
| 307 | def _encode_list_array(arr: list, level: int, opts: EncoderOptions, key: Optional[str] = None) -> str: |
no test coverage detected
searching dependent graphs…