Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
badw-it
DHParser
Commits
24538438
Commit
24538438
authored
Mar 08, 2019
by
Eckhart Arnold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- json serialization of Nodes done
parent
99c6fa7a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
8 deletions
+25
-8
DHParser/error.py
DHParser/error.py
+1
-1
DHParser/syntaxtree.py
DHParser/syntaxtree.py
+24
-7
No files found.
DHParser/error.py
View file @
24538438
...
...
@@ -143,7 +143,7 @@ class Error:
def
from_json_obj
(
self
,
json_obj
:
Dict
)
->
Error
:
"""Convert a json object representing an Error object back into an
Error object. Raises a ValueError, if json_obj does not represent
an
e
rror object"""
an
E
rror object"""
if
json_obj
.
get
(
'__class__'
,
''
)
!=
'DHParser.Error'
:
raise
ValueError
(
'JSON object: '
+
str
(
json_obj
)
+
' does not represent an Error object.'
)
...
...
DHParser/syntaxtree.py
View file @
24538438
...
...
@@ -749,17 +749,34 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
def
to_json_obj
(
self
)
->
Dict
:
return
{
'__class__'
:
'DHParser.Nose'
,
'content'
:
[
self
.
tag_name
,
"""Seralize a node or tree as json-object"""
return
{
'__class__'
:
'DHParser.Node'
,
'data'
:
[
self
.
tag_name
,
[
child
.
to_json_obj
()
for
child
in
self
.
children
]
if
self
.
children
else
self
.
result_
,
else
str
(
self
.
result_
)
,
self
.
_pos
,
self
.
_xmlattr
if
self
.
attr_active
()
else
None
]
}
@
static
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
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment