on master help sign in
Resolve #351
0718860Oleg Grenrus
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 #-}
@@ -221,14 +222,14 @@ instance FromJSON () where
221 else fail "Expected an empty array"
222 else fail "Expected an empty array"
222 {-# INLINE parseJSON #-}
223 {-# INLINE parseJSON #-}
223
224
224−instance ToJSON [Char] where
225+instance INCOHERENT_ ToJSON [Char] where
225 toJSON = String . T.pack
226 toJSON = String . T.pack
226 {-# INLINE toJSON #-}
227 {-# INLINE toJSON #-}
227
228
228 toEncoding = Encoding . E.string
229 toEncoding = Encoding . E.string
229 {-# INLINE toEncoding #-}
230 {-# INLINE toEncoding #-}
230
231
231−instance FromJSON [Char] where
232+instance INCOHERENT_ FromJSON [Char] where
232 parseJSON = withText "String" $ pure . T.unpack
233 parseJSON = withText "String" $ pure . T.unpack
233 {-# INLINE parseJSON #-}
234 {-# INLINE parseJSON #-}
234
235
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
@@ -10,6 +10,7 @@ import Data.Aeson.Encode (encodeToTextBuilder)
10import Data.Aeson.TH (deriveJSON)
10import Data.Aeson.TH (deriveJSON)
11import Data.Aeson.Types (ToJSON(..), Value, camelTo, camelTo2, defaultOptions, omitNothingFields)
11import Data.Aeson.Types (ToJSON(..), Value, camelTo, camelTo2, defaultOptions, omitNothingFields)
12import Data.Char (toUpper)
12import Data.Char (toUpper)
13+import Data.Maybe (fromMaybe)
13import Data.Time (UTCTime)
14import Data.Time (UTCTime)
14import Data.Time.Format (parseTime)
15import Data.Time.Format (parseTime)
15import GHC.Generics (Generic)
16import GHC.Generics (Generic)
@@ -52,6 +53,7 @@ tests = testGroup "unit" [
52 , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
53 , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
53 , testGroup "To JSON representation" $ fmap (testCase "-") jsonEncoding
54 , testGroup "To JSON representation" $ fmap (testCase "-") jsonEncoding
54 , testGroup "From JSON representation" $ fmap (testCase "-") jsonDecoding
55 , testGroup "From JSON representation" $ fmap (testCase "-") jsonDecoding
56+ , testGroup "Issue #351" $ fmap (testCase "-") issue351
55 ]
57 ]
56
58
57roundTripCamel :: String -> Assertion
59roundTripCamel :: String -> Assertion
@@ -218,6 +220,21 @@ jsonDecoding = [
218 , assertEqual "Just Nothing" (Just Nothing :: Maybe (Maybe Int)) (decode "null")
220 , assertEqual "Just Nothing" (Just Nothing :: Maybe (Maybe Int)) (decode "null")
219 ]
221 ]
220
222
223+------------------------------------------------------------------------------
224+-- Regressions
225+------------------------------------------------------------------------------
226+
227+-- A regression test for: https://github.com/bos/aeson/issues/351
228+overlappingRegression :: FromJSON a => L.ByteString -> [a]
229+overlappingRegression bs = fromMaybe [] $ decode bs
230+
231+issue351 :: [Assertion]
232+issue351 = [
233+ assertEqual "Int" ([1, 2, 3] :: [Int]) $ overlappingRegression "[1, 2, 3]"
234+ , assertEqual "Char" ("" :: String) $ overlappingRegression "\"abc\""
235+ , assertEqual "Char" ("abc" :: String) $ overlappingRegression "[\"a\", \"b\", \"c\"]"
236+ ]
237+
221------------------------------------------------------------------------------
238------------------------------------------------------------------------------
222-- Comparison between bytestring and text encoders
239-- Comparison between bytestring and text encoders
223------------------------------------------------------------------------------
240------------------------------------------------------------------------------