@@ -16,13 +16,14 @@ module UnitTests
1616 (
1717 ioTests
1818 , tests
19+ , withEmbeddedJSONTest
1920 ) where
2021
2122import Prelude ()
2223import Prelude.Compat
2324
2425import Control.Monad (forM, forM_)
25−import Data.Aeson ((.=), (.:), (.:?), (.:!), FromJSON(..), FromJSONKeyFunction(..), FromJSONKey(..), ToJSON1(..), decode, eitherDecode, encode, fromJSON, genericParseJSON, genericToEncoding, genericToJSON, object, withObject)
26+import Data.Aeson ((.=), (.:), (.:?), (.:!), FromJSON(..), FromJSONKeyFunction(..), FromJSONKey(..), ToJSON1(..), decode, eitherDecode, encode, fromJSON, genericParseJSON, genericToEncoding, genericToJSON, object, withObject, withEmbeddedJSON)
2627import Data.Aeson.Internal (JSONPathElement(..), formatError)
2728import Data.Aeson.TH (deriveJSON, deriveToJSON, deriveToJSON1)
2829import Data.Aeson.Text (encodeToTextBuilder)
@@ -97,6 +98,7 @@ tests = testGroup "unit" [
9798 , testCase "Unescape string (PR #477)" unescapeString
9899 , testCase "Show Options" showOptions
99100 , testGroup "SingleMaybeField" singleMaybeField
101+ , testCase "withEmbeddedJSON" withEmbeddedJSONTest
100102 ]
101103
102104roundTripCamel :: String -> Assertion
@@ -519,6 +521,19 @@ singleMaybeField = do
519521 v = SingleMaybeField Nothing
520522 opts = defaultOptions{omitNothingFields=True,unwrapUnaryRecords=True}
521523
524+
525+newtype EmbeddedJSONTest = EmbeddedJSONTest Int
526+ deriving (Eq, Show)
527+
528+instance FromJSON EmbeddedJSONTest where
529+ parseJSON =
530+ withObject "Object" $ \o ->
531+ EmbeddedJSONTest <$> (o .: "prop" >>= withEmbeddedJSON "Quoted Int" parseJSON)
532+
533+withEmbeddedJSONTest :: Assertion
534+withEmbeddedJSONTest =
535+ assertEqual "Unquote embedded JSON" (Right $ EmbeddedJSONTest 1) (eitherDecode "{\"prop\":\"1\"}")
536+
522537deriveJSON defaultOptions{omitNothingFields=True} ''MyRecord
523538
524539deriveToJSON defaultOptions ''Foo