Skip to content

Commit 69cc80e

Browse files
committed
Added to_json() helper to Chart base class.
1 parent 3cf5095 commit 69cc80e

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

immanuel/charts.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
1919
"""
2020

21+
import json
2122
from datetime import datetime
22-
from zoneinfo import ZoneInfo
23+
from typing import TypeVar
2324

2425
from immanuel.classes import wrap
2526
from immanuel.classes.localize import localize as _
27+
from immanuel.classes.serialize import ToJSON
2628
from immanuel.const import calc, chart, names
2729
from immanuel.reports import aspect, dignity, pattern, weighting
2830
from immanuel.setup import settings
@@ -36,6 +38,9 @@
3638
)
3739

3840

41+
ChartType = TypeVar("ChartType", bound="Chart")
42+
43+
3944
class Subject:
4045
"""Simple class to model a chart subject - essentially just
4146
a time and place."""
@@ -71,7 +76,7 @@ class Chart:
7176
"""Base chart class. This acts as an abstract class for the actual chart
7277
classes to inherit from."""
7378

74-
def __init__(self, type: int, aspects_to: "Chart" = None) -> None:
79+
def __init__(self, type: int, aspects_to: ChartType | None = None) -> None:
7580
self.type = _(names.CHART_TYPES[type])
7681
self._type = type
7782
self._aspects_to = aspects_to
@@ -217,6 +222,9 @@ def set_wrapped_weightings(self) -> None:
217222
quadrants=weighting.quadrants(self._objects, self._houses),
218223
)
219224

225+
def to_json(self, **kwargs) -> str:
226+
return json.dumps(self, cls=ToJSON, **kwargs)
227+
220228

221229
class Natal(Chart):
222230
"""Standard natal chart generates data straight from the passed

immanuel/classes/serialize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
class ToJSON(JSONEncoder):
1414
def default(self, obj) -> dict | str | None:
15-
if hasattr(obj, "to_json"):
16-
return obj.to_json()
15+
if hasattr(obj, "__json__"):
16+
return obj.__json__()
1717

1818
if hasattr(obj, "__dict__"):
1919
return {k: v for k, v in obj.__dict__.items() if k[0] != "_"}

0 commit comments

Comments
 (0)