@@ -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