Skip to content

Commit

Permalink
Add UUID instances
Browse files Browse the repository at this point in the history
  • Loading branch information
phadej committed Dec 11, 2016
1 parent c149038 commit 5173ab0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Data/Aeson/Types/FromJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as LT
import qualified Data.Tree as Tree
import qualified Data.UUID.Types as UUID
import qualified Data.Vector as V
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Primitive as VP
Expand Down Expand Up @@ -1486,6 +1487,18 @@ instance (FromJSON v) => FromJSON (Tree.Tree v) where
parseJSON = parseJSON1
{-# INLINE parseJSON #-}

-------------------------------------------------------------------------------
-- uuid
-------------------------------------------------------------------------------

instance FromJSON UUID.UUID where
parseJSON = withText "UUID" $
maybe (fail "Invalid UUID") pure . UUID.fromText

instance FromJSONKey UUID.UUID where
fromJSONKey = FromJSONKeyTextParser $
maybe (fail "Invalid UUID") pure . UUID.fromText

-------------------------------------------------------------------------------
-- vector
-------------------------------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions Data/Aeson/Types/ToJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,17 @@ import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as LT
import qualified Data.Tree as Tree
import qualified Data.UUID.Types as UUID
import qualified Data.Vector as V
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Unboxed as VU

import qualified Data.Aeson.Encoding.Builder as EB
import qualified Data.ByteString.Builder as B

#if !(MIN_VERSION_bytestring(0,10,0))
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Marshal.Utils (copyBytes)
Expand Down Expand Up @@ -1857,6 +1861,17 @@ instance (ToJSON v) => ToJSON (Tree.Tree v) where
toEncoding = toEncoding1
{-# INLINE toEncoding #-}

-------------------------------------------------------------------------------
-- uuid
-------------------------------------------------------------------------------

instance ToJSON UUID.UUID where
toJSON = toJSON . UUID.toText
toEncoding = E.unsafeToEncoding . EB.quote . B.byteString . UUID.toASCIIBytes

instance ToJSONKey UUID.UUID where
toJSONKey = ToJSONKeyText UUID.toText $
E.unsafeToEncoding . EB.quote . B.byteString . UUID.toASCIIBytes

-------------------------------------------------------------------------------
-- vector
Expand Down
2 changes: 2 additions & 0 deletions aeson.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ library
time >= 1.1.1.4,
time-locale-compat >= 0.1.1 && < 0.2,
unordered-containers >= 0.2.5.0,
uuid-types >= 1.0.3 && <1.1,
vector >= 0.8

if flag(bytestring-builder)
Expand Down Expand Up @@ -198,6 +199,7 @@ test-suite tests
time,
time-locale-compat,
unordered-containers,
uuid-types,
vector,
quickcheck-instances >=0.3.12

Expand Down
1 change: 1 addition & 0 deletions benchmarks/aeson-benchmarks.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ library
time,
transformers,
unordered-containers >= 0.2.3.0,
uuid-types >= 1.0.3 && <1.1,
vector >= 0.7.1

if flag(bytestring-builder)
Expand Down
8 changes: 8 additions & 0 deletions tests/Instances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Test.QuickCheck (Arbitrary(..), elements, oneof)
import Types
import qualified Data.DList as DList
import qualified Data.HashMap.Strict as HM
import qualified Data.UUID.Types as UUID

#if !MIN_VERSION_QuickCheck(2,9,0)
import Control.Applicative (Const(..))
Expand Down Expand Up @@ -218,3 +219,10 @@ makeVersion b = Version b []
instance Arbitrary a => Arbitrary (Identity a) where
arbitrary = Identity <$> arbitrary
#endif

instance Arbitrary UUID.UUID where
arbitrary = UUID.fromWords
<$> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
3 changes: 3 additions & 0 deletions tests/Properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import qualified Data.HashMap.Strict as H
import qualified Data.Map as Map
import qualified Data.Text as T
import qualified Data.Text.Lazy as LT
import qualified Data.UUID.Types as UUID
import qualified Data.Vector as V

encodeDouble :: Double -> Double -> Property
Expand Down Expand Up @@ -217,6 +218,7 @@ tests = testGroup "properties" [
, testProperty "Seq" $ roundTripEq (undefined :: Seq Int)
, testProperty "Rational" $ roundTripEq (undefined :: Rational)
, testProperty "Ratio Int" $ roundTripEq (undefined :: Ratio Int)
, testProperty "UUID" $ roundTripEq UUID.nil
, testGroup "functors"
[ testProperty "Identity Char" $ roundTripEq (undefined :: I Int)

Expand Down Expand Up @@ -278,6 +280,7 @@ tests = testGroup "properties" [
#endif
, testProperty "Version" $ roundTripKey (undefined :: Version)
, testProperty "Lazy Text" $ roundTripKey (undefined :: LT.Text)
, testProperty "UUID" $ roundTripKey UUID.nil
]
, testGroup "toFromJSON" [
testProperty "Integer" (toFromJSON :: Integer -> Property)
Expand Down
4 changes: 4 additions & 0 deletions tests/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import qualified Data.Text.Lazy.Builder as TLB
import qualified Data.Text.Lazy.Encoding as LT
import qualified Data.Text.Lazy.Encoding as TLE
import qualified Data.Tree as Tree
import qualified Data.UUID.Types as UUID
import qualified Data.Vector as Vector

tests :: Test
Expand Down Expand Up @@ -369,6 +370,9 @@ jsonExamples =
, Example "Pico" "3.14" (3.14 :: Pico)
, Example "Scientific" "3.14" (3.14 :: Scientific)

, Example "UUID" "\"c2cc10e1-57d6-4b6f-9899-38d972112d8c\"" $ UUID.fromWords
0xc2cc10e1 0x57d64b6f 0x989938d9 0x72112d8c

, Example "Set Int" "[1,2,3]" (Set.fromList [3, 2, 1] :: Set.Set Int)
, Example "IntSet" "[1,2,3]" (IntSet.fromList [3, 2, 1])
, Example "IntMap" "[[1,2],[3,4]]" (IntMap.fromList [(3,4), (1,2)] :: IntMap.IntMap Int)
Expand Down

0 comments on commit 5173ab0

Please sign in to comment.