serde: Make Raw deserialization methods more general
This commit is contained in:
parent
165729fe71
commit
fdf7af442c
@ -1,5 +1,9 @@
|
||||
# [unreleased]
|
||||
|
||||
Improvements:
|
||||
|
||||
* Make `Raw::deserialize` & `Raw::deserialize_as` more general
|
||||
|
||||
# 0.4.1
|
||||
|
||||
Improvements:
|
||||
|
@ -5,7 +5,7 @@ use std::{
|
||||
};
|
||||
|
||||
use serde::{
|
||||
de::{Deserialize, DeserializeOwned, Deserializer, IgnoredAny, MapAccess, Visitor},
|
||||
de::{Deserialize, Deserializer, IgnoredAny, MapAccess, Visitor},
|
||||
ser::{Serialize, Serializer},
|
||||
};
|
||||
use serde_json::value::RawValue;
|
||||
@ -127,21 +127,19 @@ impl<T> Raw<T> {
|
||||
deserializer.deserialize_map(SingleFieldVisitor::new(field_name))
|
||||
}
|
||||
|
||||
/// Try to deserialize the JSON as a custom type.
|
||||
pub fn deserialize_as<U>(&self) -> serde_json::Result<U>
|
||||
/// Try to deserialize the JSON as the expected type.
|
||||
pub fn deserialize<'a>(&'a self) -> serde_json::Result<T>
|
||||
where
|
||||
U: DeserializeOwned,
|
||||
T: Deserialize<'a>,
|
||||
{
|
||||
serde_json::from_str(self.json.get())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Raw<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
/// Try to deserialize the JSON as the expected type.
|
||||
pub fn deserialize(&self) -> serde_json::Result<T> {
|
||||
/// Try to deserialize the JSON as a custom type.
|
||||
pub fn deserialize_as<'a, U>(&'a self) -> serde_json::Result<U>
|
||||
where
|
||||
U: Deserialize<'a>,
|
||||
{
|
||||
serde_json::from_str(self.json.get())
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user