on master help sign in
Merge pull request #354 from phadej/issue-351

Resolve #351

commit2f0b989432987a07937cff28ddc2ad1d496bf384 authorAdam Bergmark <adam@bergmark.nl> dateThu 4 Feb 2016, 15:11 (11 years ago) parent28f5f5c0718860
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 #-}
@@ -220,14 +221,14 @@ instance FromJSON () where
220221 else fail "Expected an empty array"
221222 {-# INLINE parseJSON #-}
222223
223−instance ToJSON [Char] where
224+instance INCOHERENT_ ToJSON [Char] where
224225 toJSON = String . T.pack
225226 {-# INLINE toJSON #-}
226227
227228 toEncoding = Encoding . E.string
228229 {-# INLINE toEncoding #-}
229230
230−instance FromJSON [Char] where
231+instance INCOHERENT_ FromJSON [Char] where
231232 parseJSON = withText "String" $ pure . T.unpack
232233 {-# INLINE parseJSON #-}
233234
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
@@ -11,6 +11,7 @@ import Data.Aeson.Internal (JSONPathElement(..), formatError)
1111import Data.Aeson.TH (deriveJSON)
1212import Data.Aeson.Types (ToJSON(..), Value, camelTo, camelTo2, defaultOptions, omitNothingFields)
1313import Data.Char (toUpper)
14+import Data.Maybe (fromMaybe)
1415import Data.Time (UTCTime)
1516import Data.Time.Format (parseTime)
1617import GHC.Generics (Generic)
@@ -56,6 +57,7 @@ tests = testGroup "unit" [
5657 , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
5758 , testGroup "To JSON representation" $ fmap (testCase "-") jsonEncoding
5859 , testGroup "From JSON representation" $ fmap (testCase "-") jsonDecoding
60+ , testGroup "Issue #351" $ fmap (testCase "-") issue351
5961 ]
6062
6163roundTripCamel :: String -> Assertion
@@ -229,6 +231,21 @@ jsonDecoding = [
229231 , assertEqual "Just Nothing" (Just Nothing :: Maybe (Maybe Int)) (decode "null")
230232 ]
231233
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+
232249------------------------------------------------------------------------------
233250-- Comparison between bytestring and text encoders
234251------------------------------------------------------------------------------