on master help sign in
Add Version and Text.Lazy key encodings
commit6546d98f8456f772d7db8a0abd163035df767ac1 authorOleg Grenrus <oleg.grenrus@iki.fi> dateThu 9 Jun 2016, 12:50 (10 years ago) parente056318
4 files changed+30−6
Data/Aeson/Types/FromJSON.hs+17−5 Data/Aeson/Types/ToJSON.hs+8 aeson.cabal+1−1 tests/Properties.hs+4
Data/Aeson/Types/FromJSON.hs+17−5
@@ -1187,6 +1187,7 @@ instance FromJSON Word64 where
11871187instance FromJSONKey Word64 where
11881188 fromJSONKey = FromJSONKeyTextParser parseIntegralText
11891189
1190+
11901191instance FromJSON Text where
11911192 parseJSON = withText "Text" pure
11921193 {-# INLINE parseJSON #-}
@@ -1194,17 +1195,28 @@ instance FromJSON Text where
11941195instance FromJSONKey Text where
11951196 fromJSONKey = fromJSONKeyCoerce
11961197
1198+
11971199instance FromJSON LT.Text where
11981200 parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
11991201 {-# INLINE parseJSON #-}
12001202
1203+instance FromJSONKey LT.Text where
1204+ fromJSONKey = FromJSONKeyText LT.fromStrict
1205+
1206+
12011207instance FromJSON Version where
1208+ parseJSON = withText "Version" parseVersionText
12021209 {-# INLINE parseJSON #-}
1203− parseJSON = withText "Version" $ go . readP_to_S parseVersion . unpack
1204− where
1205− go [(v,[])] = return v
1206− go (_ : xs) = go xs
1207− go _ = fail $ "could not parse Version"
1210+
1211+instance FromJSONKey Version where
1212+ fromJSONKey = FromJSONKeyTextParser parseVersionText
1213+
1214+parseVersionText :: Text -> Parser Version
1215+parseVersionText = go . readP_to_S parseVersion . unpack
1216+ where
1217+ go [(v,[])] = return v
1218+ go (_ : xs) = go xs
1219+ go _ = fail $ "could not parse Version"
12081220
12091221-------------------------------------------------------------------------------
12101222-- semigroups NonEmpty
Data/Aeson/Types/ToJSON.hs+8
@@ -1502,6 +1502,7 @@ instance ToJSONKey Text where
15021502 toJSONKey = toJSONKeyText id
15031503 {-# INLINE toJSONKey #-}
15041504
1505+
15051506instance ToJSON LT.Text where
15061507 toJSON = String . LT.toStrict
15071508 {-# INLINE toJSON #-}
@@ -1509,6 +1510,10 @@ instance ToJSON LT.Text where
15091510 toEncoding = E.lazyText
15101511 {-# INLINE toEncoding #-}
15111512
1513+instance ToJSONKey LT.Text where
1514+ toJSONKey = toJSONKeyText LT.toStrict
1515+
1516+
15121517instance ToJSON Version where
15131518 toJSON = toJSON . showVersion
15141519 {-# INLINE toJSON #-}
@@ -1516,6 +1521,9 @@ instance ToJSON Version where
15161521 toEncoding = toEncoding . showVersion
15171522 {-# INLINE toEncoding #-}
15181523
1524+instance ToJSONKey Version where
1525+ toJSONKey = toJSONKeyText (T.pack . showVersion)
1526+
15191527-------------------------------------------------------------------------------
15201528-- semigroups NonEmpty
15211529-------------------------------------------------------------------------------
aeson.cabal+1−1
@@ -160,7 +160,7 @@ test-suite tests
160160 dlist,
161161 generic-deriving >= 1.10 && < 1.11,
162162 ghc-prim >= 0.2,
163− hashable >= 1.1.2.0,
163+ hashable >= 1.2.4.0,
164164 tagged,
165165 template-haskell,
166166 test-framework,
tests/Properties.hs+4
@@ -36,6 +36,7 @@ import qualified Data.ByteString.Lazy.Char8 as L
3636import qualified Data.HashMap.Strict as H
3737import qualified Data.Map as Map
3838import qualified Data.Text as T
39+import qualified Data.Text.Lazy as LT
3940import qualified Data.Vector as V
4041
4142encodeDouble :: Double -> Double -> Property
@@ -150,6 +151,7 @@ tests = testGroup "properties" [
150151 , testProperty "Integer" $ roundTripEq (1 :: Integer)
151152 , testProperty "String" $ roundTripEq ("" :: String)
152153 , testProperty "Text" $ roundTripEq T.empty
154+ , testProperty "Lazy Text" $ roundTripEq LT.empty
153155 , testProperty "Foo" $ roundTripEq (undefined :: Foo)
154156 , testProperty "Day" $ roundTripEq (undefined :: Day)
155157 , testProperty "DotNetTime" $ roundTripEq (undefined :: Approx DotNetTime)
@@ -191,6 +193,8 @@ tests = testGroup "properties" [
191193 , testProperty "TimeOfDay" $ roundTripKey (undefined :: TimeOfDay)
192194 , testProperty "UTCTime" $ roundTripKey (undefined :: UTCTime)
193195#endif
196+ , testProperty "Version" $ roundTripKey (undefined :: Version)
197+ , testProperty "Lazy Text" $ roundTripKey (undefined :: LT.Text)
194198 ]
195199 , testGroup "toFromJSON" [
196200 testProperty "Integer" (toFromJSON :: Integer -> Property)