Skip to content

Commit c6498c9

Browse files
committed
Add data-fix and strict instances
1 parent 5e1d23a commit c6498c9

File tree

8 files changed

+202
-4
lines changed

8 files changed

+202
-4
lines changed

.travis.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#
99
# For more information, see https://github.com/haskell-CI/haskell-ci
1010
#
11-
# version: 0.10.1
11+
# version: 0.10.2
1212
#
1313
version: ~> 1.0
1414
language: c
@@ -120,6 +120,11 @@ install:
120120
- if [ $HCNUMVER -ge 80200 ] ; then echo 'package aeson-examples' >> cabal.project ; fi
121121
- "if [ $HCNUMVER -ge 80200 ] ; then echo ' ghc-options: -Werror=missing-methods' >> cabal.project ; fi"
122122
- |
123+
echo "packages:" >> cabal.project
124+
echo " https://hackage.haskell.org/package/quickcheck-instances-0.3.24/candidate/quickcheck-instances-0.3.24.tar.gz" >> cabal.project
125+
echo "" >> cabal.project
126+
echo "packages:" >> cabal.project
127+
echo " https://hackage.haskell.org/package/strict-0.4/candidate/strict-0.4.tar.gz" >> cabal.project
123128
- "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(aeson|aeson-examples|attoparsec-iso8601)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
124129
- cat cabal.project || true
125130
- cat cabal.project.local || true
@@ -157,6 +162,11 @@ script:
157162
- if [ $HCNUMVER -ge 80200 ] ; then echo 'package aeson-examples' >> cabal.project ; fi
158163
- "if [ $HCNUMVER -ge 80200 ] ; then echo ' ghc-options: -Werror=missing-methods' >> cabal.project ; fi"
159164
- |
165+
echo "packages:" >> cabal.project
166+
echo " https://hackage.haskell.org/package/quickcheck-instances-0.3.24/candidate/quickcheck-instances-0.3.24.tar.gz" >> cabal.project
167+
echo "" >> cabal.project
168+
echo "packages:" >> cabal.project
169+
echo " https://hackage.haskell.org/package/strict-0.4/candidate/strict-0.4.tar.gz" >> cabal.project
160170
- "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(aeson|aeson-examples|attoparsec-iso8601)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
161171
- cat cabal.project || true
162172
- cat cabal.project.local || true
@@ -186,5 +196,5 @@ script:
186196
- cd $TOP || false
187197
- ${CABAL} v2-build $WITHCOMPILER --project-file=cabal.bench.project all
188198

189-
# REGENDATA ("0.10.1",["--config=cabal.haskell-ci","cabal.project"])
199+
# REGENDATA ("0.10.2",["--config=cabal.haskell-ci","cabal.project"])
190200
# EOF

Data/Aeson/Types/FromJSON.hs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ import qualified Data.Aeson.Parser.Time as Time
123123
import qualified Data.Attoparsec.ByteString.Char8 as A (endOfInput, parseOnly, scientific)
124124
import qualified Data.ByteString.Lazy as L
125125
import qualified Data.DList as DList
126+
import qualified Data.Fix as F
126127
import qualified Data.HashMap.Strict as H
127128
import qualified Data.HashSet as HashSet
128129
import qualified Data.IntMap as IntMap
@@ -133,6 +134,7 @@ import qualified Data.Scientific as Scientific
133134
import qualified Data.Semigroup as Semigroup
134135
import qualified Data.Sequence as Seq
135136
import qualified Data.Set as Set
137+
import qualified Data.Strict as S
136138
import qualified Data.Text as T
137139
import qualified Data.Text.Encoding as T
138140
import qualified Data.Text.Lazy as LT
@@ -2213,6 +2215,70 @@ instance FromJSON a => FromJSON (Semigroup.Option a) where
22132215
parseJSON = parseJSON1
22142216
{-# INLINE parseJSON #-}
22152217

2218+
-------------------------------------------------------------------------------
2219+
-- data-fix
2220+
-------------------------------------------------------------------------------
2221+
2222+
-- | @since 1.5.3.0
2223+
instance FromJSON1 f => FromJSON (F.Fix f) where
2224+
parseJSON = go where go = fmap F.Fix . liftParseJSON go parseJSONList
2225+
2226+
-- | @since 1.5.3.0
2227+
instance (FromJSON1 f, Functor f) => FromJSON (F.Mu f) where
2228+
parseJSON = fmap (F.unfoldMu F.unFix) . parseJSON
2229+
2230+
-- | @since 1.5.3.0
2231+
instance (FromJSON1 f, Functor f) => FromJSON (F.Nu f) where
2232+
parseJSON = fmap (F.unfoldNu F.unFix) . parseJSON
2233+
2234+
-------------------------------------------------------------------------------
2235+
-- strict
2236+
-------------------------------------------------------------------------------
2237+
2238+
-- | @since 1.5.3.0
2239+
instance (FromJSON a, FromJSON b) => FromJSON (S.These a b) where
2240+
parseJSON = fmap S.toStrict . parseJSON
2241+
2242+
-- | @since 1.5.3.0
2243+
instance FromJSON2 S.These where
2244+
liftParseJSON2 pa pas pb pbs = fmap S.toStrict . liftParseJSON2 pa pas pb pbs
2245+
2246+
-- | @since 1.5.3.0
2247+
instance FromJSON a => FromJSON1 (S.These a) where
2248+
liftParseJSON pa pas = fmap S.toStrict . liftParseJSON pa pas
2249+
2250+
-- | @since 1.5.3.0
2251+
instance (FromJSON a, FromJSON b) => FromJSON (S.Pair a b) where
2252+
parseJSON = fmap S.toStrict . parseJSON
2253+
2254+
-- | @since 1.5.3.0
2255+
instance FromJSON2 S.Pair where
2256+
liftParseJSON2 pa pas pb pbs = fmap S.toStrict . liftParseJSON2 pa pas pb pbs
2257+
2258+
-- | @since 1.5.3.0
2259+
instance FromJSON a => FromJSON1 (S.Pair a) where
2260+
liftParseJSON pa pas = fmap S.toStrict . liftParseJSON pa pas
2261+
2262+
-- | @since 1.5.3.0
2263+
instance (FromJSON a, FromJSON b) => FromJSON (S.Either a b) where
2264+
parseJSON = fmap S.toStrict . parseJSON
2265+
2266+
-- | @since 1.5.3.0
2267+
instance FromJSON2 S.Either where
2268+
liftParseJSON2 pa pas pb pbs = fmap S.toStrict . liftParseJSON2 pa pas pb pbs
2269+
2270+
-- | @since 1.5.3.0
2271+
instance FromJSON a => FromJSON1 (S.Either a) where
2272+
liftParseJSON pa pas = fmap S.toStrict . liftParseJSON pa pas
2273+
2274+
-- | @since 1.5.3.0
2275+
instance FromJSON a => FromJSON (S.Maybe a) where
2276+
parseJSON = fmap S.toStrict . parseJSON
2277+
2278+
-- | @since 1.5.3.0
2279+
instance FromJSON1 S.Maybe where
2280+
liftParseJSON pa pas = fmap S.toStrict . liftParseJSON pa pas
2281+
22162282
-------------------------------------------------------------------------------
22172283
-- tagged
22182284
-------------------------------------------------------------------------------

Data/Aeson/Types/ToJSON.hs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ import qualified Data.Aeson.Encoding as E
103103
import qualified Data.Aeson.Encoding.Internal as E (InArray, comma, econcat, retagEncoding)
104104
import qualified Data.ByteString.Lazy as L
105105
import qualified Data.DList as DList
106+
import qualified Data.Fix as F
106107
import qualified Data.HashMap.Strict as H
107108
import qualified Data.HashSet as HashSet
108109
import qualified Data.IntMap as IntMap
@@ -114,6 +115,7 @@ import qualified Data.Scientific as Scientific
114115
import qualified Data.Semigroup as Semigroup
115116
import qualified Data.Sequence as Seq
116117
import qualified Data.Set as Set
118+
import qualified Data.Strict as S
117119
import qualified Data.Text as T
118120
import qualified Data.Text.Encoding as T
119121
import qualified Data.Text.Lazy as LT
@@ -2228,6 +2230,84 @@ instance ToJSON a => ToJSON (Semigroup.Option a) where
22282230
toEncoding = toEncoding1
22292231
{-# INLINE toEncoding #-}
22302232

2233+
-------------------------------------------------------------------------------
2234+
-- data-fix
2235+
-------------------------------------------------------------------------------
2236+
2237+
-- | @since 1.5.3.0
2238+
instance ToJSON1 f => ToJSON (F.Fix f) where
2239+
toJSON = go where go (F.Fix f) = liftToJSON go toJSONList f
2240+
toEncoding = go where go (F.Fix f) = liftToEncoding go toEncodingList f
2241+
2242+
-- | @since 1.5.3.0
2243+
instance (ToJSON1 f, Functor f) => ToJSON (F.Mu f) where
2244+
toJSON = F.foldMu (liftToJSON id (listValue id))
2245+
toEncoding = F.foldMu (liftToEncoding id (listEncoding id))
2246+
2247+
-- | @since 1.5.3.0
2248+
instance (ToJSON1 f, Functor f) => ToJSON (F.Nu f) where
2249+
toJSON = F.foldNu (liftToJSON id (listValue id))
2250+
toEncoding = F.foldNu (liftToEncoding id (listEncoding id))
2251+
2252+
-------------------------------------------------------------------------------
2253+
-- strict
2254+
-------------------------------------------------------------------------------
2255+
2256+
-- | @since 1.5.3.0
2257+
instance (ToJSON a, ToJSON b) => ToJSON (S.These a b) where
2258+
toJSON = toJSON . S.toLazy
2259+
toEncoding = toEncoding . S.toLazy
2260+
2261+
-- | @since 1.5.3.0
2262+
instance ToJSON2 S.These where
2263+
liftToJSON2 toa toas tob tobs = liftToJSON2 toa toas tob tobs . S.toLazy
2264+
liftToEncoding2 toa toas tob tobs = liftToEncoding2 toa toas tob tobs . S.toLazy
2265+
2266+
-- | @since 1.5.3.0
2267+
instance ToJSON a => ToJSON1 (S.These a) where
2268+
liftToJSON toa tos = liftToJSON toa tos . S.toLazy
2269+
liftToEncoding toa tos = liftToEncoding toa tos . S.toLazy
2270+
2271+
-- | @since 1.5.3.0
2272+
instance (ToJSON a, ToJSON b) => ToJSON (S.Pair a b) where
2273+
toJSON = toJSON . S.toLazy
2274+
toEncoding = toEncoding . S.toLazy
2275+
2276+
-- | @since 1.5.3.0
2277+
instance ToJSON2 S.Pair where
2278+
liftToJSON2 toa toas tob tobs = liftToJSON2 toa toas tob tobs . S.toLazy
2279+
liftToEncoding2 toa toas tob tobs = liftToEncoding2 toa toas tob tobs . S.toLazy
2280+
2281+
-- | @since 1.5.3.0
2282+
instance ToJSON a => ToJSON1 (S.Pair a) where
2283+
liftToJSON toa tos = liftToJSON toa tos . S.toLazy
2284+
liftToEncoding toa tos = liftToEncoding toa tos . S.toLazy
2285+
2286+
-- | @since 1.5.3.0
2287+
instance (ToJSON a, ToJSON b) => ToJSON (S.Either a b) where
2288+
toJSON = toJSON . S.toLazy
2289+
toEncoding = toEncoding . S.toLazy
2290+
2291+
-- | @since 1.5.3.0
2292+
instance ToJSON2 S.Either where
2293+
liftToJSON2 toa toas tob tobs = liftToJSON2 toa toas tob tobs . S.toLazy
2294+
liftToEncoding2 toa toas tob tobs = liftToEncoding2 toa toas tob tobs . S.toLazy
2295+
2296+
-- | @since 1.5.3.0
2297+
instance ToJSON a => ToJSON1 (S.Either a) where
2298+
liftToJSON toa tos = liftToJSON toa tos . S.toLazy
2299+
liftToEncoding toa tos = liftToEncoding toa tos . S.toLazy
2300+
2301+
-- | @since 1.5.3.0
2302+
instance ToJSON a => ToJSON (S.Maybe a) where
2303+
toJSON = toJSON . S.toLazy
2304+
toEncoding = toEncoding . S.toLazy
2305+
2306+
-- | @since 1.5.3.0
2307+
instance ToJSON1 S.Maybe where
2308+
liftToJSON toa tos = liftToJSON toa tos . S.toLazy
2309+
liftToEncoding toa tos = liftToEncoding toa tos . S.toLazy
2310+
22312311
-------------------------------------------------------------------------------
22322312
-- tagged
22332313
-------------------------------------------------------------------------------

aeson.cabal

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: aeson
2-
version: 1.5.2.0
2+
version: 1.5.3.0
33
license: BSD3
44
license-file: LICENSE
55
category: Text, Web, JSON
@@ -136,10 +136,12 @@ library
136136
-- Other dependencies
137137
build-depends:
138138
attoparsec >= 0.13.2.2 && < 0.14,
139+
data-fix >= 0.3 && < 0.4,
139140
dlist >= 0.8.0.4 && < 0.9,
140141
hashable >= 1.2.7.0 && < 1.4,
141142
primitive >= 0.7.0.1 && < 0.8,
142143
scientific >= 0.3.6.2 && < 0.4,
144+
strict >= 0.4 && < 0.5,
143145
tagged >= 0.8.6 && < 0.9,
144146
th-abstraction >= 0.2.8.0 && < 0.4,
145147
these >= 1.1 && < 1.2,
@@ -210,6 +212,7 @@ test-suite aeson-tests
210212
base-orphans >= 0.5.3 && <0.9,
211213
base16-bytestring,
212214
containers,
215+
data-fix,
213216
directory,
214217
dlist,
215218
Diff >= 0.4 && < 0.5,
@@ -218,6 +221,7 @@ test-suite aeson-tests
218221
ghc-prim >= 0.2,
219222
hashable >= 1.2.4.0,
220223
scientific,
224+
strict,
221225
tagged,
222226
template-haskell,
223227
tasty,
@@ -231,7 +235,7 @@ test-suite aeson-tests
231235
unordered-containers,
232236
uuid-types,
233237
vector,
234-
quickcheck-instances >= 0.3.23 && <0.4
238+
quickcheck-instances >= 0.3.24 && <0.4
235239

236240
if flag(bytestring-builder)
237241
build-depends: bytestring >= 0.9 && < 0.10.4,

cabal.project

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ packages: .
33
packages: attoparsec-iso8601
44
packages: examples
55
tests: true
6+
7+
packages: https://hackage.haskell.org/package/quickcheck-instances-0.3.24/candidate/quickcheck-instances-0.3.24.tar.gz
8+
packages: https://hackage.haskell.org/package/strict-0.4/candidate/strict-0.4.tar.gz

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
For the latest version of this document, please see [https://github.com/bos/aeson/blob/master/changelog.md](https://github.com/bos/aeson/blob/master/changelog.md).
22

3+
### 1.5.3.0
4+
5+
* Add instances for types in `strict` and `data-fix` packages.
6+
37
### 1.5.2.0
48

59
* Add `Ord Value` instance, thanks to Oleg Grenrus.

tests/PropertyRoundTrip.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import Types
2626
import qualified Data.Text as T
2727
import qualified Data.Text.Lazy as LT
2828
import qualified Data.UUID.Types as UUID
29+
import qualified Data.Strict as S
30+
import qualified Data.Fix as F
2931
import PropUtils
3032
import PropertyRTFunctors
3133

@@ -66,6 +68,13 @@ roundTripTests =
6668
, testProperty "Ratio Int" $ roundTripEq (undefined :: Ratio Int)
6769
, testProperty "UUID" $ roundTripEq UUID.nil
6870
, testProperty "These" $ roundTripEq (These 'x' True)
71+
, testProperty "Fix" $ roundTripEq (undefined :: F.Fix (These Char))
72+
, testProperty "Mu" $ roundTripEq (undefined :: F.Mu (These Char))
73+
, testProperty "Nu" $ roundTripEq (undefined :: F.Nu (These Char))
74+
, testProperty "Strict Pair" $ roundTripEq (undefined :: S.Pair Int Char)
75+
, testProperty "Strict Either" $ roundTripEq (undefined :: S.Either Int Char)
76+
, testProperty "Strict These" $ roundTripEq (undefined :: S.These Int Char)
77+
, testProperty "Strict Maybe" $ roundTripEq (undefined :: S.Maybe Int)
6978
, roundTripFunctorsTests
7079
, testGroup "ghcGenerics" [
7180
testProperty "OneConstructor" $ roundTripEq OneConstructor

tests/SerializationFormatSpec.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ import qualified Data.Set as Set
5454
import qualified Data.Tree as Tree
5555
import qualified Data.UUID.Types as UUID
5656
import qualified Data.Vector as Vector
57+
import qualified Data.Fix as F
58+
import qualified Data.Strict as S
5759

5860
tests :: [TestTree]
5961
tests =
@@ -218,6 +220,26 @@ jsonExamples =
218220
, "{\"That\":false,\"This\":\"y\"}"
219221
]
220222
(These 'y' False)
223+
224+
-- data-fix and strict
225+
, ndExample "Fix Strict.These"
226+
[ "{\"This\":true,\"That\":{\"That\":{\"This\":false}}}"
227+
, "{\"That\":{\"That\":{\"This\":false}},\"This\":true}"
228+
]
229+
(F.Fix (S.These True (F.Fix (S.That (F.Fix (S.This False))))))
230+
231+
-- Mu and Nu are similar.
232+
, ndExample "Mu Strict.These"
233+
[ "{\"This\":true,\"That\":{\"That\":{\"This\":false}}}"
234+
, "{\"That\":{\"That\":{\"This\":false}},\"This\":true}"
235+
]
236+
$ F.unfoldMu F.unFix $ F.Fix (S.These True (F.Fix (S.That (F.Fix (S.This False)))))
237+
238+
, ndExample "Nu Strict.These"
239+
[ "{\"This\":true,\"That\":{\"That\":{\"This\":false}}}"
240+
, "{\"That\":{\"That\":{\"This\":false}},\"This\":true}"
241+
]
242+
$ F.unfoldNu F.unFix $ F.Fix (S.These True (F.Fix (S.That (F.Fix (S.This False)))))
221243
]
222244

223245
jsonEncodingExamples :: [Example]

0 commit comments

Comments
 (0)