Add back in serde 0.3 support for back compat

This is a bit of a dubious hack since anyone that tries to turn on both
serde and serde_json are screwed.
This commit is contained in:
Steven Fackler 2015-11-07 16:36:18 -08:00
parent d3182d96a6
commit 65b3234626
3 changed files with 44 additions and 0 deletions

View File

@ -32,6 +32,7 @@ rustc-serialize = "0.3"
net2 = { version = "0.2", features = ["nightly"] }
chrono = { version = "0.2.14", optional = true }
openssl = { version = "0.6.4", optional = true }
serde = { version = "0.3", optional = true } # Delete for 0.11
serde_json = { version = "0.6", optional = true }
time = { version = "0.1.14", optional = true }
unix_socket = { version = ">= 0.3, < 0.5", optional = true, features = ["socket_timeout"] }

View File

@ -51,6 +51,8 @@ mod slice;
mod rustc_serialize;
#[cfg(feature = "serde_json")]
mod serde_json;
#[cfg(feature = "serde")]
mod serde;
#[cfg(feature = "chrono")]
mod chrono;

41
src/types/serde.rs Normal file
View File

@ -0,0 +1,41 @@
extern crate serde;
use std::error;
use std::io::prelude::*;
use byteorder::{ReadBytesExt, WriteBytesExt};
use self::serde::json::{self, Value};
use Result;
use error::Error;
use types::{FromSql, ToSql, IsNull, Type, SessionInfo};
impl FromSql for Value {
fn from_sql<R: Read>(ty: &Type, raw: &mut R, _: &SessionInfo) -> Result<Value> {
if let Type::Jsonb = *ty {
// We only support version 1 of the jsonb binary format
if try!(raw.read_u8()) != 1 {
let err: Box<error::Error+Sync+Send> = "unsupported JSONB encoding version".into();
return Err(Error::Conversion(err));
}
}
json::de::from_reader(raw).map_err(|err| Error::Conversion(Box::new(err)))
}
accepts!(Type::Json, Type::Jsonb);
}
impl ToSql for Value {
fn to_sql<W: Write+?Sized>(&self, ty: &Type, mut out: &mut W, _: &SessionInfo)
-> Result<IsNull> {
if let Type::Jsonb = *ty {
try!(out.write_u8(1));
}
try!(write!(out, "{:?}", self));
Ok(IsNull::No)
}
accepts!(Type::Json, Type::Jsonb);
to_sql_checked!();
}