Commit 24538438 authored by Eckhart Arnold's avatar Eckhart Arnold

- json serialization of Nodes done

parent 99c6fa7a
...@@ -143,7 +143,7 @@ class Error: ...@@ -143,7 +143,7 @@ class Error:
def from_json_obj(self, json_obj: Dict) -> Error: def from_json_obj(self, json_obj: Dict) -> Error:
"""Convert a json object representing an Error object back into an """Convert a json object representing an Error object back into an
Error object. Raises a ValueError, if json_obj does not represent Error object. Raises a ValueError, if json_obj does not represent
an error object""" an Error object"""
if json_obj.get('__class__', '') != 'DHParser.Error': if json_obj.get('__class__', '') != 'DHParser.Error':
raise ValueError('JSON object: ' + str(json_obj) + raise ValueError('JSON object: ' + str(json_obj) +
' does not represent an Error object.') ' does not represent an Error object.')
......
...@@ -749,17 +749,34 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -749,17 +749,34 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
def to_json_obj(self) -> Dict: def to_json_obj(self) -> Dict:
return { '__class__': 'DHParser.Nose', """Seralize a node or tree as json-object"""
'content': [ self.tag_name, return { '__class__': 'DHParser.Node',
[child.to_json_obj() for child in self.children] if self.children 'data': [ self.tag_name,
else self.result_, [child.to_json_obj() for child in self.children] if self.children
self._pos, else str(self.result_),
self._xmlattr if self.attr_active() else None ] } self._pos,
self._xmlattr if self.attr_active() else None ] }
@static @static
def from_json_obj(self, json_obj: Dict) -> Error: def from_json_obj(self, json_obj: Dict) -> Error:
pass """Convert a json object representing a node (or tree) back into a
Node object. Raises a ValueError, if `json_obj` does not represent
a node."""
if json_obj.get('__class__', '') != 'DHParser.Node':
raise ValueError('JSON object: ' + str(json_obj) +
' does not represent a Node object.')
tag_name, result, pos, attr = json_obj['data']
if isinstance(str):
leafhint = True
else:
leafhint = False
result = tuple(Node.from_json_obj(child) for child in result)
node = Node(tag_name, result, leafhint)
node._pos = pos
if attr:
node.attr = attr
return node
def serialize(node: Node, how: str='default') -> str: def serialize(node: Node, how: str='default') -> str:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment