Common: Add AgentConfiguration.to_json()

This commit is contained in:
Mike Salvatore 2022-06-24 12:19:31 -04:00
parent 1f9a056b0b
commit 28250daffe
2 changed files with 14 additions and 0 deletions

View File

@ -1,3 +1,5 @@
from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from typing import List from typing import List
@ -32,6 +34,10 @@ class AgentConfiguration:
def from_json(config_json: dict): def from_json(config_json: dict):
return AgentConfigurationSchema().loads(config_json) return AgentConfigurationSchema().loads(config_json)
@staticmethod
def to_json(config: AgentConfiguration) -> str:
return AgentConfigurationSchema().dumps(config)
class AgentConfigurationSchema(Schema): class AgentConfigurationSchema(Schema):
keep_tunnel_open_time = fields.Float() keep_tunnel_open_time = fields.Float()

View File

@ -198,3 +198,11 @@ def test_from_json():
config = AgentConfiguration.from_json(DEFAULT_AGENT_CONFIGURATION_JSON) config = AgentConfiguration.from_json(DEFAULT_AGENT_CONFIGURATION_JSON)
assert schema.dump(config) == dict_ assert schema.dump(config) == dict_
def test_to_json():
config = AgentConfiguration.from_json(DEFAULT_AGENT_CONFIGURATION_JSON)
assert json.loads(AgentConfiguration.to_json(config)) == json.loads(
DEFAULT_AGENT_CONFIGURATION_JSON
)