fixed-vector-aeson (empty) → 2.0.0.0
raw patch · 4 files changed
+180/−0 lines, 4 filesdep +aesondep +basedep +fixed-vector
Dependencies added: aeson, base, fixed-vector, vector
Files
- ChangeLog.md +3/−0
- Data/Vector/Fixed/Instances/Aeson.hs +107/−0
- LICENSE +30/−0
- fixed-vector-aeson.cabal +40/−0
+ ChangeLog.md view
@@ -0,0 +1,3 @@+2.0.0.0+-------+* Initial release
+ Data/Vector/Fixed/Instances/Aeson.hs view
@@ -0,0 +1,107 @@+{-# LANGUAGE DerivingVia #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE StandaloneDeriving #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE UndecidableInstances #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}+-- | Module with @aeson@ instances for data types defined in fixed+-- vector+module Data.Vector.Fixed.Instances.Aeson+ ( fixedVectorParseJSON+ , fixedVectorToJSON+ , fixedVectorToEncoding+ ) where++import Control.Monad+import Control.Monad.ST+import Data.Vector.Fixed (Arity,ArityPeano,ViaFixed(..),Vector)+import qualified Data.Vector.Fixed as F+import qualified Data.Vector.Fixed.Boxed as FB+import qualified Data.Vector.Fixed.Strict as FF+import qualified Data.Vector.Fixed.Unboxed as FU+import qualified Data.Vector.Fixed.Primitive as FP+import qualified Data.Vector.Fixed.Storable as FS+import Data.Aeson+import Data.Aeson.Types++import qualified Data.Vector as V+import qualified Data.Vector.Mutable as MV+++----------------------------------------------------------------+-- Generic implementations+----------------------------------------------------------------++-- | Generic implementation of 'parseJSON' for data types which are+-- instances of 'Vector'.+fixedVectorParseJSON :: forall v a. (Vector v a, FromJSON a) => Value -> Parser (v a)+{-# INLINE fixedVectorParseJSON #-}+fixedVectorParseJSON = withArray "fixed-vector" $ \arr -> do+ let expected = F.length (undefined :: v a)+ when (V.length arr /= expected) $+ fail $ "Expecting array of length " ++ show expected+ F.generateM $ \i -> parseJSON (arr V.! i)++-- | Generic implementation of 'toJSON' for data types which are+-- instances of 'Vector'.+fixedVectorToJSON :: forall v a. (Vector v a, ToJSON a) => v a -> Value+{-# INLINE fixedVectorToJSON #-}+fixedVectorToJSON v = Array $ runST $ do+ -- NOTE: (!) from fixed vector could have O(n) complexity so let+ -- fold over fixed vector. Access to vector _is_ O(1)+ vec <- MV.unsafeNew n+ flip F.imapM_ v $ \i a -> MV.unsafeWrite vec i (toJSON a)+ V.unsafeFreeze vec+ where+ n = F.length v++-- | Generic implementation of 'toEncoding' for data types which are+-- instances of 'Vector'.+fixedVectorToEncoding :: forall v a. (Vector v a, ToJSON a) => v a -> Encoding+{-# INLINE fixedVectorToEncoding #-}+fixedVectorToEncoding = foldable . F.cvec+++----------------------------------------------------------------+-- Instances+----------------------------------------------------------------++instance (Vector v a, FromJSON a) => FromJSON (ViaFixed v a) where+ {-# INLINE parseJSON #-}+ parseJSON = fixedVectorParseJSON++instance (Vector v a, ToJSON a) => ToJSON (ViaFixed v a) where+ toJSON = fixedVectorToJSON+ toEncoding = fixedVectorToEncoding+ {-# INLINE toJSON #-}+ {-# INLINE toEncoding #-}++++deriving via ViaFixed (FB.Vec n) a instance (Arity n, FromJSON a) => FromJSON (FB.Vec n a)+deriving via ViaFixed (FB.Vec n) a instance (Arity n, ToJSON a) => ToJSON (FB.Vec n a)+deriving via ViaFixed (FF.Vec n) a instance (Arity n, FromJSON a) => FromJSON (FF.Vec n a)+deriving via ViaFixed (FF.Vec n) a instance (Arity n, ToJSON a) => ToJSON (FF.Vec n a)+deriving via ViaFixed (FP.Vec n) a instance (Arity n, FromJSON a, FP.Prim a) => FromJSON (FP.Vec n a)+deriving via ViaFixed (FP.Vec n) a instance (Arity n, ToJSON a, FP.Prim a) => ToJSON (FP.Vec n a)+deriving via ViaFixed (FS.Vec n) a instance (Arity n, FromJSON a, FS.Storable a) => FromJSON (FS.Vec n a)+deriving via ViaFixed (FS.Vec n) a instance (Arity n, ToJSON a, FS.Storable a) => ToJSON (FS.Vec n a)+deriving via ViaFixed (FU.Vec n) a instance (Arity n, FromJSON a, FU.Unbox n a) => FromJSON (FU.Vec n a)+deriving via ViaFixed (FU.Vec n) a instance (Arity n, ToJSON a, FU.Unbox n a) => ToJSON (FU.Vec n a)++deriving via ViaFixed (F.VecList n) a instance (Arity n, FromJSON a) => FromJSON (F.VecList n a)+deriving via ViaFixed (F.VecList n) a instance (Arity n, ToJSON a) => ToJSON (F.VecList n a)+deriving via ViaFixed (F.VecPeano n) a instance (ArityPeano n, FromJSON a) => FromJSON (F.VecPeano n a)+deriving via ViaFixed (F.VecPeano n) a instance (ArityPeano n, ToJSON a) => ToJSON (F.VecPeano n a)++deriving via ViaFixed F.Only a instance (FromJSON a) => FromJSON (F.Only a)+deriving via ViaFixed F.Only a instance (ToJSON a) => ToJSON (F.Only a)++instance FromJSON (F.Empty a) where+ parseJSON = withArray "fixed-vector: Empty" $ \arr -> do+ unless (V.null arr) $ fail "Nonempty array"+ pure F.Empty+instance ToJSON (F.Empty a) where+ toJSON _ = Array V.empty+ toEncoding _ = toEncoding ([]::[Value])
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) Aleksey Khudyakov++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:++1. Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++2. 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.++3. Neither the name of the author nor the names of his contributors+ may be used to endorse or promote products derived from this software+ without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE 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 AUTHORS 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.
+ fixed-vector-aeson.cabal view
@@ -0,0 +1,40 @@+Name: fixed-vector-aeson+Version: 2.0.0.0+Synopsis: Aeson instances for fixed-vector+Description:+ This package contains FromJSON and ToJSON instances for data types defined in+ fixed-vector package.+ +Cabal-Version: >= 1.10+License: BSD3+License-File: LICENSE+Author: Aleksey Khudyakov <alexey.skladnoy@gmail.com>+Maintainer: Aleksey Khudyakov <alexey.skladnoy@gmail.com>+Bug-reports: https://github.com/Shimuuar/fixed-vector/issues+Category: Data+Build-Type: Simple+extra-source-files:+ ChangeLog.md++tested-with:+ GHC ==8.10.7+ || ==9.0.1+ || ==9.2.8+ || ==9.4.7+ || ==9.6.6+ || ==9.8.2+ || ==9.10.1++source-repository head+ type: git+ location: http://github.com/Shimuuar/fixed-vector++Library+ Ghc-options: -Wall+ Default-Language: Haskell2010+ Build-Depends: base >=4.14 && <5+ , fixed-vector >=2.0+ , aeson >=2+ , vector+ Exposed-modules:+ Data.Vector.Fixed.Instances.Aeson