Skip to content

Commit de1bc93

Browse files
committed
Use json straight from json.rs
1 parent 5b6b33b commit de1bc93

File tree

3 files changed

+24
-39
lines changed

3 files changed

+24
-39
lines changed

vm/src/stdlib/json.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,31 +188,31 @@ impl<'de> Visitor<'de> for PyObjectDeserializer<'de> {
188188
}
189189
}
190190

191+
pub fn ser_pyobject(vm: &mut VirtualMachine, obj: &PyObjectRef) -> serde_json::Result<String> {
192+
let serializer = PyObjectSerializer { pyobject: obj, vm };
193+
serde_json::to_string(&serializer)
194+
}
195+
196+
pub fn de_pyobject(vm: &mut VirtualMachine, s: &str) -> serde_json::Result<PyObjectRef> {
197+
let de = PyObjectDeserializer { vm };
198+
// TODO: Support deserializing string sub-classes
199+
de.deserialize(&mut serde_json::Deserializer::from_str(s))
200+
}
201+
191202
/// Implement json.dumps
192203
fn json_dumps(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
193204
// TODO: Implement non-trivial serialisation case
194205
arg_check!(vm, args, required = [(obj, None)]);
195-
let res = {
196-
let serializer = PyObjectSerializer { pyobject: obj, vm };
197-
serde_json::to_string(&serializer)
198-
};
199-
let string = res.map_err(|err| vm.new_type_error(format!("{}", err)))?;
206+
let string = ser_pyobject(vm, obj).map_err(|err| vm.new_type_error(err.to_string()))?;
200207
Ok(vm.context().new_str(string))
201208
}
202209

203210
/// Implement json.loads
204211
fn json_loads(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
205212
// TODO: Implement non-trivial deserialization case
206213
arg_check!(vm, args, required = [(string, Some(vm.ctx.str_type()))]);
207-
let res = {
208-
let de = PyObjectDeserializer { vm };
209-
// TODO: Support deserializing string sub-classes
210-
de.deserialize(&mut serde_json::Deserializer::from_str(&objstr::get_value(
211-
&string,
212-
)))
213-
};
214-
215-
res.map_err(|err| {
214+
215+
de_pyobject(vm, &objstr::get_value(&string)).map_err(|err| {
216216
let json_decode_error = vm
217217
.sys_module
218218
.get_item("modules")

vm/src/stdlib/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod ast;
22
mod dis;
3-
mod json;
3+
pub mod json;
44
mod keyword;
55
mod math;
66
mod platform;

wasm/lib/src/convert.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,14 @@ pub fn py_to_js(vm: &mut VirtualMachine, py_obj: PyObjectRef) -> JsValue {
111111

112112
return func;
113113
}
114-
// the browser module might not be injected
115-
if let Ok(promise_type) = browser_module::import_promise_type(vm) {
116-
if objtype::isinstance(&py_obj, &promise_type) {
117-
return browser_module::get_promise_value(&py_obj).into();
118-
}
114+
}
115+
// the browser module might not be injected
116+
if let Ok(promise_type) = browser_module::import_promise_type(vm) {
117+
if objtype::isinstance(&py_obj, &promise_type) {
118+
return browser_module::get_promise_value(&py_obj).into();
119119
}
120120
}
121+
121122
if objtype::isinstance(&py_obj, &vm.ctx.bytes_type())
122123
|| objtype::isinstance(&py_obj, &vm.ctx.bytearray_type())
123124
{
@@ -129,15 +130,8 @@ pub fn py_to_js(vm: &mut VirtualMachine, py_obj: PyObjectRef) -> JsValue {
129130
}
130131
arr.into()
131132
} else {
132-
let dumps = rustpython_vm::import::import_module(vm, std::path::PathBuf::default(), "json")
133-
.expect("Couldn't get json module")
134-
.get_attr("dumps".into())
135-
.expect("Couldn't get json dumps");
136-
match vm.invoke(dumps, PyFuncArgs::new(vec![py_obj], vec![])) {
137-
Ok(value) => {
138-
let json = vm.to_pystr(&value).unwrap();
139-
js_sys::JSON::parse(&json).unwrap_or(JsValue::UNDEFINED)
140-
}
133+
match rustpython_vm::stdlib::json::ser_pyobject(vm, &py_obj) {
134+
Ok(json) => js_sys::JSON::parse(&json).unwrap_or(JsValue::UNDEFINED),
141135
Err(_) => JsValue::UNDEFINED,
142136
}
143137
}
@@ -229,19 +223,10 @@ pub fn js_to_py(vm: &mut VirtualMachine, js_val: JsValue) -> PyObjectRef {
229223
// Because `JSON.stringify(undefined)` returns undefined
230224
vm.get_none()
231225
} else {
232-
let loads = rustpython_vm::import::import_module(vm, std::path::PathBuf::default(), "json")
233-
.expect("Couldn't get json module")
234-
.get_attr("loads".into())
235-
.expect("Couldn't get json dumps");
236-
237226
let json = match js_sys::JSON::stringify(&js_val) {
238227
Ok(json) => String::from(json),
239228
Err(_) => return vm.get_none(),
240229
};
241-
let py_json = vm.new_str(json);
242-
243-
vm.invoke(loads, PyFuncArgs::new(vec![py_json], vec![]))
244-
// can safely unwrap because we know it's valid JSON
245-
.unwrap()
230+
rustpython_vm::stdlib::json::de_pyobject(vm, &json).unwrap_or_else(|_| vm.get_none())
246231
}
247232
}

0 commit comments

Comments
 (0)