forked from ijl/orjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
79 lines (71 loc) · 2.09 KB
/
Copy pathlib.rs
File metadata and controls
79 lines (71 loc) · 2.09 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
#![feature(custom_attribute)]
#![feature(core_intrinsics)]
#[macro_use]
extern crate pyo3;
extern crate encoding_rs;
extern crate itoa;
extern crate serde;
extern crate serde_json;
extern crate smallvec;
use pyo3::prelude::*;
use pyo3::ToPyPointer;
use std::ptr::NonNull;
mod decode;
mod encode;
mod exc;
mod typeref;
#[pymodule]
fn orjson(py: Python, m: &PyModule) -> PyResult<()> {
typeref::init_typerefs();
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
m.add_wrapped(wrap_pyfunction!(dumps))?;
m.add_wrapped(wrap_pyfunction!(loads))?;
m.add("JSONDecodeError", py.get_type::<exc::JSONDecodeError>())?;
m.add("JSONEncodeError", py.get_type::<exc::JSONEncodeError>())?;
m.add("OPT_STRICT_INTEGER", encode::STRICT_INTEGER.into_object(py))?;
m.add("OPT_NAIVE_UTC", encode::NAIVE_UTC.into_object(py))?;
Ok(())
}
/// loads(obj, /)
/// --
///
/// Deserialize JSON to Python objects.
#[pyfunction]
pub fn loads(py: Python, obj: PyObject) -> PyResult<PyObject> {
decode::deserialize(py, obj.as_ptr())
}
/// dumps(obj, default, option, /)
/// --
///
/// Serialize Python objects to JSON.
#[pyfunction]
pub fn dumps(
py: Python,
obj: PyObject,
default: Option<PyObject>,
option: Option<PyObject>,
) -> PyResult<PyObject> {
let pydef: Option<NonNull<pyo3::ffi::PyObject>>;
if default.is_some() {
pydef = Some(unsafe { NonNull::new_unchecked(default.unwrap().as_ptr()) });
} else {
pydef = None
};
let optsbits: i8;
if option.is_some() {
let optsptr = option.unwrap().as_ptr();
if unsafe { (*optsptr).ob_type != typeref::INT_PTR } {
return Err(exc::JSONEncodeError::py_err("Invalid opts"));
} else {
optsbits = unsafe { pyo3::ffi::PyLong_AsLong(optsptr) as i8 };
if optsbits <= 0 || optsbits > encode::MAX_OPT {
// -1
return Err(exc::JSONEncodeError::py_err("Invalid opts"));
}
}
} else {
optsbits = 0
};
encode::serialize(py, obj.as_ptr(), pydef, optsbits as u8)
}