on master help sign in
Add withEmbeddedJSON test case
commitff1fc5a9d09dffb46a9c28cbbb67f200b1a7ec78 authorAdam Bergmark <adam@bergmark.nl> dateThu 12 Oct 2017, 19:24 (9 years ago) parenta09ecee
1 file changed+16−1
tests/UnitTests.hs+16−1
tests/UnitTests.hs+16−1
@@ -12,13 +12,14 @@ module UnitTests
1212 (
1313 ioTests
1414 , tests
15+ , withEmbeddedJSONTest
1516 ) where
1617
1718import Prelude ()
1819import Prelude.Compat
1920
2021import Control.Monad (forM, forM_)
21−import Data.Aeson ((.=), (.:), (.:?), (.:!), FromJSON(..), FromJSONKeyFunction(..), FromJSONKey(..), ToJSON1(..), decode, eitherDecode, encode, fromJSON, genericParseJSON, genericToEncoding, genericToJSON, object, withObject)
22+import Data.Aeson ((.=), (.:), (.:?), (.:!), FromJSON(..), FromJSONKeyFunction(..), FromJSONKey(..), ToJSON1(..), decode, eitherDecode, encode, fromJSON, genericParseJSON, genericToEncoding, genericToJSON, object, withObject, withEmbeddedJSON)
2223import Data.Aeson.Internal (JSONPathElement(..), formatError)
2324import Data.Aeson.TH (deriveJSON, deriveToJSON, deriveToJSON1)
2425import Data.Aeson.Text (encodeToTextBuilder)
@@ -93,6 +94,7 @@ tests = testGroup "unit" [
9394 , testCase "Unescape string (PR #477)" unescapeString
9495 , testCase "Show Options" showOptions
9596 , testGroup "SingleMaybeField" singleMaybeField
97+ , testCase "withEmbeddedJSON" withEmbeddedJSONTest
9698 ]
9799
98100roundTripCamel :: String -> Assertion
@@ -515,6 +517,19 @@ singleMaybeField = do
515517 v = SingleMaybeField Nothing
516518 opts = defaultOptions{omitNothingFields=True,unwrapUnaryRecords=True}
517519
520+
521+newtype EmbeddedJSONTest = EmbeddedJSONTest Int
522+ deriving (Eq, Show)
523+
524+instance FromJSON EmbeddedJSONTest where
525+ parseJSON =
526+ withObject "Object" $ \o ->
527+ EmbeddedJSONTest <$> (o .: "prop" >>= withEmbeddedJSON "Quoted Int" parseJSON)
528+
529+withEmbeddedJSONTest :: Assertion
530+withEmbeddedJSONTest =
531+ assertEqual "Unquote embedded JSON" (Right $ EmbeddedJSONTest 1) (eitherDecode "{\"prop\":\"1\"}")
532+
518533deriveJSON defaultOptions{omitNothingFields=True} ''MyRecord
519534
520535deriveToJSON defaultOptions ''Foo