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

Function python_to_json_value

python/src/tools/result.rs:177–196  ·  view source on GitHub ↗

Helper function to convert Python values to JSON

(value: &Bound<'_, PyAny>)

Source from the content-addressed store, hash-verified

175
176/// Helper function to convert Python values to JSON
177fn python_to_json_value(value: &Bound<'_, PyAny>) -> PyResult<serde_json::Value> {
178 if value.is_none() {
179 Ok(serde_json::Value::Null)
180 } else if let Ok(s) = value.extract::<String>() {
181 Ok(serde_json::Value::String(s))
182 } else if let Ok(i) = value.extract::<i64>() {
183 Ok(serde_json::Value::Number(serde_json::Number::from(i)))
184 } else if let Ok(f) = value.extract::<f64>() {
185 if let Some(num) = serde_json::Number::from_f64(f) {
186 Ok(serde_json::Value::Number(num))
187 } else {
188 Ok(serde_json::Value::Null)
189 }
190 } else if let Ok(b) = value.extract::<bool>() {
191 Ok(serde_json::Value::Bool(b))
192 } else {
193 // Fallback to string representation
194 Ok(serde_json::Value::String(value.to_string()))
195 }
196}
197
198/// Collection of tool results for batch operations
199#[pyclass]

Callers 1

add_metadataMethod · 0.85

Calls 1

to_stringMethod · 0.80

Tested by

no test coverage detected