Skip to content
Open
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
5 changes: 2 additions & 3 deletions json-alt/json-alt.cabal
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.34.4.
-- This file has been generated from package.yaml by hpack version 0.35.0.
--
-- see: https://github.com/sol/hpack
--

name: json-alt
version: 1.0.1
Expand Down Expand Up @@ -64,6 +63,6 @@ library
DeriveGeneric
RecordWildCards
build-depends:
aeson >=1.2.1 && <1.6
aeson >=1.2.1 && <2.1
, base >=4.3 && <5
default-language: Haskell2010
4 changes: 2 additions & 2 deletions json-alt/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json-alt
version: '1.0.0'
version: '1.0.1'
synopsis: Union 'alternative' or Either that has untagged JSON encoding.
description: ! 'Parsing JSON with Aeson often requires decoding fields

Expand Down Expand Up @@ -61,7 +61,7 @@ other-extensions:
- RecordWildCards
dependencies:
- base >=4.3 && <5
- aeson >=1.2.1 && <1.6
- aeson >=1.2.1 && <2.1
library:
exposed-modules:
- Data.Aeson.AutoType.Alternative
Expand Down
44 changes: 22 additions & 22 deletions json-autotype/json-autotype.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.34.4.
-- This file has been generated from package.yaml by hpack version 0.35.0.
--
-- see: https://github.com/sol/hpack

Expand Down Expand Up @@ -111,20 +111,20 @@ library
build-depends:
GenericPretty ==1.2.*
, QuickCheck >=2.4 && <3.0
, aeson >=1.2.1 && <1.6
, aeson >=1.2.1 && <2.1
, base >=4.9 && <5
, containers >=0.3 && <0.7
, data-default ==0.7.*
, filepath >=1.3 && <1.5
, hashable >=1.2 && <1.4
, hashable >=1.2 && <1.5
, json-alt
, lens >=4.1 && <4.20
, lens >=4.1 && <5.2
, mtl >=2.1 && <2.3
, pretty >=1.1 && <1.3
, process >=1.1 && <1.7
, run-haskell-module
, scientific >=0.3 && <0.5
, smallcheck >=1.0 && <1.2
, smallcheck >=1.0 && <1.3
, template-haskell
, text >=1.1 && <1.4
, uniplate ==1.6.*
Expand All @@ -151,15 +151,15 @@ executable json-autotype
RecordWildCards
build-depends:
GenericPretty ==1.2.*
, aeson >=1.2.1 && <1.6
, aeson >=1.2.1 && <2.1
, base >=4.9 && <5
, bytestring >=0.9 && <0.11
, bytestring >=0.9 && <0.12
, containers >=0.3 && <0.7
, filepath >=1.3 && <1.5
, hashable >=1.2 && <1.4
, hashable >=1.2 && <1.5
, json-alt
, json-autotype
, lens >=4.1 && <4.20
, lens >=4.1 && <5.2
, mtl >=2.1 && <2.3
, optparse-applicative >=0.12 && <1.0
, pretty >=1.1 && <1.3
Expand Down Expand Up @@ -194,21 +194,21 @@ test-suite json-autotype-examples
build-depends:
GenericPretty ==1.2.*
, QuickCheck >=2.4 && <3.0
, aeson >=1.2.1 && <1.6
, aeson >=1.2.1 && <2.1
, base >=4.9 && <5
, containers >=0.3 && <0.7
, directory >=1.1 && <1.4
, filepath >=1.3 && <1.5
, hashable >=1.2 && <1.4
, hashable >=1.2 && <1.5
, json-alt
, json-autotype
, lens >=4.1 && <4.20
, lens >=4.1 && <5.2
, mtl >=2.1 && <2.3
, optparse-applicative >=0.11 && <1.0
, pretty >=1.1 && <1.3
, process >=1.1 && <1.7
, scientific >=0.3 && <0.5
, smallcheck >=1.0 && <1.2
, smallcheck >=1.0 && <1.3
, template-haskell
, text >=1.1 && <1.4
, uniplate ==1.6.*
Expand Down Expand Up @@ -237,22 +237,22 @@ test-suite json-autotype-gen-test
build-depends:
GenericPretty ==1.2.*
, QuickCheck >=2.4 && <3.0
, aeson >=1.2.1 && <1.6
, aeson >=1.2.1 && <2.1
, base >=4.9 && <5
, bytestring >=0.9 && <0.11
, bytestring >=0.9 && <0.12
, containers >=0.3 && <0.7
, directory >=1.1 && <1.4
, filepath >=1.3 && <1.5
, hashable >=1.2 && <1.4
, hashable >=1.2 && <1.5
, json-alt
, json-autotype
, lens >=4.1 && <4.20
, lens >=4.1 && <5.2
, mtl >=2.1 && <2.3
, optparse-applicative >=0.12 && <1.0
, pretty >=1.1 && <1.3
, process >=1.1 && <1.7
, scientific >=0.3 && <0.5
, smallcheck >=1.0 && <1.2
, smallcheck >=1.0 && <1.3
, template-haskell
, text >=1.1 && <1.4
, uniplate ==1.6.*
Expand Down Expand Up @@ -281,20 +281,20 @@ test-suite json-autotype-qc-test
build-depends:
GenericPretty ==1.2.*
, QuickCheck >=2.4 && <3.0
, aeson >=1.2.1 && <1.6
, aeson >=1.2.1 && <2.1
, base >=4.9 && <5
, containers >=0.3 && <0.7
, filepath >=1.3 && <1.5
, hashable >=1.2 && <1.4
, hashable >=1.2 && <1.5
, json-alt
, json-autotype
, lens >=4.1 && <4.20
, lens >=4.1 && <5.2
, mtl >=2.1 && <2.3
, optparse-applicative >=0.12 && <1.0
, pretty >=1.1 && <1.3
, process >=1.1 && <1.7
, scientific >=0.3 && <0.5
, smallcheck >=1.0 && <1.2
, smallcheck >=1.0 && <1.3
, template-haskell
, text >=1.1 && <1.4
, uniplate ==1.6.*
Expand Down
20 changes: 10 additions & 10 deletions json-autotype/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json-autotype
version: "3.1.2"
version: "3.1.3"
synopsis: Automatic type declaration for JSON input data
description: |
Generates datatype declarations with Aeson''s ''Data.Aeson.FromJSON''
Expand Down Expand Up @@ -75,11 +75,11 @@ other-extensions:
dependencies:
- base >=4.9 && <5
- GenericPretty >=1.2 && <1.3
- aeson >=1.2.1 && <1.6
- aeson >=1.2.1 && <2.1
- containers >=0.3 && <0.7
- filepath >=1.3 && <1.5
- hashable >=1.2 && <1.4
- lens >=4.1 && <4.20
- hashable >=1.2 && <1.5
- lens >=4.1 && <5.2
- mtl >=2.1 && <2.3
- pretty >=1.1 && <1.3
- process >=1.1 && <1.7
Expand Down Expand Up @@ -109,7 +109,7 @@ library:
- Data.Aeson.AutoType.Nested
dependencies:
- data-default >=0.7 && <0.8
- smallcheck >=1.0 && <1.2
- smallcheck >=1.0 && <1.3
- QuickCheck >=2.4 && <3.0
- run-haskell-module
executables:
Expand All @@ -119,7 +119,7 @@ executables:
- app
- common
dependencies:
- bytestring >=0.9 && <0.11
- bytestring >=0.9 && <0.12
- optparse-applicative >=0.12 && <1.0
- yaml >=0.8 && <0.12
- json-autotype
Expand All @@ -132,7 +132,7 @@ tests:
dependencies:
- directory >=1.1 && <1.4
- optparse-applicative >=0.11 && <1.0
- smallcheck >=1.0 && <1.2
- smallcheck >=1.0 && <1.3
- QuickCheck >=2.4 && <3.0
- json-autotype
json-autotype-qc-test:
Expand All @@ -141,7 +141,7 @@ tests:
- test/qc
- common
dependencies:
- smallcheck >=1.0 && <1.2
- smallcheck >=1.0 && <1.3
- optparse-applicative >=0.12 && <1.0
- QuickCheck >=2.4 && <3.0
- json-autotype
Expand All @@ -151,10 +151,10 @@ tests:
- test/gen
- common
dependencies:
- bytestring >=0.9 && <0.11
- bytestring >=0.9 && <0.12
- directory >=1.1 && <1.4
- optparse-applicative >=0.12 && <1.0
- smallcheck >=1.0 && <1.2
- smallcheck >=1.0 && <1.3
- QuickCheck >=2.4 && <3.0
- json-autotype
stability: stable
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ makeFromJSON identifier contents =
makeParser identifier _ = Text.unwords [identifier, "<$>", inner]
inner = " <*> " `Text.intercalate`
map takeValue contents
takeValue (jsonId, _, ty, True ) = Text.concat ["v .:? \"", jsonId, "\""] -- nullable types
takeValue (jsonId, _, _ , False) = Text.concat ["v .: \"", jsonId, "\""]
takeValue (jsonId, _, ty, True ) = Text.concat ["v .:? ", escapeText jsonId] -- nullable types
takeValue (jsonId, _, _ , False) = Text.concat ["v .: ", escapeText jsonId]
escapeText = Text.pack . show . Text.unpack
-- Contents example for wrapFromJSON:
-- " <$>
--" v .: "hexValue" <*>
Expand Down
13 changes: 7 additions & 6 deletions json-autotype/src/Data/Aeson/AutoType/Extract.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Data.Aeson.AutoType.Extract(valueSize, valueTypeSize,
import Control.Arrow ((&&&))
import Control.Exception (assert)
import Data.Aeson.AutoType.Type
import Data.Aeson.KeyMap (toHashMapText)
import qualified Data.Graph as Graph
import qualified Data.HashMap.Strict as Map
import Data.HashMap.Strict (HashMap)
Expand All @@ -31,7 +32,7 @@ valueSize (Bool _) = 1
valueSize (Number _) = 1
valueSize (String _) = 1
valueSize (Array a) = V.foldl' (+) 1 $ V.map valueSize a
valueSize (Object o) = (1+) . sum . map valueSize . Map.elems $ o
valueSize (Object o) = (1+) . sum . map valueSize . Map.elems $ toHashMapText o

-- | Compute total size of the type of the @Value@.
-- For:
Expand All @@ -45,7 +46,7 @@ valueTypeSize (Bool _) = 1
valueTypeSize (Number _) = 1
valueTypeSize (String _) = 1
valueTypeSize (Array a) = (1+) . V.foldl' max 0 $ V.map valueTypeSize a
valueTypeSize (Object o) = (1+) . sum . map valueTypeSize . Map.elems $ o
valueTypeSize (Object o) = (1+) . sum . map valueTypeSize . Map.elems $ toHashMapText o

-- | Compute total depth of the value.
-- For:
Expand All @@ -57,13 +58,13 @@ valueDepth (Bool _) = 1
valueDepth (Number _) = 1
valueDepth (String _) = 1
valueDepth (Array a) = (1+) . V.foldl' max 0 $ V.map valueDepth a
valueDepth (Object o) = (1+) . maximum . (0:) . map valueDepth . Map.elems $ o
valueDepth (Object o) = (1+) . maximum . (0:) . map valueDepth . Map.elems $ toHashMapText o

-- | Check if a number is integral, or floating point
-- | Extract @Type@ from the JSON @Value@.
-- Unifying types of array elements, if necessary.
extractType :: Value -> Type
extractType (Object o) = TObj $ Dict $ Map.map extractType o
extractType (Object o) = TObj $ Dict $ Map.map extractType $ toHashMapText o
extractType Null = TNull
extractType (Bool _) = TBool
extractType (Number n) | isInteger n = TInt
Expand All @@ -86,11 +87,11 @@ typeCheck (Number _) TDouble = True
typeCheck (Array elts) (TArray eltType) = (`typeCheck` eltType) `all` V.toList elts
typeCheck (Object d) (TObj e ) = typeCheckKey `all` keysOfBoth
where
typeCheckKey k = getValue k d `typeCheck` get k e
typeCheckKey k = getValue k (toHashMapText d) `typeCheck` get k e
getValue :: Text -> HashMap Text Value -> Value
getValue = Map.lookupDefault Null
keysOfBoth :: [Text]
keysOfBoth = Set.toList $ Set.fromList (Map.keys d) `Set.union` keys e
keysOfBoth = Set.toList $ Set.fromList (Map.keys $ toHashMapText d) `Set.union` keys e
typeCheck _ (TLabel _ ) = error "Cannot typecheck labels without environment!"
typeCheck {-a-} _ _ {-b-} = {-trace msg $-} False
where
Expand Down
8 changes: 6 additions & 2 deletions json-autotype/src/Data/Aeson/AutoType/Pretty.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
Expand All @@ -13,6 +11,8 @@ module Data.Aeson.AutoType.Pretty() where
import qualified Data.HashMap.Strict as Hash
import Data.HashMap.Strict(HashMap)
import Data.Aeson
import Data.Aeson.KeyMap(KeyMap, toHashMapText)
import Data.Aeson.Key
import qualified Data.Text as Text
import Data.Text (Text)
import Data.Set as Set(Set, toList)
Expand All @@ -35,6 +35,10 @@ instance (Out a) => Out (Vector a) where

instance Out Value

instance (Out a) => Out (KeyMap a) where
doc (toHashMapText -> s) = doc s
docPrec _ = doc

instance (Out a) => Out (Set a) where
doc (Set.toList -> s) = "{" <+> doc s <+> "}"
docPrec _ = doc
Expand Down
Loading