From 68e8c681d8f9ea15d1dab4524f814adf551d1e7f Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Thu, 26 Sep 2024 08:59:48 +0100 Subject: [PATCH] allow parser to accept python dict --- pyzx/graph/jsonparser.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pyzx/graph/jsonparser.py b/pyzx/graph/jsonparser.py index 2d3fe3b1..d3955893 100644 --- a/pyzx/graph/jsonparser.py +++ b/pyzx/graph/jsonparser.py @@ -71,10 +71,13 @@ def _new_var(name: str) -> Poly: except Exception as e: raise ValueError(e) -def json_to_graph(js: str, backend:Optional[str]=None) -> BaseGraph: +def json_to_graph(js: str|dict[str,Any], backend:Optional[str]=None) -> BaseGraph: """Converts the json representation of a .qgraph Quantomatic graph into - a pyzx graph.""" - j = json.loads(js, cls=ComplexDecoder) + a pyzx graph. If JSON is given as a string, parse it first.""" + if isinstance(js, str): + j = json.loads(js, cls=ComplexDecoder) + else: + j = js g = Graph(backend) g.variable_types = j.get('variable_types',{})