Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aeson instances behind a cabal flag #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Data/ULID/Aeson.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Data.ULID.Aeson () where

import Data.ULID
import Data.ULID.Random
import Data.Aeson
import Data.Aeson.Encoding (string)
import Data.String (IsString(fromString))
import Data.Aeson.Types (Parser)

instance FromJSON ULID where
parseJSON value =
read <$> parseJSON value

instance ToJSON ULID where
toJSON = fromString . show
toEncoding = string . show

instance FromJSON ULIDRandom where
parseJSON value =
read <$> parseJSON value

instance ToJSON ULIDRandom where
toJSON = fromString . show
toEncoding = string . show
3 changes: 3 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
resolver: lts-18.24
packages: ['.']
flags:
ulid:
aeson: true
42 changes: 42 additions & 0 deletions test/Data/ULID/AesonSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{-# LANGUAGE TypeApplications #-}
module Data.ULID.AesonSpec where

import Data.ULID
import Data.ULID.Random
import Data.ByteString.Lazy
import qualified Data.Aeson as Aeson
import Data.Text.Lazy.Encoding
import Data.Text.Lazy as T
import Data.ULID.Aeson ()

import Test.Hspec
import Data.String (IsString(fromString))


spec :: Spec
spec = do
describe "ULID fromJson/toJson" $ do
it "Same after encode -> decode" $ do
ulid <- getULID
Aeson.eitherDecode (Aeson.encode ulid) `shouldBe` Right ulid
read (show ulid) `shouldBe` ulid

it "decodes similar to read" $ do
ulid_str <- show <$> getULID
let
ulid_bs :: ByteString
ulid_bs = encodeUtf8 $ T.pack ("\"" <> ulid_str <> "\"")
Aeson.eitherDecode ulid_bs `shouldBe` Right (read @ULID ulid_str)

describe "ULIDRandom fromJson/toJson" $ do
it "Same after encode -> decode" $ do
ulid <- getULIDRandom
Aeson.eitherDecode (Aeson.encode ulid) `shouldBe` Right ulid
read (show ulid) `shouldBe` ulid

it "decodes similar to read" $ do
ulid_str <- show <$> getULIDRandom
let
ulid_bs :: ByteString
ulid_bs = encodeUtf8 $ T.pack ("\"" <> ulid_str <> "\"")
Aeson.eitherDecode ulid_bs `shouldBe` Right (read @ULIDRandom ulid_str)
12 changes: 11 additions & 1 deletion ulid.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ build-type: Simple
extra-source-files: README.md
cabal-version: >=1.10

flag aeson
description: build json de-/encoding via aeson
default: False


library
hs-source-dirs: src
Expand All @@ -39,6 +43,9 @@ library
, time
default-language: Haskell2010
default-extensions: OverloadedStrings
if flag(aeson)
build-depends: aeson
exposed-modules: Data.ULID.Aeson


executable ulid-exe
Expand All @@ -55,7 +62,8 @@ test-suite ulid-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
other-modules: Data.ULID.Base32Spec
other-modules: Data.ULID.AesonSpec
, Data.ULID.Base32Spec
, Data.ULID.RandomSpec
, Data.ULID.TimeStampSpec
, Data.ULIDSpec
Expand All @@ -66,6 +74,8 @@ test-suite ulid-test
, binary
, random
, hashable
, aeson
, text
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
default-extensions: OverloadedStrings
Expand Down