forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.rs
More file actions
51 lines (45 loc) · 1.75 KB
/
json.rs
File metadata and controls
51 lines (45 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::obj::objstr::PyStringRef;
use crate::py_serde;
use crate::pyobject::{create_type, ItemProtocol, PyObjectRef, PyResult};
use crate::VirtualMachine;
use serde_json;
/// Implement json.dumps
pub fn json_dumps(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
let serializer = py_serde::PyObjectSerializer::new(vm, &obj);
serde_json::to_string(&serializer).map_err(|err| vm.new_type_error(err.to_string()))
}
/// Implement json.loads
pub fn json_loads(string: PyStringRef, vm: &VirtualMachine) -> PyResult {
// TODO: Implement non-trivial deserialization case
let de_result =
py_serde::deserialize(vm, &mut serde_json::Deserializer::from_str(string.as_str()));
de_result.map_err(|err| {
let module = vm
.get_attribute(vm.sys_module.clone(), "modules")
.unwrap()
.get_item("json", vm)
.unwrap();
let json_decode_error = vm.get_attribute(module, "JSONDecodeError").unwrap();
let json_decode_error = json_decode_error.downcast().unwrap();
let exc = vm.new_exception(json_decode_error, format!("{}", err));
vm.set_attr(&exc, "lineno", vm.ctx.new_int(err.line()))
.unwrap();
vm.set_attr(&exc, "colno", vm.ctx.new_int(err.column()))
.unwrap();
exc
})
}
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let ctx = &vm.ctx;
// TODO: Make this a proper type with a constructor
let json_decode_error = create_type(
"JSONDecodeError",
&ctx.type_type,
&ctx.exceptions.exception_type,
);
py_module!(vm, "json", {
"dumps" => ctx.new_rustfunc(json_dumps),
"loads" => ctx.new_rustfunc(json_loads),
"JSONDecodeError" => json_decode_error
})
}