Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serialisation of python datetime without tzinfo #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions crates/target_python/output/basic_discriminator/__init__.py
Original file line number Diff line number Diff line change
@@ -71,6 +71,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/basic_enum/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/basic_properties/__init__.py
Original file line number Diff line number Diff line change
@@ -47,6 +47,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/custom_overrides/__init__.py
Original file line number Diff line number Diff line change
@@ -53,6 +53,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
Original file line number Diff line number Diff line change
@@ -78,6 +78,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/description/__init__.py
Original file line number Diff line number Diff line change
@@ -176,6 +176,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
Original file line number Diff line number Diff line change
@@ -59,6 +59,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/elements/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
Original file line number Diff line number Diff line change
@@ -122,6 +122,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
Original file line number Diff line number Diff line change
@@ -39,6 +39,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
Original file line number Diff line number Diff line change
@@ -59,6 +59,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/enum_collisions/__init__.py
Original file line number Diff line number Diff line change
@@ -77,6 +77,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/geojson/__init__.py
Original file line number Diff line number Diff line change
@@ -523,6 +523,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/initialisms/__init__.py
Original file line number Diff line number Diff line change
@@ -71,6 +71,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/keywords/__init__.py
Original file line number Diff line number Diff line change
@@ -63,6 +63,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
Original file line number Diff line number Diff line change
@@ -82,6 +82,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/nullable_elements/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/nullable_enum/__init__.py
Original file line number Diff line number Diff line change
@@ -46,6 +46,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/nullable_properties/__init__.py
Original file line number Diff line number Diff line change
@@ -58,6 +58,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/nullable_references/__init__.py
Original file line number Diff line number Diff line change
@@ -119,6 +119,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
Original file line number Diff line number Diff line change
@@ -38,6 +38,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/optional_properties/__init__.py
Original file line number Diff line number Diff line change
@@ -47,6 +47,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
Original file line number Diff line number Diff line change
@@ -41,6 +41,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/reference/__init__.py
Original file line number Diff line number Diff line change
@@ -67,6 +67,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_boolean/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_empty/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_float32/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_float64/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_int16/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_int32/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_int8/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_nullable_string/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_string/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_timestamp/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_uint16/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_uint32/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/root_uint8/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/type_collisions/__init__.py
Original file line number Diff line number Diff line change
@@ -86,6 +86,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/output/values/__init__.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ def _to_json_data(data: Any) -> Any:
if data is None or type(data) in [bool, int, float, str, object]:
return data
if type(data) is datetime:
if data.tzinfo == None:
return data.replace(tzinfo=timezone.utc).isoformat()
return data.isoformat()
if type(data) is list:
return [_to_json_data(d) for d in data]
2 changes: 2 additions & 0 deletions crates/target_python/src/lib.rs
Original file line number Diff line number Diff line change
@@ -227,6 +227,8 @@ impl jtd_codegen::target::Target for Target {
)?;
writeln!(out, " return data")?;
writeln!(out, " if type(data) is datetime:")?;
writeln!(out, " if data.tzinfo == None:")?;
writeln!(out, " return data.replace(tzinfo=timezone.utc).isoformat()")?;
writeln!(out, " return data.isoformat()")?;
writeln!(out, " if type(data) is list:")?;
writeln!(out, " return [_to_json_data(d) for d in data]")?;