Skip to content

Add Data.Complex instances #1091

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

Open
wants to merge 1 commit into
base: master
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
9 changes: 9 additions & 0 deletions src/Data/Aeson/Types/FromJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import Data.Aeson.Types.Internal
import Data.Aeson.Decoding.ByteString.Lazy
import Data.Aeson.Decoding.Conversion (unResult, toResultValue, lbsSpace)
import Data.Bits (unsafeShiftR)
import Data.Complex (Complex(..))
import Data.Fixed (Fixed, HasResolution (resolution), Nano)
import Data.Functor.Compose (Compose(..))
import Data.Functor.Identity (Identity(..))
Expand Down Expand Up @@ -1720,6 +1721,14 @@ instance (FromJSON a, Integral a) => FromJSON (Ratio a) where
then fail "Ratio denominator was 0"
else pure $ numerator % denominator

instance FromJSON a => FromJSON (Complex a) where
parseJSON = withArray "Complex" $ \c ->
let n = V.length c
in if n == 2
then (:+) <$> parseJSONElemAtIndex parseJSON 0 c
<*> parseJSONElemAtIndex parseJSON 1 c
else fail $ "cannot unpack array of length " ++ show n ++ "into a Complex"

-- | This instance includes a bounds check to prevent maliciously
-- large inputs to fill up the memory of the target system. You can
-- newtype 'Scientific' and provide your own instance using
Expand Down
11 changes: 11 additions & 0 deletions src/Data/Aeson/Types/ToJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import Data.Aeson.Types.Internal
import qualified Data.Aeson.Key as Key
import qualified Data.Aeson.KeyMap as KM
import Data.Bits (unsafeShiftR)
import Data.Complex (Complex(..))
import Data.DList (DList)
import Data.Fixed (Fixed, HasResolution, Nano)
import Data.Foldable (toList)
Expand Down Expand Up @@ -1421,6 +1422,16 @@ instance (ToJSON a, Integral a) => ToJSON (Ratio a) where
"numerator" .= numerator r <>
"denominator" .= denominator r

instance ToJSON a => ToJSON (Complex a) where
toJSON (i :+ q) = Array $ V.create $ do
mv <- VM.unsafeNew 2
VM.unsafeWrite mv 0 (toJSON i)
VM.unsafeWrite mv 1 (toJSON q)
return mv
toEncoding (i :+ q) = E.list id
[ toEncoding i
, toEncoding q
]

instance HasResolution a => ToJSON (Fixed a) where
toJSON = Number . realToFrac
Expand Down
3 changes: 3 additions & 0 deletions tests/PropertyRoundTrip.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Prelude.Compat

import Control.Applicative (Const)
import Data.Aeson.Types
import Data.Complex (Complex(..))
import Data.DList (DList)
import Data.List.NonEmpty (NonEmpty)
import Data.Map (Map)
Expand Down Expand Up @@ -80,6 +81,8 @@ roundTripTests =
, testProperty "Seq" $ roundTripEq @(Seq Int)
, testProperty "Rational" $ roundTripEq @Rational
, testProperty "Ratio Int" $ roundTripEq @(Ratio Int)
, testProperty "Complex Float" $ roundTripEq @(Complex Float)
, testProperty "Complex Double" $ roundTripEq @(Complex Double)
, testProperty "UUID" $ roundTripEq @UUID.UUID
, testProperty "These" $ roundTripEq @(These Char Bool)
, testProperty "Fix" $ roundTripEq @(F.Fix (These Char))
Expand Down