pub struct Extensions(/* private fields */);Expand description
Undocumented JSON fields found on an OCPI object, preserved verbatim.
OCPI 2.3.0 is explicit:
An OCPI Platform SHALL NOT reject request or response payloads based on the presence of JSON object field names that are not documented in this specification.
OCPI implementers are encouraged to extend OCPI with new fields to address needs that are not foreseen by the specification.
Every OCPI object in this crate carries an extensions field marked #[serde(flatten)], so
a vendor field arrives, survives, and is written back out unchanged. A hub built on this
crate can therefore sit between two parties that have agreed on an extension without knowing
anything about it — which is the whole point of that paragraph, and the thing generated type
sets get wrong.
Keys are kept in a BTreeMap, so serialisation order is deterministic.
Every wire object in this crate carries one as a #[serde(flatten)] field, so the undocumented
members of the object it came from land here and are written straight back:
use ocpi_kit::types::Extensions;
let json = r#"{"acme_note":"kerbside","nltnm_accuracy_m":3}"#;
let extensions: Extensions = serde_json::from_str(json).unwrap();
assert_eq!(extensions.get::<u32>("nltnm_accuracy_m").unwrap(), Some(3));
assert_eq!(serde_json::to_string(&extensions).unwrap(), json);In place, on a real object:
let json = r#"{"latitude":"52.010","longitude":"4.350","nltnm_accuracy_m":3}"#;
let geo: GeoLocation = serde_json::from_str(json).unwrap();
assert_eq!(geo.extensions.get::<u32>("nltnm_accuracy_m").unwrap(), Some(3));
assert_eq!(serde_json::to_string(&geo).unwrap(), json);Spec: 2.3.0 §transport_and_format — Non-specified JSON fields
Implementations§
Source§impl Extensions
impl Extensions
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether no undocumented field was present.
Objects skip serialising their extensions field when this is true, so an object that
carried no extensions is written back byte-identically.
Sourcepub fn get_raw(&self, key: &str) -> Option<&Value>
pub fn get_raw(&self, key: &str) -> Option<&Value>
The raw JSON value stored under key, if any.
Sourcepub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>, Error>
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>, Error>
Deserialises the value stored under key into T.
Returns Ok(None) when the key is absent, and Err when it is present but does not
deserialise into T.
§Errors
Propagates the serde_json error describing why the value did not fit T.
Sourcepub fn insert<T: Serialize>(
&mut self,
key: impl Into<String>,
value: T,
) -> Result<(), Error>
pub fn insert<T: Serialize>( &mut self, key: impl Into<String>, value: T, ) -> Result<(), Error>
Stores value under key, replacing any previous value.
§Errors
Propagates the serde_json error if value cannot be serialised.
Sourcepub fn remove(&mut self, key: &str) -> Option<Value>
pub fn remove(&mut self, key: &str) -> Option<Value>
Removes and returns the raw value stored under key.
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Whether key is present.
Trait Implementations§
Source§impl Clone for Extensions
impl Clone for Extensions
Source§fn clone(&self) -> Extensions
fn clone(&self) -> Extensions
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Extensions
impl Debug for Extensions
Source§impl Default for Extensions
impl Default for Extensions
Source§fn default() -> Extensions
fn default() -> Extensions
Source§impl<'de> Deserialize<'de> for Extensions
impl<'de> Deserialize<'de> for Extensions
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<K: Into<String>, V: Into<Value>> FromIterator<(K, V)> for Extensions
impl<K: Into<String>, V: Into<Value>> FromIterator<(K, V)> for Extensions
Source§impl<'a> IntoIterator for &'a Extensions
impl<'a> IntoIterator for &'a Extensions
Source§impl JsonSchema for Extensions
Available on crate feature schema only.
impl JsonSchema for Extensions
schema only.Source§fn json_schema(_g: &mut SchemaGenerator) -> Schema
fn json_schema(_g: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read more