on master help sign in
Resolve #351
commit0718860e88795849bf983b8f0b6e928ecce0fd9e authorOleg Grenrus <oleg.grenrus@iki.fi> dateWed 3 Feb 2016, 08:26 (11 years ago) parentfdb8672
3 files changed+27−2
Data/Aeson/Types/Instances.hs+3−2 include/overlapping-compat.h+7 tests/UnitTests.hs+17
Data/Aeson/Types/Instances.hs+3−2
@@ -4,6 +4,7 @@
44 ViewPatterns #-}
55{-# LANGUAGE DefaultSignatures #-}
66
7+#define NEEDS_INCOHERENT
78#include "overlapping-compat.h"
89
910{-# OPTIONS_GHC -fno-warn-orphans #-}
@@ -221,14 +222,14 @@ instance FromJSON () where
221222 else fail "Expected an empty array"
222223 {-# INLINE parseJSON #-}
223224
224−instance ToJSON [Char] where
225+instance INCOHERENT_ ToJSON [Char] where
225226 toJSON = String . T.pack
226227 {-# INLINE toJSON #-}
227228
228229 toEncoding = Encoding . E.string
229230 {-# INLINE toEncoding #-}
230231
231−instance FromJSON [Char] where
232+instance INCOHERENT_ FromJSON [Char] where
232233 parseJSON = withText "String" $ pure . T.unpack
233234 {-# INLINE parseJSON #-}
234235
include/overlapping-compat.h+7
@@ -1,8 +1,15 @@
11#if __GLASGOW_HASKELL__ >= 710
22#define OVERLAPPABLE_ {-# OVERLAPPABLE #-}
33#define OVERLAPPING_ {-# OVERLAPPING #-}
4+#ifdef NEEDS_INCOHERENT
5+#define INCOHERENT_ {-# INCOHERENT #-}
6+#endif
47#else
58{-# LANGUAGE OverlappingInstances #-}
69#define OVERLAPPABLE_
710#define OVERLAPPING_
11+#ifdef NEEDS_INCOHERENT
12+{-# LANGUAGE IncoherentInstances #-}
13+#define INCOHERENT_
14+#endif
815#endif
tests/UnitTests.hs+17
@@ -10,6 +10,7 @@ import Data.Aeson.Encode (encodeToTextBuilder)
1010import Data.Aeson.TH (deriveJSON)
1111import Data.Aeson.Types (ToJSON(..), Value, camelTo, camelTo2, defaultOptions, omitNothingFields)
1212import Data.Char (toUpper)
13+import Data.Maybe (fromMaybe)
1314import Data.Time (UTCTime)
1415import Data.Time.Format (parseTime)
1516import GHC.Generics (Generic)
@@ -52,6 +53,7 @@ tests = testGroup "unit" [
5253 , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
5354 , testGroup "To JSON representation" $ fmap (testCase "-") jsonEncoding
5455 , testGroup "From JSON representation" $ fmap (testCase "-") jsonDecoding
56+ , testGroup "Issue #351" $ fmap (testCase "-") issue351
5557 ]
5658
5759roundTripCamel :: String -> Assertion
@@ -218,6 +220,21 @@ jsonDecoding = [
218220 , assertEqual "Just Nothing" (Just Nothing :: Maybe (Maybe Int)) (decode "null")
219221 ]
220222
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+
221238------------------------------------------------------------------------------
222239-- Comparison between bytestring and text encoders
223240------------------------------------------------------------------------------