2025-08-10 02:45:57 +00:00
|
|
|
//! JSONBox 📋 - JSON解析・生成
|
|
|
|
|
// Nyashの箱システムによるJSON解析・生成を提供します。
|
|
|
|
|
// 参考: 既存Boxの設計思想
|
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
use crate::box_trait::{NyashBox, StringBox, BoolBox};
|
|
|
|
|
use std::any::Any;
|
2025-08-10 02:45:57 +00:00
|
|
|
use serde_json::{Value, Error};
|
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2025-08-10 02:45:57 +00:00
|
|
|
pub struct JSONBox {
|
|
|
|
|
pub value: Value,
|
2025-08-10 03:21:24 +00:00
|
|
|
id: u64,
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl JSONBox {
|
|
|
|
|
pub fn from_str(s: &str) -> Result<Self, Error> {
|
2025-08-10 03:21:24 +00:00
|
|
|
static mut COUNTER: u64 = 0;
|
|
|
|
|
let id = unsafe {
|
|
|
|
|
COUNTER += 1;
|
|
|
|
|
COUNTER
|
|
|
|
|
};
|
2025-08-10 02:45:57 +00:00
|
|
|
let value = serde_json::from_str(s)?;
|
2025-08-10 03:21:24 +00:00
|
|
|
Ok(JSONBox { value, id })
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
|
|
|
|
pub fn new(value: Value) -> Self {
|
|
|
|
|
static mut COUNTER: u64 = 0;
|
|
|
|
|
let id = unsafe {
|
|
|
|
|
COUNTER += 1;
|
|
|
|
|
COUNTER
|
|
|
|
|
};
|
|
|
|
|
JSONBox { value, id }
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-10 02:45:57 +00:00
|
|
|
pub fn to_string(&self) -> String {
|
|
|
|
|
self.value.to_string()
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
|
|
|
|
impl NyashBox for JSONBox {
|
|
|
|
|
fn clone_box(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
Box::new(self.clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_string_box(&self) -> StringBox {
|
|
|
|
|
StringBox::new(self.value.to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
|
"JSONBox"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn box_id(&self) -> u64 {
|
|
|
|
|
self.id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
|
|
|
|
if let Some(other_json) = other.as_any().downcast_ref::<JSONBox>() {
|
|
|
|
|
BoolBox::new(self.value == other_json.value)
|
|
|
|
|
} else {
|
|
|
|
|
BoolBox::new(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|