json-alt (empty) → 1.0.0
raw patch · 6 files changed
+168/−0 lines, 6 filesdep +aesondep +basesetup-changed
Dependencies added: aeson, base
Files
- Data/Aeson/AutoType/Alternative.hs +48/−0
- LICENSE +34/−0
- README.md +3/−0
- Setup.hs +2/−0
- changelog.md +4/−0
- json-alt.cabal +77/−0
+ Data/Aeson/AutoType/Alternative.hs view
@@ -0,0 +1,48 @@+{-# LANGUAGE TypeOperators #-}+-- | This module defines data type (a :|: b) that behaves all like @Either@,+-- except that has no tag in JSON representation as used by @FromJSON@ and @ToJSON@.+module Data.Aeson.AutoType.Alternative(+ (:|:)(..)+ , toEither, fromEither+ , alt+ ) where++import Data.Aeson+import Control.Applicative++-- | Data type (a :|: b) that behaves all like @Either@,+-- except that has no tag in JSON representation as used by @FromJSON@ and @ToJSON@.+data a :|: b = AltLeft a+ | AltRight b+ deriving(Show,Eq,Ord)+infixr 5 :|:++-- | Convert to @Either@ datatype.+toEither :: a :|: b -> Either a b+toEither (AltLeft a) = Left a+toEither (AltRight b) = Right b+{-# INLINE toEither #-}++-- | Convert from @Either@ datatype.+fromEither :: Either a b -> a :|: b+fromEither (Left a) = AltLeft a+fromEither (Right b) = AltRight b+{-# INLINE fromEither #-}++-- | Deconstruct the type with two functions corresponding to constructors.+-- This is like @either@.+alt :: (a -> c) -> (b -> c) -> a :|: b -> c+alt f _ (AltLeft a) = f a+alt _ g (AltRight b) = g b+infixr 5 `alt`++instance (ToJSON a, ToJSON b) => ToJSON (a :|: b) where+ toJSON (AltLeft a) = toJSON a+ toJSON (AltRight b) = toJSON b+ {-# INLINE toJSON #-}++instance (FromJSON a, FromJSON b) => FromJSON (a :|: b) where+ parseJSON input = (AltLeft <$> parseJSON input) <|>+ (AltRight <$> parseJSON input) <|>+ fail ("Neither alternative was found for: " ++ show input)+ {-# INLINE parseJSON #-}
+ LICENSE view
@@ -0,0 +1,34 @@+This repository contains some test JSON examples in test/*.json+that is downloaded from Twitter API and http://www.jquery4u.com/json/.+Naturally these are covered by other licenses.++Code copyright (c) 2014, Michal J. Gajda++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 Michal J. Gajda 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.
+ README.md view
@@ -0,0 +1,3 @@+json-alt+========+Runtime module for `json-autotype` that allows easy parsing of JSON values that have more than a single Haskell type.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ changelog.md view
@@ -0,0 +1,4 @@+Changelog+=========+ 1.0.0 Nov 2018+ * Split `json-alt` from `json-autotype` package as a runtime library.
+ json-alt.cabal view
@@ -0,0 +1,77 @@+-- Build information for the package.+name: json-alt+version: 1.0.0+synopsis: Union 'alternative' or Either that has untagged JSON encoding.+description: Parsing JSON with Aeson often requires decoding fields+ that have more than one Haskell type.+ .+ So we have:+ ```+ data a :|: b = AltLeft a+ | AltLeft b++ printIt = print . (fromJSON :: ByteString -> Int :|: Bool)+ main = do+ printIt "1" -- AltLeft 1+ printIt "true" -- AltRight True+ printIt "null" -- errors!+ ```+ To generate types for larger JSON documents,+ you might use `json-autotype`.+ .+ This is separate package so that users+ do not have to keep `json-autotype` as runtime+ dependency.+ .+ See <https://github.com/mgajda/json-autotype>+homepage: https://github.com/mgajda/json-autotype+license: BSD3+license-file: LICENSE+stability: stable+author: Michal J. Gajda+maintainer: simons@cryp.to+ mjgajda@gmail.com+copyright: Copyright by Michal J. Gajda '2014-'2018+category: Data, Tools+build-type: Simple+extra-source-files: README.md changelog.md+cabal-version: >=1.10+bug-reports: https://github.com/mgajda/json-autotype/issues+tested-with: GHC==7.6.1+ , GHC==7.6.2+ , GHC==7.6.3+ , GHC==7.8.1+ , GHC==7.8.2+ , GHC==7.8.3+ , GHC==7.8.4+ , GHC==7.10.1+ , GHC==7.10.2+ , GHC==7.10.3+ , GHC==8.0.1+ , GHC==8.0.2+ , GHC==8.2.2+ , GHC==8.4.1+ , GHC==8.4.2+ , GHC==8.4.3+ , GHC==8.4.4+ , GHC==8.6.2+ , GHC==8.6.1++source-repository head+ type: git+ location: https://github.com/mgajda/json-autotype.git++library+ exposed-modules: Data.Aeson.AutoType.Alternative++ other-extensions: TemplateHaskell,+ ScopedTypeVariables,+ OverloadedStrings,+ FlexibleInstances,+ MultiParamTypeClasses,+ DeriveDataTypeable,+ DeriveGeneric,+ RecordWildCards+ build-depends: base >=4.3 && <5,+ aeson >=1.2.1 && <1.5+ default-language: Haskell2010