Skip to content

Commit 624a91c

Browse files
committed
Resolve #842: Add instances for network-uri's URI
1 parent 352b9ce commit 624a91c

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

aeson.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ library
115115
, hashable >=1.3.5.0 && <1.5
116116
, indexed-traversable >=0.1.2 && <0.2
117117
, integer-conversion >=0.1 && <0.2
118+
, network-uri >=2.6.4.1 && <2.7
118119
, OneTuple >=0.4.1.1 && <0.5
119120
, primitive >=0.8.0.0 && <0.9
120121
, QuickCheck >=2.14.3 && <2.15
@@ -202,6 +203,7 @@ test-suite aeson-tests
202203
, hashable
203204
, indexed-traversable
204205
, integer-logarithms >=1 && <1.1
206+
, network-uri
205207
, OneTuple
206208
, primitive
207209
, QuickCheck >=2.14.2 && <2.15

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ For the latest version of this document, please see [https://github.com/haskell/
3232
The new native Haskell implementation (introduced in version 2.0.3.0) is at least as fast.
3333
* Drop instances for `Number` from `attoparsec` package.
3434
* Improve `Arbitrary Value` instance.
35+
* Add instances for `URI` from `network-uri`.
3536

3637
### 2.1.2.1
3738

src/Data/Aeson/Types/FromJSON.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ import qualified Data.Vector.Generic as VG
151151
import qualified Data.Vector.Primitive as VP
152152
import qualified Data.Vector.Storable as VS
153153
import qualified Data.Vector.Unboxed as VU
154+
import qualified Network.URI as URI
154155

155156
import qualified GHC.Exts as Exts
156157
import qualified Data.Primitive.Array as PM
@@ -2423,6 +2424,23 @@ instance (FromJSON1 f, Functor f) => FromJSON (F.Mu f) where
24232424
instance (FromJSON1 f, Functor f) => FromJSON (F.Nu f) where
24242425
parseJSON = fmap (F.unfoldNu F.unFix) . parseJSON
24252426

2427+
-------------------------------------------------------------------------------
2428+
-- network-uri
2429+
-------------------------------------------------------------------------------
2430+
2431+
-- | @since 2.2.0.0
2432+
instance FromJSON URI.URI where
2433+
parseJSON = withText "URI" parseURI
2434+
2435+
-- | @since 2.2.0.0
2436+
instance FromJSONKey URI.URI where
2437+
fromJSONKey = FromJSONKeyTextParser parseURI
2438+
2439+
parseURI :: Text -> Parser URI.URI
2440+
parseURI t = case URI.parseURI (T.unpack t) of
2441+
Nothing -> fail "Invalid URI"
2442+
Just x -> return x
2443+
24262444
-------------------------------------------------------------------------------
24272445
-- strict
24282446
-------------------------------------------------------------------------------

src/Data/Aeson/Types/ToJSON.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ import qualified Data.Vector.Mutable as VM
124124
import qualified Data.Vector.Primitive as VP
125125
import qualified Data.Vector.Storable as VS
126126
import qualified Data.Vector.Unboxed as VU
127+
import qualified Network.URI as URI
127128

128129
import qualified Data.Aeson.Encoding.Builder as EB
129130
import qualified Data.ByteString.Builder as B
@@ -2188,6 +2189,22 @@ instance (ToJSON1 f, Functor f) => ToJSON (F.Nu f) where
21882189
toJSON = F.foldNu (liftToJSON (const False) id (listValue id))
21892190
toEncoding = F.foldNu (liftToEncoding (const False) id (listEncoding id))
21902191

2192+
-------------------------------------------------------------------------------
2193+
-- network-uri
2194+
-------------------------------------------------------------------------------
2195+
2196+
-- | @since 2.2.0.0
2197+
instance ToJSON URI.URI where
2198+
toJSON uri = toJSON (URI.uriToString id uri "")
2199+
toEncoding = encodeURI
2200+
2201+
-- | @since 2.2.0.0
2202+
instance ToJSONKey URI.URI where
2203+
toJSONKey = toJSONKeyTextEnc encodeURI
2204+
2205+
encodeURI :: URI.URI -> Encoding' a
2206+
encodeURI uri = E.string (URI.uriToString id uri "")
2207+
21912208
-------------------------------------------------------------------------------
21922209
-- strict
21932210
-------------------------------------------------------------------------------

tests/Instances.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ import Prelude.Compat
1313

1414
import Control.Applicative (empty)
1515
import Control.Monad
16+
import Data.Maybe (mapMaybe)
1617
import Data.Aeson.Types
1718
import Data.Function (on)
1819
import Data.Time (ZonedTime(..), TimeZone(..))
1920
import Data.Time.Clock (UTCTime(..))
2021
import Functions
21-
import Test.QuickCheck (Arbitrary(..), elements, oneof)
22+
import Test.QuickCheck (Arbitrary(..), elements, oneof)
2223
import Types
2324
import qualified Data.DList as DList
2425
import qualified Data.HashMap.Strict as HM
26+
import qualified Network.URI as URI
2527

2628
import Data.Orphans ()
2729
import Test.QuickCheck.Instances ()
@@ -167,3 +169,9 @@ instance (ApproxEq a) => ApproxEq [a] where
167169

168170
instance Arbitrary a => Arbitrary (DList.DList a) where
169171
arbitrary = DList.fromList <$> arbitrary
172+
173+
instance Arbitrary URI.URI where
174+
arbitrary = elements $ mapMaybe URI.parseURI
175+
[ "https://haskell.org"
176+
, "foo://[email protected]:42/ghc?query#frag"
177+
]

tests/PropertyRoundTrip.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Data.Time.Calendar.Compat (CalendarDiffDays, DayOfWeek)
2323
import Data.Time.LocalTime.Compat (CalendarDiffTime)
2424
import Data.Time.Clock.System.Compat (SystemTime)
2525
import Data.Tuple.Solo (Solo)
26+
import Network.URI (URI)
2627
import Numeric.Natural (Natural)
2728
import Test.Tasty (TestTree, testGroup)
2829
import Test.Tasty.QuickCheck (testProperty)
@@ -92,6 +93,7 @@ roundTripTests =
9293
, testProperty "Strict Maybe" $ roundTripEq @(S.Maybe Int)
9394
, testProperty "Solo Int" $ roundTripEq @(Solo Int)
9495
, testProperty "ShortText" $ roundTripEq @ST.ShortText
96+
, testProperty "URI" $ roundTripEq @URI
9597
, roundTripFunctorsTests
9698
, testGroup "ghcGenerics" [
9799
testProperty "OneConstructor" $ roundTripEq OneConstructor

0 commit comments

Comments
 (0)