Clean up json dumps (#642)
This commit is contained in:
parent
e106b7aedf
commit
ae5b6426aa
@ -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)
|
||||
|
@ -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)}."
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user