serde: Make Raw deserialization methods more general

This commit is contained in:
Jonas Platte 2021-08-09 19:34:50 +02:00
parent 165729fe71
commit fdf7af442c
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
2 changed files with 13 additions and 11 deletions

View File

@ -1,5 +1,9 @@
# [unreleased]
Improvements:
* Make `Raw::deserialize` & `Raw::deserialize_as` more general
# 0.4.1
Improvements:

View File

@ -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())
}
}