From 4a17ada2f78c57261ebae35113b308b7e361c7dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A1ta=20Hodov=C3=A1n?= Date: Mon, 27 May 2024 17:05:42 +0200 Subject: [PATCH] Set default encoding error handler for JSON tree codec (#222) --- grammarinator/tool/tree_codec.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/grammarinator/tool/tree_codec.py b/grammarinator/tool/tree_codec.py index 46be629..61e4e63 100644 --- a/grammarinator/tool/tree_codec.py +++ b/grammarinator/tool/tree_codec.py @@ -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): @@ -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): @@ -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