pub struct JsonObject { /* private fields */ }Expand description
A JSON object, validated on construction.
Implementations§
Source§impl JsonObject
impl JsonObject
Sourcepub fn new(text: impl Into<String>) -> Result<Self, JsonObjectError>
pub fn new(text: impl Into<String>) -> Result<Self, JsonObjectError>
Validates text as a JSON object.
§Errors
JsonObjectError::InvalidJson when the text is not well-formed JSON,
and JsonObjectError::NotAnObject when it parses but is not an
object — an array or a bare scalar is a different thing, and 2025-06-18
structuredContent is specifically an object.
§Examples
use protocol_droid::domain::ir::JsonObject;
let json = JsonObject::new(r#"{"temperature": 0.7}"#)?;
assert_eq!(json.as_str(), r#"{"temperature": 0.7}"#);Sourcepub fn from_value(value: Value) -> Result<Self, JsonObjectError>
pub fn from_value(value: Value) -> Result<Self, JsonObjectError>
Validates a serde_json::Value and stores it.
A tool handler’s result is built from data — the argument echoed back,
a computed value, an error message composed from a failure. Doing that
with new means format!ing a JSON document by hand, and a handler
that interpolates a peer’s string into a JSON literal without escaping
it is an injection bug. This is the constructor that makes that mistake
unavailable.
The value is serialised rather than kept, so a JsonObject is still one
validated String and still owns no borrowed peer’s data.
§Errors
JsonObjectError::NotAnObject when the value is not an object.
JsonObjectError::InvalidJson when the value cannot be serialised,
which for a Value cannot happen — but the error type is the honest
one, and inventing a second error for an unreachable case would be a
promise this function cannot keep.
§Examples
use protocol_droid::domain::ir::JsonObject;
let value = serde_json::json!({ "echoed": "quotes \" and \\ survive" });
let json = JsonObject::from_value(value)?;
assert_eq!(json.as_str(), r#"{"echoed":"quotes \" and \\ survive"}"#);Sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Consumes the object, yielding its text.
Trait Implementations§
Source§impl Clone for JsonObject
impl Clone for JsonObject
Source§impl Debug for JsonObject
impl Debug for JsonObject
Source§impl Default for JsonObject
impl Default for JsonObject
Source§fn default() -> Self
fn default() -> Self
The empty object, {}.
A tool handler returns a JsonObject and cannot return a Result,
because MCP reports a tool failure as a successful call carrying
isError: true. So a handler that cannot build the result it meant to
build needs somewhere to fall back to, and it needs to be infallible.
{} is that place: it is a valid document under every revision of the
protocol, so no fallback can itself be a parse error.
Not "", which is the default a String-backed type reaches for and
which is not a JSON object at all.