Skip to content

Commit ff1fc5a

Browse files
committed
Add withEmbeddedJSON test case
1 parent a09ecee commit ff1fc5a

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

tests/UnitTests.hs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ module UnitTests
1212
(
1313
ioTests
1414
, tests
15+
, withEmbeddedJSONTest
1516
) where
1617

1718
import Prelude ()
1819
import Prelude.Compat
1920

2021
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)
2223
import Data.Aeson.Internal (JSONPathElement(..), formatError)
2324
import Data.Aeson.TH (deriveJSON, deriveToJSON, deriveToJSON1)
2425
import 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

98100
roundTripCamel :: 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+
518533
deriveJSON defaultOptions{omitNothingFields=True} ''MyRecord
519534

520535
deriveToJSON defaultOptions ''Foo

0 commit comments

Comments
 (0)