feagi_data_structures/
feagi_json.rs

1use serde_json;
2use crate::FeagiDataError;
3
4/// A wrapper around serde_json::Value for handling JSON data in FEAGI.
5/// 
6/// Provides methods to create, parse, and manipulate JSON data with error handling.
7#[derive(Clone)]
8pub struct FeagiJSON {
9    json : serde_json::Value,
10}
11
12impl FeagiJSON {
13    pub fn new_empty() -> FeagiJSON {
14        FeagiJSON {
15            json: serde_json::json!({}),
16        }
17    }
18
19    pub fn from_json_string(string: String) -> Result<FeagiJSON, FeagiDataError> {
20        match serde_json::from_str(&string) {
21            Ok(json_value) => Ok(FeagiJSON { json: json_value }),
22            Err(e) => Err(FeagiDataError::BadParameters(
23                format!("Failed to parse JSON string: {}", e)
24            ).into()),
25        }
26    }
27
28    /// Creates a FeagiJSON from an existing serde_json::Value.
29    pub fn from_json_value(value: serde_json::Value) -> FeagiJSON {
30        FeagiJSON { json: value }
31    }
32
33    /// Returns a reference to the internal JSON value.
34    pub fn borrow_json_value(&self) -> &serde_json::Value {
35        &self.json
36    }
37
38    /// Updates the internal JSON value.
39    pub fn update_json_value(&mut self, new_value: serde_json::Value) {
40        self.json = new_value;
41    }
42}
43
44impl std::fmt::Display for FeagiJSON {
45    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
46        write!(f, "{}", self.json)
47    }
48}
49