gent/runtime/tools/
json_parse.rs1use super::Tool;
2use async_trait::async_trait;
3use serde_json::{json, Value};
4
5pub struct JsonParseTool;
6
7impl JsonParseTool {
8 pub fn new() -> Self {
9 Self
10 }
11}
12
13impl Default for JsonParseTool {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19#[async_trait]
20impl Tool for JsonParseTool {
21 fn name(&self) -> &str {
22 "json_parse"
23 }
24
25 fn description(&self) -> &str {
26 "Parse a JSON string into an object or array"
27 }
28
29 fn parameters_schema(&self) -> Value {
30 json!({
31 "type": "object",
32 "properties": {
33 "text": {
34 "type": "string",
35 "description": "The JSON string to parse"
36 }
37 },
38 "required": ["text"]
39 })
40 }
41
42 async fn execute(&self, args: Value) -> Result<String, String> {
43 let text = args
44 .get("text")
45 .and_then(|v| v.as_str())
46 .ok_or_else(|| "Missing required parameter: text".to_string())?;
47
48 let parsed: Value =
50 serde_json::from_str(text).map_err(|e| format!("Failed to parse JSON: {}", e))?;
51
52 serde_json::to_string(&parsed).map_err(|e| format!("Failed to serialize JSON: {}", e))
54 }
55}