diff --git a/src/types/serde_json.rs b/src/types/serde_json.rs index f018032..a9761bd 100644 --- a/src/types/serde_json.rs +++ b/src/types/serde_json.rs @@ -17,12 +17,8 @@ impl ToSql for Value { impl FromSql for Value { #[inline] fn column_result(value: ValueRef<'_>) -> FromSqlResult { - match value { - ValueRef::Text(s) => serde_json::from_slice(s), - ValueRef::Blob(b) => serde_json::from_slice(b), - _ => return Err(FromSqlError::InvalidType), - } - .map_err(|err| FromSqlError::Other(Box::new(err))) + let bytes = value.as_bytes()?; + serde_json::from_slice(bytes).map_err(|err| FromSqlError::Other(Box::new(err))) } } diff --git a/src/types/value_ref.rs b/src/types/value_ref.rs index 446ad08..20a9f63 100644 --- a/src/types/value_ref.rs +++ b/src/types/value_ref.rs @@ -77,6 +77,16 @@ impl<'a> ValueRef<'a> { _ => Err(FromSqlError::InvalidType), } } + + /// Returns the byte slice that makes up this ValueRef if it's either + /// [`ValueRef::Blob`] or [`ValueRef::Text`]. + #[inline] + pub fn as_bytes(&self) -> FromSqlResult<&'a [u8]> { + match self { + ValueRef::Text(s) | ValueRef::Blob(s) => Ok(s), + _ => Err(FromSqlError::InvalidType), + } + } } impl From> for Value {