Skip to content

Commit 88a6f94

Browse files
committed
Support JSON[]
1 parent a2487c0 commit 88a6f94

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ types. The driver currently supports the following conversions:
282282
<td>types::array::ArrayBase&lt;Option&lt;~str&gt;&gt;</td>
283283
<td>TEXT[], CHAR(n)[], VARCHAR[], TEXT[][], ...</td>
284284
</tr>
285+
<tr>
286+
<td>types::array::ArrayBase&lt;Option&lt;Json&gt;&gt;</td>
287+
<td>JSON[], JSON[][], ...</td>
288+
</tr>
285289
<tr>
286290
<td>types::array::ArrayBase&lt;Option&lt;i64&gt;&gt;</td>
287291
<td>INT8[], INT8[][], ...</td>

test.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,3 +796,14 @@ fn test_dns_failure() {
796796
_ => fail!("Expected error")
797797
}
798798
}
799+
800+
#[test]
801+
fn test_jsonarray_params() {
802+
test_array_params!("JSON",
803+
json::from_str("[10, 11, 12]").unwrap(),
804+
"\"[10,11,12]\"",
805+
json::from_str(r#"{"a": 10, "b": null}"#).unwrap(),
806+
r#""{\"a\": 10, \"b\": null}""#,
807+
json::from_str(r#"{"a": [10], "b": true}"#).unwrap(),
808+
r#""{\"a\": [10], \"b\": true}""#);
809+
}

types/mod.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static INT2OID: Oid = 21;
3030
static INT4OID: Oid = 23;
3131
static TEXTOID: Oid = 25;
3232
static JSONOID: Oid = 114;
33+
static JSONARRAYOID: Oid = 199;
3334
static FLOAT4OID: Oid = 700;
3435
static FLOAT8OID: Oid = 701;
3536
static 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)
341344
from_range_impl!(PgInt8Range, i64)
342345
from_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+
344355
macro_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)
379390
from_raw_from_impl!(PgFloat4, f32)
380391
from_raw_from_impl!(PgFloat8, f64)
381392
from_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

387395
from_raw_from_impl!(PgTimestamp | PgTimestampTZ, Timespec)
388396
from_raw_from_impl!(PgInt4Range, Range<i32>)
@@ -431,6 +439,7 @@ from_array_impl!(PgInt4Array, i32)
431439
from_array_impl!(PgTextArray | PgCharNArray | PgVarcharArray, ~str)
432440
from_array_impl!(PgInt8Array, i64)
433441
from_array_impl!(PgTimestampArray | PgTimestampTZArray, Timespec)
442+
from_array_impl!(PgJsonArray, Json)
434443
from_array_impl!(PgFloat4Array, f32)
435444
from_array_impl!(PgFloat8Array, f64)
436445
from_array_impl!(PgUuidArray, Uuid)
@@ -580,6 +589,12 @@ to_range_impl!(PgInt4Range, i32)
580589
to_range_impl!(PgInt8Range, i64)
581590
to_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+
583598
macro_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(
629644
to_raw_to_impl!(PgBool, bool)
630645
to_raw_to_impl!(PgByteA, ~[u8])
631646
to_raw_to_impl!(PgVarchar | PgText | PgCharN, ~str)
647+
to_raw_to_impl!(PgJson, Json)
632648
to_raw_to_impl!(PgChar, i8)
633649
to_raw_to_impl!(PgInt2, i16)
634650
to_raw_to_impl!(PgInt4, i32)
@@ -657,15 +673,6 @@ impl<'self> ToSql for &'self [u8] {
657673
658674
to_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-
669676
to_raw_to_impl!(PgTimestamp | PgTimestampTZ, Timespec)
670677
to_raw_to_impl!(PgUuid, Uuid)
671678
@@ -720,6 +727,7 @@ to_array_impl!(PgUuidArray, Uuid)
720727
to_array_impl!(PgInt4RangeArray, Range<i32>)
721728
to_array_impl!(PgTsRangeArray | PgTstzRangeArray, Range<Timespec>)
722729
to_array_impl!(PgInt8RangeArray, Range<i64>)
730+
to_array_impl!(PgJsonArray, Json)
723731
724732
impl<'self> ToSql for HashMap<~str, Option<~str>> {
725733
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {

0 commit comments

Comments
 (0)