mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Add json_dump
This commit is contained in:
@@ -10,6 +10,12 @@ pub fn json_dumps(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
|
||||
serde_json::to_string(&serializer).map_err(|err| vm.new_type_error(err.to_string()))
|
||||
}
|
||||
|
||||
pub fn json_dump(obj: PyObjectRef, fs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let result = json_dumps(obj, vm)?;
|
||||
vm.call_method(&fs, "write", vec![vm.new_str(result)])?;
|
||||
Ok(vm.get_none())
|
||||
}
|
||||
|
||||
/// Implement json.loads
|
||||
pub fn json_loads(string: PyStringRef, vm: &VirtualMachine) -> PyResult {
|
||||
// TODO: Implement non-trivial deserialization case
|
||||
@@ -45,6 +51,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
|
||||
py_module!(vm, "json", {
|
||||
"dumps" => ctx.new_rustfunc(json_dumps),
|
||||
"dump" => ctx.new_rustfunc(json_dump),
|
||||
"loads" => ctx.new_rustfunc(json_loads),
|
||||
"JSONDecodeError" => json_decode_error
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user