MCPcopy Create free account
hub / github.com/InfinitiBit/graphbit / json_value_to_python

Method json_value_to_python

python/src/tools/executor.rs:636–674  ·  view source on GitHub ↗

Convert JSON value to Python object

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

Source from the content-addressed store, hash-verified

634
635 /// Convert JSON value to Python object
636 fn json_value_to_python(&self, value: &Value, py: Python<'_>) -> PyResult<PyObject> {
637 match value {
638 Value::Null => Ok(py.None()),
639 Value::Bool(b) => {
640 let py_bool = b.into_pyobject(py)?;
641 Ok(
642 <pyo3::Bound<'_, pyo3::types::PyBool> as Clone>::clone(&py_bool)
643 .into_any()
644 .unbind(),
645 )
646 }
647 Value::Number(n) => {
648 if let Some(i) = n.as_i64() {
649 Ok(i.into_pyobject(py)?.into_any().unbind())
650 } else if let Some(f) = n.as_f64() {
651 Ok(f.into_pyobject(py)?.into_any().unbind())
652 } else {
653 Ok(py.None())
654 }
655 }
656 Value::String(s) => Ok(s.into_pyobject(py)?.into_any().unbind()),
657 Value::Array(arr) => {
658 let py_list = PyList::empty(py);
659 for item in arr {
660 let py_item = self.json_value_to_python(item, py)?;
661 py_list.append(py_item)?;
662 }
663 Ok(py_list.into_pyobject(py)?.into_any().unbind())
664 }
665 Value::Object(obj) => {
666 let py_dict = PyDict::new(py);
667 for (key, val) in obj {
668 let py_val = self.json_value_to_python(val, py)?;
669 py_dict.set_item(key, py_val)?;
670 }
671 Ok(py_dict.into_pyobject(py)?.into_any().unbind())
672 }
673 }
674 }
675
676 /// Convert Python value to JSON
677 fn python_to_json_value(&self, value: &Bound<'_, PyAny>) -> PyResult<Value> {

Callers 1

json_to_pydictMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected