|
1 | | -use pyo3::prelude::*; |
| 1 | +use pyo3::{ |
| 2 | + exceptions, |
| 3 | + prelude::*, |
| 4 | + types::{ PyBool, PyDict, PyFloat, PyInt, PyList, PyNone, PyString }, |
| 5 | +}; |
| 6 | + |
| 7 | +use ijson::{ IValue, ValueType }; |
| 8 | + |
| 9 | +#[pyfunction] |
| 10 | +fn json_to_py(py: Python, json: &str) -> PyResult<Py<PyAny>> { |
| 11 | + let value = match serde_json::from_str::<IValue>(json) { |
| 12 | + Ok(d) => d, |
| 13 | + Err(e) => { |
| 14 | + return Err( |
| 15 | + exceptions::PyRuntimeError::new_err(format!("Failed to load JSON:\n{:#?}", e)) |
| 16 | + ); |
| 17 | + } |
| 18 | + }; |
| 19 | + ivalue_to_py(py, value) |
| 20 | +} |
| 21 | + |
| 22 | +#[pyfunction] |
| 23 | +fn jsonc_to_py(py: Python, json: &str) -> PyResult<Py<PyAny>> { |
| 24 | + let value = match serde_json5::from_str::<IValue>(json) { |
| 25 | + Ok(d) => d, |
| 26 | + Err(e) => { |
| 27 | + return Err( |
| 28 | + exceptions::PyRuntimeError::new_err(format!("Failed to load JSONC:\n{:#?}", e)) |
| 29 | + ); |
| 30 | + } |
| 31 | + }; |
| 32 | + ivalue_to_py(py, value) |
| 33 | +} |
| 34 | + |
| 35 | +fn ivalue_to_py(py: Python, value: IValue) -> PyResult<Py<PyAny>> { |
| 36 | + match value.type_() { |
| 37 | + ValueType::Array => { |
| 38 | + let list = PyList::empty(py); |
| 39 | + let Ok(array) = value.into_array() else { |
| 40 | + return Err(exceptions::PyRuntimeError::new_err("Failed to convert into array")); |
| 41 | + }; |
| 42 | + |
| 43 | + for item in array { |
| 44 | + let value = ivalue_to_py(py, item)?; |
| 45 | + list.append(value)?; |
| 46 | + } |
| 47 | + |
| 48 | + Ok(list.unbind().into()) |
| 49 | + } |
| 50 | + ValueType::Bool => { |
| 51 | + let b = PyBool::new(py, value.to_bool().unwrap()); |
| 52 | + Ok(unsafe { Py::from_borrowed_ptr_or_opt(py, b.as_ptr()).unwrap() }) |
| 53 | + } |
| 54 | + ValueType::Null => { |
| 55 | + let none = PyNone::get(py); |
| 56 | + Ok(unsafe { Py::from_borrowed_ptr_or_opt(py, none.as_ptr()).unwrap() }) |
| 57 | + } |
| 58 | + ValueType::Number => { |
| 59 | + let Ok(number) = value.into_number() else { |
| 60 | + return Err(exceptions::PyRuntimeError::new_err("Failed to convert into number")); |
| 61 | + }; |
| 62 | + |
| 63 | + if number.has_decimal_point() { |
| 64 | + Ok(PyFloat::new(py, number.to_f64().unwrap()).unbind().into()) |
| 65 | + } else { |
| 66 | + Ok(PyInt::new(py, number.to_i64().unwrap()).unbind().into()) |
| 67 | + } |
| 68 | + } |
| 69 | + ValueType::Object => { |
| 70 | + let Ok(object) = value.into_object() else { |
| 71 | + return Err(exceptions::PyRuntimeError::new_err("Failed to convert into object")); |
| 72 | + }; |
| 73 | + |
| 74 | + let dict = PyDict::new(py); |
| 75 | + for (key, value) in object { |
| 76 | + dict.set_item(key.as_str(), ivalue_to_py(py, value)?)?; |
| 77 | + } |
| 78 | + |
| 79 | + Ok(dict.unbind().into()) |
| 80 | + } |
| 81 | + ValueType::String => { |
| 82 | + let Ok(s) = value.into_string() else { |
| 83 | + return Err(exceptions::PyRuntimeError::new_err("Failed to convert into string")); |
| 84 | + }; |
| 85 | + Ok(PyString::new(py, s.as_str()).unbind().into()) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// #[derive(FromPyObject)] |
| 91 | +// enum AnyPy { |
| 92 | +// List(Py<PyList>), |
| 93 | +// Dict(Py<PyDict>), |
| 94 | +// Str(String), |
| 95 | +// Bool(bool), |
| 96 | +// None(Py<PyNone>), |
| 97 | +// Int(i64), |
| 98 | +// Float(f64), |
| 99 | +// } |
2 | 100 |
|
3 | 101 | #[pymodule] |
4 | | -fn exacting(m: &Bound<'_, PyModule>) -> PyResult<()> { |
5 | | - Ok(()) |
| 102 | +mod exacting { |
| 103 | + use super::*; |
| 104 | + |
| 105 | + #[pymodule] |
| 106 | + mod json { |
| 107 | + use super::*; |
| 108 | + |
| 109 | + #[pymodule_init] |
| 110 | + fn init(m: &Bound<'_, PyModule>) -> PyResult<()> { |
| 111 | + m.add_function(wrap_pyfunction!(json_to_py, m)?)?; |
| 112 | + m.add_function(wrap_pyfunction!(jsonc_to_py, m)?)?; |
| 113 | + Ok(()) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + #[pymodule_init] |
| 118 | + fn init(_m: &Bound<'_, PyModule>) -> PyResult<()> { |
| 119 | + Ok(()) |
| 120 | + } |
6 | 121 | } |
0 commit comments