Skip to content

List instance #382

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

Closed
wants to merge 2 commits into from
Closed
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
34 changes: 34 additions & 0 deletions Data/Aeson/Types/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ module Data.Aeson.Types.Class
, typeMismatch
) where

import Data.Aeson.Encode.Builder
import Data.Aeson.Types.Internal
import Data.Monoid ((<>))
import Data.Text (Text)
import GHC.Generics (Generic, Rep, from, to)
import qualified Data.Aeson.Encode.Builder as E
import qualified Data.ByteString.Builder as B
import qualified Data.Vector as V

#if !MIN_VERSION_base(4,8,0)
import Data.Monoid (mempty)
#endif

-- | Class of generic representation types ('Rep') that can be converted to
-- JSON.
Expand Down Expand Up @@ -165,6 +173,19 @@ class ToJSON a where
toEncoding = Encoding . E.encodeToBuilder . toJSON
{-# INLINE toEncoding #-}

toJSONList :: [a] -> Value
toJSONList = Array . V.fromList . map toJSON
{-# INLINE toJSONList #-}

toEncodingList :: [a] -> Encoding
toEncodingList [] = emptyArray_
toEncodingList (x:xs) = Encoding $
B.char7 '[' <> builder x <> commas xs <> B.char7 ']'
where
commas = foldr (\v vs -> B.char7 ',' <> builder v <> vs) mempty
builder = fromEncoding . toEncoding
{-# INLINE toEncodingList #-}

-- | A type that can be converted from JSON, with the possibility of
-- failure.
--
Expand Down Expand Up @@ -245,6 +266,19 @@ class FromJSON a where
default parseJSON :: (Generic a, GFromJSON (Rep a)) => Value -> Parser a
parseJSON = genericParseJSON defaultOptions

parseJSONList :: Value -> Parser [a]
parseJSONList (Array a)
= sequence
. zipWith parseIndexedJSON [0..]
. V.toList
$ a
where
parseIndexedJSON :: FromJSON a => Int -> Value -> Parser a
parseIndexedJSON idx value = parseJSON value <?> Index idx

parseJSONList v = typeMismatch "[a]" v


-- | A key-value pair for encoding a JSON object.
class KeyValue kv where
(.=) :: ToJSON v => Text -> v -> kv
Expand Down
44 changes: 21 additions & 23 deletions Data/Aeson/Types/Instances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
ViewPatterns #-}
{-# LANGUAGE DefaultSignatures #-}

#define NEEDS_INCOHERENT
#include "overlapping-compat.h"

{-# OPTIONS_GHC -fno-warn-orphans #-}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

-- TODO: Drop this when we remove support for Data.Attoparsec.Number
Expand Down Expand Up @@ -62,7 +59,7 @@ module Data.Aeson.Types.Instances
) where

import Control.Applicative (Const(..))
import Data.Aeson.Encode.Functions (brackets, builder, encode, foldable, list)
import Data.Aeson.Encode.Functions (brackets, builder, encode, foldable)
import Data.Aeson.Functions (hashMapKey, mapHashKeyVal, mapKey, mapKeyVal)
import Data.Aeson.Types.Class
import Data.Aeson.Types.Internal
Expand Down Expand Up @@ -92,6 +89,7 @@ import Text.ParserCombinators.ReadP (readP_to_S)
import Foreign.Storable (Storable)
import Numeric.Natural (Natural)
import Prelude hiding (foldr)
import qualified Prelude
import qualified Data.Aeson.Encode.Builder as E
import qualified Data.Aeson.Parser.Time as Time
import qualified Data.ByteString.Builder as B
Expand Down Expand Up @@ -224,31 +222,29 @@ instance FromJSON () where
else fail "Expected an empty array"
{-# INLINE parseJSON #-}

instance INCOHERENT_ ToJSON [Char] where
toJSON = String . T.pack
{-# INLINE toJSON #-}

toEncoding = Encoding . E.string
{-# INLINE toEncoding #-}

instance INCOHERENT_ FromJSON [Char] where
parseJSON = withText "String" $ pure . T.unpack
{-# INLINE parseJSON #-}

instance ToJSON Char where
toJSON = String . T.singleton
{-# INLINE toJSON #-}

toJSONList = String . T.pack
{-# INLINE toJSONList #-}

toEncoding = Encoding . E.string . (:[])
{-# INLINE toEncoding #-}

toEncodingList = Encoding . E.string
{-# INLINE toEncodingList #-}

instance FromJSON Char where
parseJSON = withText "Char" $ \t ->
if T.compareLength t 1 == EQ
then pure $ T.head t
else fail "Expected a string of length 1"
{-# INLINE parseJSON #-}

parseJSONList = withText "String" $ pure . T.unpack
{-# INLINE parseJSONList #-}

instance ToJSON Scientific where
toJSON = Number
{-# INLINE toJSON #-}
Expand Down Expand Up @@ -493,10 +489,13 @@ instance FromJSON LT.Text where
{-# INLINE parseJSON #-}

instance (ToJSON a) => ToJSON (NonEmpty a) where
toJSON = toJSON . toList
toJSON = Array . V.fromList . map toJSON . toList
{-# INLINE toJSON #-}

toEncoding = toEncoding . toList
toEncoding (x :| xs) = Encoding $
B.char7 '[' <> builder x <> commas xs <> B.char7 ']'
where
commas = Prelude.foldr (\v vs -> B.char7 ',' <> builder v <> vs) mempty
{-# INLINE toEncoding #-}

instance (FromJSON a) => FromJSON (NonEmpty a) where
Expand All @@ -506,16 +505,15 @@ instance (FromJSON a) => FromJSON (NonEmpty a) where
ne [] = fail "Expected a NonEmpty but got an empty list"
ne (x:xs) = pure (x :| xs)

instance OVERLAPPABLE_ (ToJSON a) => ToJSON [a] where
toJSON = Array . V.fromList . map toJSON
instance (ToJSON a) => ToJSON [a] where
toJSON = toJSONList
{-# INLINE toJSON #-}

toEncoding xs = list xs
toEncoding = toEncodingList
{-# INLINE toEncoding #-}

instance OVERLAPPABLE_ (FromJSON a) => FromJSON [a] where
parseJSON = withArray "[a]" $ Tr.sequence .
zipWith parseIndexedJSON [0..] . V.toList
instance (FromJSON a) => FromJSON [a] where
parseJSON = parseJSONList
{-# INLINE parseJSON #-}

instance (ToJSON a) => ToJSON (Seq.Seq a) where
Expand Down
4 changes: 2 additions & 2 deletions tests/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ overlappingRegression bs = fromMaybe [] $ decode bs
issue351 :: [Assertion]
issue351 = [
assertEqual "Int" ([1, 2, 3] :: [Int]) $ overlappingRegression "[1, 2, 3]"
, assertEqual "Char" ("" :: String) $ overlappingRegression "\"abc\""
, assertEqual "Char" ("abc" :: String) $ overlappingRegression "[\"a\", \"b\", \"c\"]"
, assertEqual "Char" ("abc" :: String) $ overlappingRegression "\"abc\""
, assertEqual "Char" ("" :: String) $ overlappingRegression "[\"a\", \"b\", \"c\"]"
]

------------------------------------------------------------------------------
Expand Down