@@ -12,13 +12,14 @@ module UnitTests
12
12
(
13
13
ioTests
14
14
, tests
15
+ , withEmbeddedJSONTest
15
16
) where
16
17
17
18
import Prelude ()
18
19
import Prelude.Compat
19
20
20
21
import 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 )
22
23
import Data.Aeson.Internal (JSONPathElement (.. ), formatError )
23
24
import Data.Aeson.TH (deriveJSON , deriveToJSON , deriveToJSON1 )
24
25
import Data.Aeson.Text (encodeToTextBuilder )
@@ -93,6 +94,7 @@ tests = testGroup "unit" [
93
94
, testCase " Unescape string (PR #477)" unescapeString
94
95
, testCase " Show Options" showOptions
95
96
, testGroup " SingleMaybeField" singleMaybeField
97
+ , testCase " withEmbeddedJSON" withEmbeddedJSONTest
96
98
]
97
99
98
100
roundTripCamel :: String -> Assertion
@@ -515,6 +517,19 @@ singleMaybeField = do
515
517
v = SingleMaybeField Nothing
516
518
opts = defaultOptions{omitNothingFields= True ,unwrapUnaryRecords= True }
517
519
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
+
518
533
deriveJSON defaultOptions{omitNothingFields= True } ''MyRecord
519
534
520
535
deriveToJSON defaultOptions ''Foo
0 commit comments