MCPcopy Index your code
hub / github.com/InfinitiBit/graphbit / serde_json_to_py_object

Function serde_json_to_py_object

python/src/document_loader.rs:335–373  ·  view source on GitHub ↗

Helper function to convert serde_json::Value to Python object

(py: Python<'_>, value: &serde_json::Value)

Source from the content-addressed store, hash-verified

333
334/// Helper function to convert serde_json::Value to Python object
335fn serde_json_to_py_object(py: Python<'_>, value: &serde_json::Value) -> PyResult<PyObject> {
336 match value {
337 serde_json::Value::Null => Ok(py.None()),
338 serde_json::Value::Bool(b) => {
339 let py_bool = b.into_pyobject(py)?;
340 Ok(
341 <pyo3::Bound<'_, pyo3::types::PyBool> as Clone>::clone(&py_bool)
342 .into_any()
343 .unbind(),
344 )
345 }
346 serde_json::Value::Number(n) => {
347 if let Some(i) = n.as_i64() {
348 Ok(i.into_pyobject(py)?.into_any().unbind())
349 } else if let Some(f) = n.as_f64() {
350 Ok(f.into_pyobject(py)?.into_any().unbind())
351 } else {
352 Ok(n.to_string().into_pyobject(py)?.into_any().unbind())
353 }
354 }
355 serde_json::Value::String(s) => Ok(s.into_pyobject(py)?.into_any().unbind()),
356 serde_json::Value::Array(arr) => {
357 let py_list = pyo3::types::PyList::empty(py);
358 for item in arr {
359 let py_item = serde_json_to_py_object(py, item)?;
360 py_list.append(py_item)?;
361 }
362 Ok(py_list.into_any().unbind())
363 }
364 serde_json::Value::Object(obj) => {
365 let py_dict = PyDict::new(py);
366 for (key, val) in obj {
367 let py_val = serde_json_to_py_object(py, val)?;
368 py_dict.set_item(key, py_val)?;
369 }
370 Ok(py_dict.into_any().unbind())
371 }
372 }
373}
374
375/// Helper function to convert Python object to serde_json::Value
376fn py_object_to_serde_json(py: Python<'_>, obj: &Bound<'_, PyAny>) -> PyResult<serde_json::Value> {

Callers 2

extraction_settingsMethod · 0.85
metadataMethod · 0.85

Calls 1

to_stringMethod · 0.80

Tested by

no test coverage detected