aeson-compat-0.3.6/0000755000000000000000000000000012765475716012320 5ustar0000000000000000aeson-compat-0.3.6/aeson-compat.cabal0000644000000000000000000000601712765475716015676 0ustar0000000000000000-- This file has been generated from package.yaml by hpack version 0.14.1. -- -- see: https://github.com/sol/hpack name: aeson-compat version: 0.3.6 synopsis: Compatibility layer for aeson description: Compatibility layer for @aeson@ category: Web homepage: https://github.com/phadej/aeson-compat#readme bug-reports: https://github.com/phadej/aeson-compat/issues author: Oleg Grenrus maintainer: Oleg Grenrus license: BSD3 license-file: LICENSE tested-with: GHC==7.6.3, GHC==7.8.4, GHC==7.10.3, GHC==8.0.1 build-type: Simple cabal-version: >= 1.10 extra-source-files: CHANGELOG.md README.md source-repository head type: git location: https://github.com/phadej/aeson-compat library hs-source-dirs: src ghc-options: -Wall build-depends: base >=4.6 && <4.10 , base-compat >=0.6.0 && <0.10 , aeson >=0.7.0.6 && <1.1 , attoparsec >=0.12 && <0.14 , bytestring >=0.10 && <0.11 , containers >=0.5 && <0.6 , exceptions >=0.8 && <0.9 , hashable >=1.2 && <1.3 , nats >=1 && <1.2 , scientific >=0.3 && <0.4 , text >=1.2 && <1.3 , time >=1.4.2 && <1.7 , time-locale-compat >=0.1.0.1 && <0.2 , unordered-containers >=0.2 && <0.3 , vector >=0.10 && <0.12 , semigroups >=0.16.2.2 && <0.19 , tagged >=0.7.3 && <0.9 exposed-modules: Data.Aeson.Compat other-modules: Data.Aeson.Compat.Time default-language: Haskell2010 test-suite aeson-compat-test type: exitcode-stdio-1.0 main-is: Tests.hs hs-source-dirs: test ghc-options: -Wall build-depends: base >=4.6 && <4.10 , base-compat >=0.6.0 && <0.10 , aeson >=0.7.0.6 && <1.1 , attoparsec >=0.12 && <0.14 , bytestring >=0.10 && <0.11 , containers >=0.5 && <0.6 , exceptions >=0.8 && <0.9 , hashable >=1.2 && <1.3 , nats >=1 && <1.2 , scientific >=0.3 && <0.4 , text >=1.2 && <1.3 , time >=1.4.2 && <1.7 , time-locale-compat >=0.1.0.1 && <0.2 , unordered-containers >=0.2 && <0.3 , vector >=0.10 && <0.12 , semigroups >=0.16.2.2 && <0.19 , tagged >=0.7.3 && <0.9 , aeson-compat , base-orphans >=0.4.5 && <0.6 , tasty >=0.10 && <0.12 , tasty-hunit >=0.9 && <0.10 , tasty-quickcheck >=0.8 && <0.9 , QuickCheck >=2.7.6 && <2.9.2 , quickcheck-instances >=0.3 && <0.3.13 other-modules: Orphans default-language: Haskell2010 aeson-compat-0.3.6/CHANGELOG.md0000644000000000000000000000154412765475716014135 0ustar0000000000000000# 0.3.6 - Fix accidental removal of `AesonException` export # 0.3.5.2 - Support QuickCheck-2.9.1 # 0.3.5.1 - Fix `(.=)` export regression introduced by 0.3.5.0 # 0.3.5.0 - Use explicit export list. Now we are sure we don't break interface. - `value`, `value'` and `Parser` are exported from `Data.Aeson.Compat` # 0.3.4.0 - Add `NominalDiffTime` instances # 0.3.3.0 - Enable `PolyKinds` to generalize `Proxy`, `Tagged`, and `Const` instances. # 0.3.2.0 - Introduce instances from `aeson-0.11.1.0`: `Const`, `Tagged`, `Proxy` and `NonEmpty` - Fix bug with `Natural` instance, `aeson-0.11.1.0` and `base <=4.7` # 0.3.1.0 - `aeson-0.11` support - GHC 8.0.1 support - Add `ToJSON` `Day` and `LocalTime` instances - *NOTE* this instances are broken in `aeson-0.10.0.0` - Add `Natural`, `Ordering` and `Version` instances # 0.3.0.0 Split out `aeson-extra` aeson-compat-0.3.6/LICENSE0000644000000000000000000000276212765475716013334 0ustar0000000000000000Copyright (c) 2015, Oleg Grenrus All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Oleg Grenrus nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. aeson-compat-0.3.6/README.md0000644000000000000000000000112212765475716013573 0ustar0000000000000000# aeson-compat [![Build Status](https://travis-ci.org/phadej/aeson-compat.svg?branch=master)](https://travis-ci.org/phadej/aeson-compat) [![Hackage](https://img.shields.io/hackage/v/aeson-compat.svg)](http://hackage.haskell.org/package/aeson-compat) [![Stackage LTS 3](http://stackage.org/package/monad-http/badge/lts-3)](http://stackage.org/lts-3/package/aeson-compat) [![Stackage Nightly](http://stackage.org/package/monad-http/badge/nightly)](http://stackage.org/nightly/package/aeson-compat) The package provides compatibility layer for [`aeson`](http://hackage.haskell.org/package/aeson)aeson-compat-0.3.6/Setup.hs0000644000000000000000000000005612765475716013755 0ustar0000000000000000import Distribution.Simple main = defaultMain aeson-compat-0.3.6/src/0000755000000000000000000000000012765475716013107 5ustar0000000000000000aeson-compat-0.3.6/src/Data/0000755000000000000000000000000012765475716013760 5ustar0000000000000000aeson-compat-0.3.6/src/Data/Aeson/0000755000000000000000000000000012765475716015025 5ustar0000000000000000aeson-compat-0.3.6/src/Data/Aeson/Compat.hs0000644000000000000000000003064612765475716016615 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE PolyKinds #-} {-# OPTIONS_GHC -fno-warn-orphans #-} ----------------------------------------------------------------------------- -- | -- Module : Data.Aeson.Compat -- Copyright : (C) 2015 Oleg Grenrus -- License : BSD3 -- Maintainer : Oleg Grenrus -- -- Compatibility notices -- -- * 'decode' etc. work as in @aeson >=0.9@ -- * but it is generalised to work in any 'MonadThrow' (that is extra) -- * '.:?' works as in @aeson <0.10@ -- * '.:!' works as '.:?' in @aeson ==0.10@ -- * Orphan instances 'FromJSON' 'Day' and 'FromJSON' 'LocalTime' for @aeson <0.10@ -- * 'Encoding' related functionality is not added. It's present only with @aeson >=0.10@ -- module Data.Aeson.Compat ( -- * Encoding and decoding -- ** Direct encoding decode, decode', AesonException(..), eitherDecode, eitherDecode', encode, -- ** Variants for strict bytestrings decodeStrict, decodeStrict', eitherDecodeStrict, eitherDecodeStrict', -- * Core JSON types Value(..), #if MIN_VERSION_aeson(0,10,0) Encoding, fromEncoding, #endif Array, Object, -- * Convenience types DotNetTime(..), -- * Type conversion FromJSON(..), Result(..), fromJSON, ToJSON(..), #if MIN_VERSION_aeson(0,10,0) KeyValue(..), #else (.=), #endif -- ** Generic JSON classes and options GFromJSON(..), GToJSON(..), #if MIN_VERSION_aeson(0,11,0) -- GToEncoding is introduced in 0.11.0.0 GToEncoding(..), #endif genericToJSON, #if MIN_VERSION_aeson(0,10,0) genericToEncoding, #endif genericParseJSON, defaultOptions, -- * Inspecting @'Value's@ withObject, withText, withArray, withNumber, withScientific, withBool, -- * Constructors and accessors #if MIN_VERSION_aeson(0,10,0) Series, pairs, foldable, #endif (.:), (.:?), (.:!), (.!=), object, -- * Parsing json, json', value, value', Parser, ) where import Prelude () import Prelude.Compat import Data.Aeson hiding ((.:?), decode, decode', decodeStrict, decodeStrict' #if !MIN_VERSION_aeson (0,9,0) , eitherDecode, eitherDecode', eitherDecodeStrict, eitherDecodeStrict' #endif ) import Data.Aeson.Parser (value, value') #if !MIN_VERSION_aeson (0,9,0) import qualified Data.Attoparsec.ByteString as A import qualified Data.Attoparsec.ByteString.Char8 as A (skipSpace) import qualified Data.Attoparsec.Lazy as L #endif import Control.Monad.Catch (MonadThrow (..), Exception) import Data.Aeson.Types hiding ((.:?)) import Data.ByteString as B import qualified Data.Scientific as Scientific import Data.ByteString.Lazy as L import qualified Data.HashMap.Strict as H import Data.Text as T import Data.Typeable (Typeable) #if !MIN_VERSION_aeson(0,10,0) import Data.Time (Day, LocalTime, formatTime, NominalDiffTime) import Data.Time.Locale.Compat (defaultTimeLocale) import qualified Data.Aeson.Compat.Time as CompatTime #endif #if !(MIN_VERSION_aeson(0,11,0) && MIN_VERSION_base(4,8,0)) import Numeric.Natural (Natural) #endif #if !MIN_VERSION_aeson(0,11,0) import Data.Version (Version, showVersion, parseVersion) import Text.ParserCombinators.ReadP (readP_to_S) #endif #if !MIN_VERSION_aeson(0,11,1) import Control.Applicative (Const (..)) import Data.List.NonEmpty (NonEmpty (..)) import Data.Proxy (Proxy (..)) import Data.Tagged (Tagged (..)) import qualified Data.List.NonEmpty as NonEmpty import qualified Data.Vector as V #endif -- | Exception thrown by 'decode' - family of functions in this module. newtype AesonException = AesonException String deriving (Show, Typeable) instance Exception AesonException eitherAesonExc :: (MonadThrow m) => Either String a -> m a eitherAesonExc (Left err) = throwM (AesonException err) eitherAesonExc (Right x) = return x -- | Like original 'Data.Aeson.decode' but in arbitrary 'MonadThrow'. -- -- Parse a top-level JSON value, i.e. also strings, numbers etc. decode :: (FromJSON a, MonadThrow m) => L.ByteString -> m a decode = eitherAesonExc . eitherDecode -- | Like original 'Data.Aeson.decode'' but in arbitrary 'MonadThrow'. decode' :: (FromJSON a, MonadThrow m) => L.ByteString -> m a decode' = eitherAesonExc . eitherDecode' -- | Like original 'Data.Aeson.decodeStrict' but in arbitrary 'MonadThrow'. decodeStrict :: (FromJSON a, MonadThrow m) => B.ByteString -> m a decodeStrict = eitherAesonExc . eitherDecodeStrict -- | Like original 'Data.Aeson.decodeStrict'' but in arbitrary 'MonadThrow'. decodeStrict' :: (FromJSON a, MonadThrow m) => B.ByteString -> m a decodeStrict' = eitherAesonExc . eitherDecodeStrict' -- | Retrieve the value associated with the given key of an 'Object'. -- The result is 'Nothing' if the key is not present, or 'empty' if -- the value cannot be converted to the desired type. -- -- This accessor is most useful if the key and value can be absent -- from an object without affecting its validity. If the key and -- value are mandatory, use '.:' instead. -- -- This operator is consistent in @aeson >=0.7 && <0.11@ (.:?) :: (FromJSON a) => Object -> Text -> Parser (Maybe a) obj .:? key = case H.lookup key obj of Nothing -> pure Nothing Just v -> #if MIN_VERSION_aeson(0,10,0) modifyFailure addKeyName $ parseJSON v -- Key key where addKeyName = mappend $ mconcat ["failed to parse field ", T.unpack key, ": "] #else parseJSON v #endif {-# INLINE (.:?) #-} #if !MIN_VERSION_aeson(0,11,0) -- | Like '.:?', but the resulting parser will fail, -- if the key is present but is 'Null'. (.:!) :: (FromJSON a) => Object -> Text -> Parser (Maybe a) obj .:! key = case H.lookup key obj of Nothing -> pure Nothing Just v -> #if MIN_VERSION_aeson(0,10,0) modifyFailure addKeyName $ Just <$> parseJSON v -- Key key where addKeyName = mappend $ mconcat ["failed to parse field ", T.unpack key, ": "] #else Just <$> parseJSON v #endif {-# INLINE (.:!) #-} #endif #if !MIN_VERSION_aeson(0,9,0) -- From Parser.Internal -- | Parse a top-level JSON value followed by optional whitespace and -- end-of-input. See also: 'json'. jsonEOF :: A.Parser Value jsonEOF = value <* A.skipSpace <* A.endOfInput -- | Parse a top-level JSON value followed by optional whitespace and -- end-of-input. See also: 'json''. jsonEOF' :: A.Parser Value jsonEOF' = value' <* A.skipSpace <* A.endOfInput -- | Like 'decode' but returns an error message when decoding fails. eitherDecode :: (FromJSON a) => L.ByteString -> Either String a eitherDecode = eitherDecodeWith jsonEOF fromJSON {-# INLINE eitherDecode #-} -- | Like 'decodeStrict' but returns an error message when decoding fails. eitherDecodeStrict :: (FromJSON a) => B.ByteString -> Either String a eitherDecodeStrict = eitherDecodeStrictWith jsonEOF fromJSON {-# INLINE eitherDecodeStrict #-} -- | Like 'decode'' but returns an error message when decoding fails. eitherDecode' :: (FromJSON a) => L.ByteString -> Either String a eitherDecode' = eitherDecodeWith jsonEOF' fromJSON {-# INLINE eitherDecode' #-} -- | Like 'decodeStrict'' but returns an error message when decoding fails. eitherDecodeStrict' :: (FromJSON a) => B.ByteString -> Either String a eitherDecodeStrict' = eitherDecodeStrictWith jsonEOF' fromJSON {-# INLINE eitherDecodeStrict' #-} eitherDecodeWith :: L.Parser Value -> (Value -> Result a) -> L.ByteString -> Either String a eitherDecodeWith p to s = case L.parse p s of L.Done _ v -> case to v of Success a -> Right a Error msg -> Left msg L.Fail _ _ msg -> Left msg {-# INLINE eitherDecodeWith #-} eitherDecodeStrictWith :: A.Parser Value -> (Value -> Result a) -> B.ByteString -> Either String a eitherDecodeStrictWith p to s = case either Error to (A.parseOnly p s) of Success a -> Right a Error msg -> Left msg {-# INLINE eitherDecodeStrictWith #-} #endif ----------------------------------------------------------------------- -- Instances in aeson-0.10 ----------------------------------------------------------------------- #if !MIN_VERSION_aeson(0,10,0) instance FromJSON Day where parseJSON = withText "Day" (CompatTime.run CompatTime.day) instance FromJSON LocalTime where parseJSON = withText "LocalTime" (CompatTime.run CompatTime.localTime) instance ToJSON Day where toJSON = toJSON . T.pack . formatTime defaultTimeLocale "%F" instance ToJSON LocalTime where toJSON = toJSON . T.pack . formatTime defaultTimeLocale "%FT%T%Q" instance ToJSON NominalDiffTime where toJSON = Number . realToFrac {-# INLINE toJSON #-} #if MIN_VERSION_aeson(0,10,0) toEncoding = Encoding . E.number . realToFrac {-# INLINE toEncoding #-} #endif -- | /WARNING:/ Only parse lengths of time from trusted input -- since an attacker could easily fill up the memory of the target -- system by specifying a scientific number with a big exponent like -- @1e1000000000@. instance FromJSON NominalDiffTime where parseJSON = withScientific "NominalDiffTime" $ pure . realToFrac {-# INLINE parseJSON #-} #endif ----------------------------------------------------------------------- -- Instances in aeson-0.11 ----------------------------------------------------------------------- #if !(MIN_VERSION_aeson(0,11,1)) #if !(MIN_VERSION_aeson(0,11,0) && MIN_VERSION_base(4,8,0)) instance ToJSON Natural where toJSON = toJSON . toInteger {-# INLINE toJSON #-} #if MIN_VERSION_aeson(0,10,0) toEncoding = toEncoding . toInteger {-# INLINE toEncoding #-} #endif instance FromJSON Natural where parseJSON = withScientific "Natural" $ \s -> if Scientific.coefficient s < 0 then fail $ "Expected a Natural number but got the negative number: " ++ show s else pure $ truncate s #endif #endif #if !MIN_VERSION_aeson(0,11,0) instance ToJSON Version where toJSON = toJSON . showVersion {-# INLINE toJSON #-} #if MIN_VERSION_aeson(0,10,0) toEncoding = toEncoding . showVersion {-# INLINE toEncoding #-} #endif instance FromJSON Version where {-# INLINE parseJSON #-} parseJSON = withText "Version" $ go . readP_to_S parseVersion . T.unpack where go [(v,[])] = return v go (_ : xs) = go xs go _ = fail $ "could not parse Version" instance ToJSON Ordering where toJSON = toJSON . orderingToText #if MIN_VERSION_aeson(0,10,0) toEncoding = toEncoding . orderingToText #endif orderingToText :: Ordering -> T.Text orderingToText o = case o of LT -> "LT" EQ -> "EQ" GT -> "GT" instance FromJSON Ordering where parseJSON = withText "Ordering" $ \s -> case s of "LT" -> return LT "EQ" -> return EQ "GT" -> return GT _ -> fail "Parsing Ordering value failed: expected \"LT\", \"EQ\", or \"GT\"" #endif #if !MIN_VERSION_aeson(0,11,1) instance ToJSON (Proxy a) where toJSON _ = Null {-# INLINE toJSON #-} -- No 'toEncoding', default is good enough instance FromJSON (Proxy a) where {-# INLINE parseJSON #-} parseJSON Null = pure Proxy parseJSON v = typeMismatch "Proxy" v instance ToJSON b => ToJSON (Tagged a b) where toJSON (Tagged x) = toJSON x {-# INLINE toJSON #-} #if MIN_VERSION_aeson(0,10,0) toEncoding (Tagged x) = toEncoding x {-# INLINE toEncoding #-} #endif instance FromJSON b => FromJSON (Tagged a b) where {-# INLINE parseJSON #-} parseJSON = fmap Tagged . parseJSON instance ToJSON a => ToJSON (Const a b) where toJSON (Const x) = toJSON x {-# INLINE toJSON #-} #if MIN_VERSION_aeson(0,10,0) toEncoding (Const x) = toEncoding x {-# INLINE toEncoding #-} #endif instance FromJSON a => FromJSON (Const a b) where {-# INLINE parseJSON #-} parseJSON = fmap Const . parseJSON instance (ToJSON a) => ToJSON (NonEmpty a) where toJSON = toJSON . NonEmpty.toList {-# INLINE toJSON #-} #if MIN_VERSION_aeson(0,10,0) toEncoding = toEncoding . NonEmpty.toList {-# INLINE toEncoding #-} #endif instance (FromJSON a) => FromJSON (NonEmpty a) where parseJSON = withArray "NonEmpty a" $ (>>= ne) . traverse parseJSON . V.toList where ne [] = fail "Expected a NonEmpty but got an empty list" ne (x:xs) = pure (x :| xs) #endif aeson-compat-0.3.6/src/Data/Aeson/Compat/0000755000000000000000000000000012765475716016250 5ustar0000000000000000aeson-compat-0.3.6/src/Data/Aeson/Compat/Time.hs0000644000000000000000000001150312765475716017502 0ustar0000000000000000{-# LANGUAGE BangPatterns, CPP, ScopedTypeVariables #-} -- | -- Module: Data.Aeson.Compat.Time (Data.Aeson.Parser.Time) -- Copyright: (c) 2015 Bryan O'Sullivan -- License: BSD3 -- Maintainer: Bryan O'Sullivan -- Stability: experimental -- Portability: portable -- -- Parsers for parsing dates and times. module Data.Aeson.Compat.Time ( run , day , localTime , timeOfDay , timeZone , utcTime , zonedTime ) where import Control.Monad (when, void) import Data.Attoparsec.Text as A import Data.Bits ((.&.)) import Data.Char (isDigit, ord) import Data.Fixed (Pico) import Data.Int (Int64) import Data.Maybe (fromMaybe) import Data.Text (Text) import Data.Time.Calendar (Day, fromGregorianValid) import Data.Time.Clock (UTCTime(..)) import qualified Data.Aeson.Types as Aeson import qualified Data.Text as T import qualified Data.Time.LocalTime as Local #if !MIN_VERSION_base(4,8,0) import Control.Applicative ((<$>), (<*>), (<*), (*>)) #endif --- from Data.Aeson.Internal.Time import Unsafe.Coerce (unsafeCoerce) toPico :: Integer -> Pico toPico = unsafeCoerce -- | Run an attoparsec parser as an aeson parser. run :: Parser a -> Text -> Aeson.Parser a run p t = case A.parseOnly (p <* endOfInput) t of Left err -> fail $ "could not parse date: " ++ err Right r -> return r -- | Parse a date of the form @YYYY-MM-DD@. day :: Parser Day day = do y <- decimal <* char '-' m <- twoDigits <* char '-' d <- twoDigits maybe (fail "invalid date") return (fromGregorianValid y m d) -- | Parse a two-digit integer (e.g. day of month, hour). twoDigits :: Parser Int twoDigits = do a <- digit b <- digit let c2d c = ord c .&. 15 return $! c2d a * 10 + c2d b -- | Parse a time of the form @HH:MM[:SS[.SSS]]@. timeOfDay :: Parser Local.TimeOfDay timeOfDay = do h <- twoDigits m <- char ':' *> twoDigits s <- option 0 (char ':' *> seconds) if h < 24 && m < 60 && s < 61 then return (Local.TimeOfDay h m s) else fail "invalid time" data T = T {-# UNPACK #-} !Int {-# UNPACK #-} !Int64 -- | Parse a count of seconds, with the integer part being two digits -- long. seconds :: Parser Pico seconds = do real <- twoDigits mc <- peekChar case mc of Just '.' -> do t <- anyChar *> takeWhile1 isDigit return $! parsePicos real t _ -> return $! fromIntegral real where parsePicos a0 t = toPico (fromIntegral (t' * 10^n)) where T n t' = T.foldl' step (T 12 (fromIntegral a0)) t step ma@(T m a) c | m <= 0 = ma | otherwise = T (m-1) (10 * a + fromIntegral (ord c) .&. 15) -- | Parse a time zone, and return 'Nothing' if the offset from UTC is -- zero. (This makes some speedups possible.) timeZone :: Parser (Maybe Local.TimeZone) timeZone = do let maybeSkip c = do ch <- peekChar'; when (ch == c) (void anyChar) maybeSkip ' ' ch <- satisfy $ \c -> c == 'Z' || c == '+' || c == '-' if ch == 'Z' then return Nothing else do h <- twoDigits mm <- peekChar m <- case mm of Just ':' -> anyChar *> twoDigits Just d | isDigit d -> twoDigits _ -> return 0 let off | ch == '-' = negate off0 | otherwise = off0 off0 = h * 60 + m case undefined of _ | off == 0 -> return Nothing | off < -720 || off > 840 || m > 59 -> fail "invalid time zone offset" | otherwise -> let !tz = Local.minutesToTimeZone off in return (Just tz) -- | Parse a date and time, of the form @YYYY-MM-DD HH:MM[:SS[.SSS]]@. -- The space may be replaced with a @T@. The number of seconds is optional -- and may be followed by a fractional component. localTime :: Parser Local.LocalTime localTime = Local.LocalTime <$> day <* daySep <*> timeOfDay where daySep = satisfy (\c -> c == 'T' || c == ' ') -- | Behaves as 'zonedTime', but converts any time zone offset into a -- UTC time. utcTime :: Parser UTCTime utcTime = do lt@(Local.LocalTime d t) <- localTime mtz <- timeZone case mtz of Nothing -> let !tt = Local.timeOfDayToTime t in return (UTCTime d tt) Just tz -> return $! Local.localTimeToUTC tz lt -- | Parse a date with time zone info. Acceptable formats: -- -- @YYYY-MM-DD HH:MM Z@ -- @YYYY-MM-DD HH:MM:SS Z@ -- @YYYY-MM-DD HH:MM:SS.SSS Z@ -- -- The first space may instead be a @T@, and the second space is -- optional. The @Z@ represents UTC. The @Z@ may be replaced with a -- time zone offset of the form @+0000@ or @-08:00@, where the first -- two digits are hours, the @:@ is optional and the second two digits -- (also optional) are minutes. zonedTime :: Parser Local.ZonedTime zonedTime = Local.ZonedTime <$> localTime <*> (fromMaybe utc <$> timeZone) utc :: Local.TimeZone utc = Local.TimeZone 0 False "" aeson-compat-0.3.6/test/0000755000000000000000000000000012765475716013277 5ustar0000000000000000aeson-compat-0.3.6/test/Orphans.hs0000644000000000000000000000326212765475716015250 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Orphans where import Prelude () import Prelude.Compat import Test.Tasty.QuickCheck import Data.Orphans () import Test.QuickCheck.Instances () #if !MIN_VERSION_quickcheck_instances(0,3,12) import Data.Vector as V #endif #if !(MIN_VERSION_QuickCheck(2,8,0) && MIN_VERSION_base(4,8,0)) && !(MIN_VERSION_QuickCheck(2,9,0)) import Numeric.Natural (Natural) #endif #if !MIN_VERSION_QuickCheck(2,9,0) import Control.Applicative (Const (..)) import Data.List.NonEmpty (NonEmpty (..)) import Data.Version (Version (..)) #endif import Data.Proxy (Proxy (..)) import Data.Tagged (Tagged (..)) #if !MIN_VERSION_quickcheck_instances(0,3,12) instance Arbitrary a => Arbitrary (Vector a) where arbitrary = V.fromList <$> arbitrary shrink = fmap V.fromList . shrink . V.toList #endif #if !(MIN_VERSION_QuickCheck(2,8,0) && MIN_VERSION_base(4,8,0)) && !(MIN_VERSION_QuickCheck(2,9,0)) instance Arbitrary Natural where arbitrary = fmap (fromInteger . abs) arbitrary #endif #if !MIN_VERSION_QuickCheck(2,9,0) instance Arbitrary a => Arbitrary (NonEmpty a) where arbitrary = do x <- arbitrary xs <- arbitrary return (x :| xs) instance Arbitrary a => Arbitrary (Const a b) where arbitrary = fmap Const arbitrary instance Arbitrary Version where arbitrary = do x <- fmap abs arbitrary xs <- (fmap . fmap) abs arbitrary return $ Version (x : xs) [] #endif instance Arbitrary a => Arbitrary (Tagged t a) where arbitrary = fmap Tagged arbitrary instance Arbitrary (Proxy a) where arbitrary = return Proxy aeson-compat-0.3.6/test/Tests.hs0000644000000000000000000000725712765475716014750 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} module Main (main, ex) where import Prelude () import Prelude.Compat import Control.Applicative (Const) import Data.List.NonEmpty (NonEmpty) import Data.Proxy (Proxy) import Data.Tagged (Tagged) import Data.Text (Text) import Data.Time (Day, LocalTime, NominalDiffTime) import Data.Version (Version) import Numeric.Natural (Natural) import Data.Orphans () import Test.QuickCheck.Instances () import Test.Tasty import Test.Tasty.HUnit import Test.Tasty.QuickCheck import Data.Aeson.Compat import Orphans () main :: IO () main = defaultMain $ testGroup "Tests" [ dotColonMark , testGroup "Roundtrip" [ testProperty "Day" $ roundtripBroken10 (undefined :: Day) , testProperty "LocalTime" $ roundtripBroken10 (undefined :: LocalTime) , testProperty "Version" $ roundtrip (undefined :: Version) , testProperty "Ordering" $ roundtrip (undefined :: Ordering) , testProperty "Natural" $ roundtrip (undefined :: Natural) , testProperty "Const" $ roundtrip (undefined :: Const Int Int) , testProperty "Proxy" $ roundtrip (undefined :: Proxy Int) , testProperty "Tagged" $ roundtrip (undefined :: Tagged Int Int) , testProperty "NonEmpty" $ roundtrip (undefined :: NonEmpty Int) , testProperty "NominalDiffTime" $ roundtrip (undefined :: NominalDiffTime) ] ] ------------------------------------------------------------------------------ -- Comparison (.:?) and (.:!) ------------------------------------------------------------------------------ newtype T1 = T1 (Maybe Int) deriving (Eq, Show) newtype T2 = T2 (Maybe Int) deriving (Eq, Show) newtype T3 = T3 (Maybe Int) deriving (Eq, Show) instance FromJSON T1 where parseJSON = fmap T1 . withObject "T1" (.: "value") instance FromJSON T2 where parseJSON = fmap T2 . withObject "T2" (.:? "value") instance FromJSON T3 where parseJSON = fmap T3 . withObject "T3" (.:! "value") dotColonMark :: TestTree dotColonMark = testGroup "Operators" $ fmap t [ assertEqual ".: not-present" Nothing (decode ex1 :: Maybe T1) , assertEqual ".: 42" (Just (T1 (Just 42))) (decode ex2 :: Maybe T1) , assertEqual ".: null" (Just (T1 Nothing)) (decode ex3 :: Maybe T1) , assertEqual ".:? not-present" (Just (T2 (Nothing))) (decode ex1 :: Maybe T2) , assertEqual ".:? 42" (Just (T2 (Just 42))) (decode ex2 :: Maybe T2) , assertEqual ".:? null" (Just (T2 Nothing)) (decode ex3 :: Maybe T2) , assertEqual ".:! not-present" (Just (T3 (Nothing))) (decode ex1 :: Maybe T3) , assertEqual ".:! 42" (Just (T3 (Just 42))) (decode ex2 :: Maybe T3) , assertEqual ".:! null" Nothing (decode ex3 :: Maybe T3) ] where ex1 = "{}" ex2 = "{\"value\": 42 }" ex3 = "{\"value\": null }" t = testCase "-" roundtrip :: (Arbitrary a, Eq a, Show a, ToJSON a, FromJSON a) => a -> a -> Property roundtrip _ x = Right x === (eitherDecode . encode $ x) roundtripBroken10 :: (Arbitrary a, Eq a, Show a, ToJSON a, FromJSON a) => a -> a -> Property #if MIN_VERSION_aeson(0,10,0) && !MIN_VERSION_aeson(0,11,0) roundtripBroken10 _ x = property $ case eitherDecode . encode $ x of Right y -> False && x == y -- x and y of the same type! Left _ -> True #else roundtripBroken10 = roundtrip #endif ------------------------------------------------------------------------------- -- tests that symbols are exported ------------------------------------------------------------------------------- ex :: (Text, Value) ex = "foo" .= True