import json
from q2_sdk.core.configuration import EnvVar
[docs]
class RecursiveEncoder(json.JSONEncoder):
def default(self, obj):
if hasattr(obj, "__json__"):
return obj.__json__()
elif isinstance(obj, EnvVar):
return obj.value
super().default(obj)
[docs]
class JsonSerializable:
"""Inheriting from this will allow serialization with RecursiveEncoder"""
def to_json(self):
"""Json representation of class"""
return vars(self)
@classmethod
def from_json(cls, data: dict):
"""Instantiate the class from a dictionary params matching the init"""
return cls(**data)
def __json__(self):
return self.to_json()