@@ -30,6 +30,7 @@ static INT2OID: Oid = 21;
3030static INT4OID : Oid = 23 ;
3131static TEXTOID : Oid = 25 ;
3232static JSONOID : Oid = 114 ;
33+ static JSONARRAYOID : Oid = 199 ;
3334static FLOAT4OID : Oid = 700 ;
3435static FLOAT8OID : Oid = 701 ;
3536static BOOLARRAYOID : Oid = 1000 ;
@@ -147,6 +148,8 @@ make_postgres_type!(
147148 TEXTOID => PgText ,
148149 #[ doc="JSON" ]
149150 JSONOID => PgJson ,
151+ #[ doc="JSON[]" ]
152+ JSONARRAYOID => PgJsonArray member PgJson ,
150153 #[ doc="FLOAT4/REAL" ]
151154 FLOAT4OID => PgFloat4 ,
152155 #[ doc="FLOAT8/DOUBLE PRECISION" ]
@@ -341,6 +344,14 @@ from_range_impl!(PgInt4Range, i32)
341344from_range_impl!( PgInt8Range , i64 )
342345from_range_impl!( PgTsRange | PgTstzRange , Timespec )
343346
347+ impl RawFromSql for Json {
348+ fn raw_from_sql<R : Reader >( len: uint, raw: & mut R ) -> Json {
349+ // TODO this should really use from_reader, but that seems to overshoot
350+ let s = raw. read_bytes( len) ;
351+ json:: from_str( str :: from_utf8_owned( s) ) . unwrap( )
352+ }
353+ }
354+
344355macro_rules! from_map_impl(
345356 ( $( $expected: pat) |+, $t: ty, $blk: expr) => (
346357 impl FromSql for Option <$t> {
@@ -379,10 +390,7 @@ from_raw_from_impl!(PgInt8, i64)
379390from_raw_from_impl!( PgFloat4 , f32 )
380391from_raw_from_impl!( PgFloat8 , f64 )
381392from_raw_from_impl!( PgUuid , Uuid )
382-
383- from_map_impl!( PgJson , Json , |buf| {
384- json:: from_str( str :: from_utf8( buf. as_slice( ) ) ) . unwrap( )
385- } )
393+ from_raw_from_impl!( PgJson , Json )
386394
387395from_raw_from_impl!( PgTimestamp | PgTimestampTZ , Timespec )
388396from_raw_from_impl!( PgInt4Range , Range <i32 >)
@@ -431,6 +439,7 @@ from_array_impl!(PgInt4Array, i32)
431439from_array_impl!( PgTextArray | PgCharNArray | PgVarcharArray , ~str )
432440from_array_impl!( PgInt8Array , i64 )
433441from_array_impl!( PgTimestampArray | PgTimestampTZArray , Timespec )
442+ from_array_impl!( PgJsonArray , Json )
434443from_array_impl!( PgFloat4Array , f32 )
435444from_array_impl!( PgFloat8Array , f64 )
436445from_array_impl!( PgUuidArray , Uuid )
@@ -580,6 +589,12 @@ to_range_impl!(PgInt4Range, i32)
580589to_range_impl!(PgInt8Range, i64)
581590to_range_impl!(PgTsRange | PgTstzRange, Timespec)
582591
592+ impl RawToSql for Json {
593+ fn raw_to_sql<W: Writer>(&self, raw: &mut W) {
594+ self.to_writer(raw as &mut Writer)
595+ }
596+ }
597+
583598macro_rules! to_option_impl(
584599 ($($oid:pat)|+, $t:ty) => (
585600 impl ToSql for Option<$t> {
@@ -629,6 +644,7 @@ macro_rules! to_raw_to_impl(
629644to_raw_to_impl!(PgBool, bool)
630645to_raw_to_impl!(PgByteA, ~[u8])
631646to_raw_to_impl!(PgVarchar | PgText | PgCharN, ~str)
647+ to_raw_to_impl!(PgJson, Json)
632648to_raw_to_impl!(PgChar, i8)
633649to_raw_to_impl!(PgInt2, i16)
634650to_raw_to_impl!(PgInt4, i32)
@@ -657,15 +673,6 @@ impl<'self> ToSql for &'self [u8] {
657673
658674to_option_impl_self!(PgByteA, &'self [u8])
659675
660- impl ToSql for Json {
661- fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {
662- check_types!(PgJson, ty)
663- (Text, Some(self.to_str().into_bytes()))
664- }
665- }
666-
667- to_option_impl!(PgJson, Json)
668-
669676to_raw_to_impl!(PgTimestamp | PgTimestampTZ, Timespec)
670677to_raw_to_impl!(PgUuid, Uuid)
671678
@@ -720,6 +727,7 @@ to_array_impl!(PgUuidArray, Uuid)
720727to_array_impl!(PgInt4RangeArray, Range<i32>)
721728to_array_impl!(PgTsRangeArray | PgTstzRangeArray, Range<Timespec>)
722729to_array_impl!(PgInt8RangeArray, Range<i64>)
730+ to_array_impl!(PgJsonArray, Json)
723731
724732impl<'self> ToSql for HashMap<~str, Option<~str>> {
725733 fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {
0 commit comments