encode arrays an vectors

This commit is contained in:
Tom Sydney Kerckhove 2021-10-30 12:11:41 +02:00
parent f2cc6badbf
commit f03d879004
6 changed files with 11 additions and 7 deletions

View File

@ -28,7 +28,7 @@ parseJSONVia = flip go
BoolCodec -> parseJSON value
StringCodec -> parseJSON value
NumberCodec -> parseJSON value
ArrayCodec mname c -> withArray (maybe "Unnamed" T.unpack mname) (\a -> toList <$> mapM (`go` c) a) value
ArrayCodec mname c -> withArray (maybe "Unnamed" T.unpack mname) (mapM (`go` c)) value
ObjectCodec mname c -> withObject (maybe "Unnamed" T.unpack mname) (\o -> goObject o c) value
EqCodec expected c -> do
actual <- go value c

View File

@ -22,7 +22,7 @@ toJSONVia = flip go
BoolCodec -> toJSON (a :: Bool)
StringCodec -> toJSON (a :: Text)
NumberCodec -> toJSON (a :: Scientific)
ArrayCodec _ c -> toJSON (map (`go` c) a)
ArrayCodec _ c -> toJSON (fmap (`go` c) a)
ObjectCodec _ oc -> JSON.Object (goObject a oc)
EqCodec value c -> go value c
MapCodec _ g c -> go (g a) c

View File

@ -37,4 +37,5 @@ library
, mtl
, scientific
, text
, vector
default-language: Haskell2010

View File

@ -20,3 +20,4 @@ library:
- mtl
- scientific
- text
- vector

View File

@ -12,6 +12,7 @@ import Data.Int
import Data.Scientific
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
import qualified Data.Vector as V
import Data.Word
class HasCodec a where
@ -24,7 +25,7 @@ class HasCodec a where
--
-- This is really only useful for cases like 'Char' and 'String'
listCodec :: Codec [a] [a]
listCodec = ArrayCodec Nothing codec
listCodec = bimapCodec V.toList V.fromList $ ArrayCodec Nothing codec
{-# MINIMAL codec #-}

View File

@ -15,6 +15,7 @@ import Data.Set (Set)
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T
import Data.Vector (Vector)
-- | A Self-documenting encoder and decoder,
--
@ -55,16 +56,16 @@ data Codec input output where
NumberCodec ::
-- |
Codec Scientific Scientific
-- TODO use a vector here because that's what aeson uses.
-- | Encode a 'Vector' of values as an @array@ value, and decode an @array@ value as a 'Vector' of values.
ArrayCodec ::
-- |
-- | Name of the array, for error messages and documentation.
!(Maybe Text) ->
-- |
!(Codec input output) ->
-- |
Codec [input] [output]
Codec (Vector input) (Vector output)
ObjectCodec ::
-- |
-- | Name of the array, for error messages and documentation.
!(Maybe Text) ->
-- |
!(ObjectCodec value value) ->