strands/types/json.rs
1//! JSON aliases.
2
3use serde::{Deserialize, Deserializer};
4
5/// Any JSON value.
6pub type JsonValue = serde_json::Value;
7
8/// A JSON object with string keys, in insertion order.
9pub type JsonObject = serde_json::Map<String, JsonValue>;
10
11/// A JSON Schema document kept as raw JSON.
12///
13/// Not re-exported at the crate root: `use strands::*` or `use strands::JsonSchema` next to
14/// `schemars::JsonSchema` would clash with the derive that structured output and `#[tool]` inputs
15/// use. Name it as `strands::types::JsonSchema`.
16pub type JsonSchema = JsonValue;
17
18/// Deserializes an optional JSON field so that a present `null` is `Some(JsonValue::Null)`.
19///
20/// An explicit `null` is a value, not an absent field, so `{"response": null}` must survive a
21/// round trip. Use with `#[serde(default, deserialize_with = "...")]`.
22pub(crate) fn present<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<JsonValue>, D::Error> {
23 JsonValue::deserialize(deserializer).map(Some)
24}