diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bb30074c..ab89513aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## unreleased + +- Allow nested objects and arrays in JSON post for jsonb columns + ## [0.2.8.0] - 2015-04-17 ### Added - Option to specify nulls first or last, eg `/people?order=age.desc.nullsfirst` diff --git a/src/PgQuery.hs b/src/PgQuery.hs index 9e97decfe..ec58c08a6 100644 --- a/src/PgQuery.hs +++ b/src/PgQuery.hs @@ -268,7 +268,7 @@ unquoted (JSON.String t) = t unquoted (JSON.Number n) = cs $ formatScientific Fixed (if isInteger n then Just 0 else Nothing) n unquoted (JSON.Bool b) = cs . show $ b -unquoted _ = "" +unquoted v = cs $ JSON.encode v insertableText :: T.Text -> T.Text insertableText = (<> "::unknown") . pgFmtLit diff --git a/test/Feature/InsertSpec.hs b/test/Feature/InsertSpec.hs index b8ffa831c..3581a1869 100644 --- a/test/Feature/InsertSpec.hs +++ b/test/Feature/InsertSpec.hs @@ -94,6 +94,22 @@ spec = afterAll_ resetDb $ around withApp $ do , matchHeaders = [] } + context "jsonb" . after_ (clearTable "json") $ do + it "serializes nested object" $ do + let inserted = [json| { "data": { "foo":"bar" } } |] + p <- request methodPost "json" [("Prefer", "return=representation")] inserted + liftIO $ do + simpleBody p `shouldBe` inserted + simpleHeaders p `shouldSatisfy` matchHeader hLocation "/json\\?data=eq\\.%7B%22foo%22%3A%22bar%22%7D" + simpleStatus p `shouldBe` created201 + it "serializes nested array" $ do + let inserted = [json| { "data": [1,2,3] } |] + p <- request methodPost "json" [("Prefer", "return=representation")] inserted + liftIO $ do + simpleBody p `shouldBe` inserted + simpleHeaders p `shouldSatisfy` matchHeader hLocation "/json\\?data=eq\\.%5B1%2C2%2C3%5D" + simpleStatus p `shouldBe` created201 + describe "CSV insert" $ do after_ (clearTable "menagerie") . context "disparate csv types" $