pub struct Json { /* private fields */ }
Implementations§
Source§impl Json
impl Json
Sourcepub fn to_object<T: DeserializeOwned>(&self) -> Result<T, Error>
pub fn to_object<T: DeserializeOwned>(&self) -> Result<T, Error>
Deserializes the JSON data into a custom type.
This method attempts to deserialize the stored JSON value into any type
that implements DeserializeOwned
. This is useful for converting the
raw JSON configuration into strongly-typed structs.
§Type Parameters
T
- The target type to deserialize into. Must implementDeserializeOwned
.
§Returns
Ok(T)
- The deserialized configuration objectErr(serde_json::Error)
- If deserialization fails due to format mismatches, missing fields, or type conversion failures
§Errors
This method will return an error if:
- The JSON structure doesn’t match the expected type
- Required fields are missing from the JSON
- Type conversion fails (e.g., string to number)
- The JSON is malformed or invalid
§Examples
ⓘ
use serde::{Deserialize, Serialize};
use serde_json::json;
use apollo_client::namespace::json::Json;
#[derive(Deserialize, Serialize)]
struct DatabaseConfig {
host: String,
port: u16,
}
let json_data = json!({
"host": "localhost",
"port": 5432
});
let json_namespace = Json::from(json_data);
let config: DatabaseConfig = json_namespace.to_object()?;
Trait Implementations§
Source§impl TryFrom<Value> for Json
Converts a serde_json::Value
into a Json
instance.
impl TryFrom<Value> for Json
Converts a serde_json::Value
into a Json
instance.
This implementation allows for easy creation of Json
instances from
raw JSON data, typically used by the namespace detection system.
§Arguments
json_value
- The raw JSON value containing configuration data
§Returns
Ok(Json)
- A new Json instance containing the parsed configurationErr(Error::ContentNotFound)
- If the JSON value doesn’t contain a “content” fieldErr(Error::DeserializeError)
- If the content field cannot be parsed as valid JSON
§Errors
This function will return an error if:
- The JSON value doesn’t contain a “content” field
- The “content” field is not a string
- The content string cannot be parsed as valid JSON
§Examples
ⓘ
use serde_json::json;
use apollo_client::namespace::json::Json;
let json_data = json!({"content": "{\"key\": \"value\"}"});
let json_namespace = Json::try_from(json_data)?;
Auto Trait Implementations§
impl Freeze for Json
impl RefUnwindSafe for Json
impl Send for Json
impl Sync for Json
impl Unpin for Json
impl UnwindSafe for Json
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more