From ae5b6426aaf84a49c4b99b3486b20032fd9a4ff2 Mon Sep 17 00:00:00 2001 From: Xiaojing Chen Date: Wed, 8 Mar 2023 06:37:34 +0800 Subject: [PATCH] Clean up json dumps (#642) --- pynecone/components/tags/tag.py | 6 +++--- pynecone/event.py | 4 ++-- pynecone/utils.py | 12 ++++++++++++ tests/test_event.py | 8 +++----- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/pynecone/components/tags/tag.py b/pynecone/components/tags/tag.py index 2d38fd802..b5c455b47 100644 --- a/pynecone/components/tags/tag.py +++ b/pynecone/components/tags/tag.py @@ -68,7 +68,7 @@ class Tag(Base): if not prop.is_local or prop.is_string: return str(prop) if issubclass(prop.type_, str): - return json.dumps(prop.full_name, ensure_ascii=False) + return utils.json_dumps(prop.full_name) prop = prop.full_name # Handle event props. @@ -88,7 +88,7 @@ class Tag(Base): elif isinstance(prop, str): if utils.is_wrapped(prop, "{"): return prop - return json.dumps(prop, ensure_ascii=False) + return utils.json_dumps(prop) elif isinstance(prop, Figure): prop = json.loads(to_json(prop))["data"] # type: ignore @@ -103,7 +103,7 @@ class Tag(Base): } # Dump the prop as JSON. - prop = json.dumps(prop, ensure_ascii=False) + prop = utils.json_dumps(prop) # This substitution is necessary to unwrap var values. prop = re.sub('"{', "", prop) diff --git a/pynecone/event.py b/pynecone/event.py index 2cefc99c1..43a64b84e 100644 --- a/pynecone/event.py +++ b/pynecone/event.py @@ -2,9 +2,9 @@ from __future__ import annotations import inspect -import json from typing import Any, Callable, Dict, List, Set, Tuple +from pynecone import utils from pynecone.base import Base from pynecone.var import BaseVar, Var @@ -67,7 +67,7 @@ class EventHandler(Base): # Otherwise, convert to JSON. try: - values.append(json.dumps(arg, ensure_ascii=False)) + values.append(utils.json_dumps(arg)) except TypeError as e: raise TypeError( f"Arguments to event handlers must be Vars or JSON-serializable. Got {arg} of type {type(arg)}." diff --git a/pynecone/utils.py b/pynecone/utils.py index f6e33a215..42b95651c 100644 --- a/pynecone/utils.py +++ b/pynecone/utils.py @@ -1576,5 +1576,17 @@ def is_backend_variable(name: str) -> bool: return name.startswith("_") and not name.startswith("__") +def json_dumps(obj: Any): + """Serialize ``obj`` to a JSON formatted ``str``, ensure_ascii=False. + + Args: + obj: The obj to be fromatted + + Returns: + str: The result of the json dumps + """ + return json.dumps(obj, ensure_ascii=False) + + # Store this here for performance. StateBases = get_base_class(StateVar) diff --git a/tests/test_event.py b/tests/test_event.py index 8bada92eb..8daa0cd05 100644 --- a/tests/test_event.py +++ b/tests/test_event.py @@ -1,8 +1,6 @@ -import json - import pytest -from pynecone import event +from pynecone import event, utils from pynecone.event import Event, EventHandler, EventSpec from pynecone.var import Var @@ -59,8 +57,8 @@ def test_call_event_handler(): assert event_spec.handler == handler assert event_spec.local_args == () assert event_spec.args == ( - ("arg1", json.dumps(first, ensure_ascii=False)), - ("arg2", json.dumps(second, ensure_ascii=False)), + ("arg1", utils.json_dumps(first)), + ("arg2", utils.json_dumps(second)), ) handler = EventHandler(fn=test_fn_with_args)