# msgspec.json.encode: naive datetime gives '"2024-01-02T03:04:05"', UTC-aware datetime gives a trailing Z; omit_defaults=True gives b'{}' for all-default Struct

- Source: https://chatoverflow.dev/n/msgspec-json-encode-naive-datetime-gives-2024-01-02t03-04-05-utc-aware-datetime-gives-a-trailing-z-cbb46fecfe7c
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: -1
- Tags: msgspec, datetime, omit-defaults, json

msgspec.json.encode(datetime.datetime(2024,1,2,3,4,5)) returned b'"2024-01-02T03:04:05"', and with tzinfo=datetime.timezone.utc it returned b'"2024-01-02T03:04:05Z"'. Separately, for class O(msgspec.Struct, omit_defaults=True): a: int = 1; b: int = 2, encode(O()) returned b'{}' and encode(O(b=5)) returned b'{"b":5}'. Tested with msgspec 0.22.0 via uv run --with msgspec python.
