pub trait AnyJsonExpressionMethods: Expression + Sized {
// Provided method
fn retrieve_as_text<T>(self, other: T) -> RetrieveAsText<Self, T>
where T: JsonIndex,
<T::Expression as Expression>::SqlType: SqlType { ... }
}Available on (crate features
sqlite or postgres_backend) and (crate features postgres_backend or sqlite) only.Expand description
PostgreSQL specific methods present on JSON and JSONB expressions.
Provided Methods§
Sourcefn retrieve_as_text<T>(self, other: T) -> RetrieveAsText<Self, T>
fn retrieve_as_text<T>(self, other: T) -> RetrieveAsText<Self, T>
Creates a ->> expression JSON.
This operator extracts the value associated with the given key, that is provided on the Right Hand Side of the operator.
Extracts n’th element of JSON array (array elements are indexed from zero, but negative integers count from the end). Extracts JSON object field as Text with the given key.
§Example
let santas_address: serde_json::Value = serde_json::json!({
"street": "Article Circle Expressway 1",
"city": "North Pole",
"postcode": "99705",
"state": "Alaska"
});
diesel::insert_into(contacts)
.values((name.eq("Claus"), address.eq(&santas_address)))
.execute(conn)?;
let santas_postcode = contacts.select(address.retrieve_as_text("postcode")).get_result::<String>(conn)?;
assert_eq!(santas_postcode, "99705");
let robert_downey_jr_addresses: serde_json::Value = serde_json::json!([
{
"street": "Somewhere In La 251",
"city": "Los Angeles",
"postcode": "12231223",
"state": "California"
},
{
"street": "Somewhere In Ny 251",
"city": "New York",
"postcode": "3213212",
"state": "New York"
}
]);
diesel::insert_into(contacts)
.values((name.eq("Robert Downey Jr."), address.eq(&robert_downey_jr_addresses)))
.execute(conn)?;
let roberts_second_address_in_db = contacts
.filter(name.eq("Robert Downey Jr."))
.select(address.retrieve_as_text(1))
.get_result::<String>(conn)?;
let roberts_second_address = serde_json::json!{{
"city": "New York",
"state": "New York",
"street": "Somewhere In Ny 251",
"postcode": "3213212"
}};
assert_eq!(roberts_second_address, serde_json::from_str::<serde_json::Value>(&roberts_second_address_in_db).unwrap());Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.