Skip to content

Commit

Permalink
Set default encoding error handler for JSON tree codec (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
renatahodovan authored May 27, 2024
1 parent 85d6237 commit 4a17ada
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions grammarinator/tool/tree_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ class JsonTreeCodec(TreeCodec):
JSON-based tree codec.
"""

def __init__(self, encoding='utf-8'):
def __init__(self, encoding='utf-8', encoding_errors='surrogatepass'):
"""
:param str encoding: The encoding to use when converting between
json-formatted text and bytes (default: utf-8).
"""
self._encoding = encoding
self._encoding_errors = encoding_errors

def encode(self, root):
def _rule_to_dict(node):
Expand All @@ -132,7 +133,7 @@ def _rule_to_dict(node):
if isinstance(node, UnparserRuleQuantifier):
return {'t': 'q', 'i': node.idx, 'b': node.start, 'e': node.stop, 'c': node.children}
raise AssertionError
return json.dumps(root, default=_rule_to_dict).encode(encoding=self._encoding)
return json.dumps(root, default=_rule_to_dict).encode(encoding=self._encoding, errors=self._encoding_errors)

def decode(self, data):
def _dict_to_rule(dct):
Expand All @@ -149,6 +150,6 @@ def _dict_to_rule(dct):
raise json.JSONDecodeError

try:
return json.loads(data.decode(encoding=self._encoding), object_hook=_dict_to_rule)
return json.loads(data.decode(encoding=self._encoding, errors=self._encoding_errors), object_hook=_dict_to_rule)
except json.JSONDecodeError:
return None

0 comments on commit 4a17ada

Please sign in to comment.