Serialize/deserialize to json
This commit is contained in:
parent
d2952ffab1
commit
8e19d33343
2 changed files with 23 additions and 0 deletions
|
@ -74,6 +74,27 @@ class NagObject:
|
||||||
wrap(dir[1], ws_base).strip().strip('\\\n').strip())
|
wrap(dir[1], ws_base).strip().strip('\\\n').strip())
|
||||||
return string + '}\n'
|
return string + '}\n'
|
||||||
|
|
||||||
|
def from_object(self, object: dict) -> None:
|
||||||
|
self.m_type.value = object['type']
|
||||||
|
self.m_config_file = object['config_file']
|
||||||
|
self.m_directives = object['directives']
|
||||||
|
self.m_line_no = object['line_number']
|
||||||
|
|
||||||
|
def to_object(self) -> dict:
|
||||||
|
object = {
|
||||||
|
'type': self.m_type.value,
|
||||||
|
'config_file': self.m_config_file,
|
||||||
|
'directives': self.m_directives,
|
||||||
|
'line_number': self.m_line_no
|
||||||
|
}
|
||||||
|
return object
|
||||||
|
|
||||||
|
def from_json(self, string: str) -> None:
|
||||||
|
self.from_object(json.loads(string))
|
||||||
|
|
||||||
|
def to_json(self) -> str:
|
||||||
|
return json.dumps(self.to_object)
|
||||||
|
|
||||||
def get_type(self) -> NagObjectType:
|
def get_type(self) -> NagObjectType:
|
||||||
return self.m_type
|
return self.m_type
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,8 @@ class NagParse:
|
||||||
for object in self.m_objects:
|
for object in self.m_objects:
|
||||||
string += str(object) + "\n"
|
string += str(object) + "\n"
|
||||||
return string
|
return string
|
||||||
|
def to_json(self):
|
||||||
|
return json.dumps([x.to_object() for x in self.m_objects])
|
||||||
|
|
||||||
def parse_config(self,config_file: str) -> list[NagObject]:
|
def parse_config(self,config_file: str) -> list[NagObject]:
|
||||||
state = ParserState.DEFINE
|
state = ParserState.DEFINE
|
||||||
|
|
Loading…
Add table
Reference in a new issue