on master help sign in
Merge pull request #354 from phadej/issue-351
2f0b989Adam Bergmark
Data/Aeson/Types/Instances.hs+3−2
@@ -4,6 +4,7 @@
4 ViewPatterns #-}
4 ViewPatterns #-}
5{-# LANGUAGE DefaultSignatures #-}
5{-# LANGUAGE DefaultSignatures #-}
6
6
7+#define NEEDS_INCOHERENT
7#include "overlapping-compat.h"
8#include "overlapping-compat.h"
8
9
9{-# OPTIONS_GHC -fno-warn-orphans #-}
10{-# OPTIONS_GHC -fno-warn-orphans #-}
@@ -220,14 +221,14 @@ instance FromJSON () where
220 else fail "Expected an empty array"
221 else fail "Expected an empty array"
221 {-# INLINE parseJSON #-}
222 {-# INLINE parseJSON #-}
222
223
223−instance ToJSON [Char] where
224+instance INCOHERENT_ ToJSON [Char] where
224 toJSON = String . T.pack
225 toJSON = String . T.pack
225 {-# INLINE toJSON #-}
226 {-# INLINE toJSON #-}
226
227
227 toEncoding = Encoding . E.string
228 toEncoding = Encoding . E.string
228 {-# INLINE toEncoding #-}
229 {-# INLINE toEncoding #-}
229
230
230−instance FromJSON [Char] where
231+instance INCOHERENT_ FromJSON [Char] where
231 parseJSON = withText "String" $ pure . T.unpack
232 parseJSON = withText "String" $ pure . T.unpack
232 {-# INLINE parseJSON #-}
233 {-# INLINE parseJSON #-}
233
234
include/overlapping-compat.h+7
@@ -1,8 +1,15 @@
1#if __GLASGOW_HASKELL__ >= 710
1#if __GLASGOW_HASKELL__ >= 710
2#define OVERLAPPABLE_ {-# OVERLAPPABLE #-}
2#define OVERLAPPABLE_ {-# OVERLAPPABLE #-}
3#define OVERLAPPING_ {-# OVERLAPPING #-}
3#define OVERLAPPING_ {-# OVERLAPPING #-}
4+#ifdef NEEDS_INCOHERENT
5+#define INCOHERENT_ {-# INCOHERENT #-}
6+#endif
4#else
7#else
5{-# LANGUAGE OverlappingInstances #-}
8{-# LANGUAGE OverlappingInstances #-}
6#define OVERLAPPABLE_
9#define OVERLAPPABLE_
7#define OVERLAPPING_
10#define OVERLAPPING_
11+#ifdef NEEDS_INCOHERENT
12+{-# LANGUAGE IncoherentInstances #-}
13+#define INCOHERENT_
14+#endif
8#endif
15#endif
tests/UnitTests.hs+17
@@ -11,6 +11,7 @@ import Data.Aeson.Internal (JSONPathElement(..), formatError)
11import Data.Aeson.TH (deriveJSON)
11import Data.Aeson.TH (deriveJSON)
12import Data.Aeson.Types (ToJSON(..), Value, camelTo, camelTo2, defaultOptions, omitNothingFields)
12import Data.Aeson.Types (ToJSON(..), Value, camelTo, camelTo2, defaultOptions, omitNothingFields)
13import Data.Char (toUpper)
13import Data.Char (toUpper)
14+import Data.Maybe (fromMaybe)
14import Data.Time (UTCTime)
15import Data.Time (UTCTime)
15import Data.Time.Format (parseTime)
16import Data.Time.Format (parseTime)
16import GHC.Generics (Generic)
17import GHC.Generics (Generic)
@@ -56,6 +57,7 @@ tests = testGroup "unit" [
56 , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
57 , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
57 , testGroup "To JSON representation" $ fmap (testCase "-") jsonEncoding
58 , testGroup "To JSON representation" $ fmap (testCase "-") jsonEncoding
58 , testGroup "From JSON representation" $ fmap (testCase "-") jsonDecoding
59 , testGroup "From JSON representation" $ fmap (testCase "-") jsonDecoding
60+ , testGroup "Issue #351" $ fmap (testCase "-") issue351
59 ]
61 ]
60
62
61roundTripCamel :: String -> Assertion
63roundTripCamel :: String -> Assertion
@@ -229,6 +231,21 @@ jsonDecoding = [
229 , assertEqual "Just Nothing" (Just Nothing :: Maybe (Maybe Int)) (decode "null")
231 , assertEqual "Just Nothing" (Just Nothing :: Maybe (Maybe Int)) (decode "null")
230 ]
232 ]
231
233
234+------------------------------------------------------------------------------
235+-- Regressions
236+------------------------------------------------------------------------------
237+
238+-- A regression test for: https://github.com/bos/aeson/issues/351
239+overlappingRegression :: FromJSON a => L.ByteString -> [a]
240+overlappingRegression bs = fromMaybe [] $ decode bs
241+
242+issue351 :: [Assertion]
243+issue351 = [
244+ assertEqual "Int" ([1, 2, 3] :: [Int]) $ overlappingRegression "[1, 2, 3]"
245+ , assertEqual "Char" ("" :: String) $ overlappingRegression "\"abc\""
246+ , assertEqual "Char" ("abc" :: String) $ overlappingRegression "[\"a\", \"b\", \"c\"]"
247+ ]
248+
232------------------------------------------------------------------------------
249------------------------------------------------------------------------------
233-- Comparison between bytestring and text encoders
250-- Comparison between bytestring and text encoders
234------------------------------------------------------------------------------
251------------------------------------------------------------------------------