Skip to content

Commit 352b9ce

Browse files
authored
Merge pull request #1039 from haskell/optional-fields
Optional fields
2 parents 5a7767c + b06eb2c commit 352b9ce

24 files changed

+1802
-675
lines changed

aeson.cabal

+7
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ test-suite aeson-tests
165165
PropUtils
166166
Regression.Issue351
167167
Regression.Issue571
168+
Regression.Issue687
168169
Regression.Issue967
169170
SerializationFormatSpec
170171
Types
@@ -174,6 +175,12 @@ test-suite aeson-tests
174175
UnitTests.KeyMapInsertWith
175176
UnitTests.MonadFix
176177
UnitTests.NullaryConstructors
178+
UnitTests.OmitNothingFieldsNote
179+
UnitTests.OptionalFields
180+
UnitTests.OptionalFields.Common
181+
UnitTests.OptionalFields.Generics
182+
UnitTests.OptionalFields.Manual
183+
UnitTests.OptionalFields.TH
177184
UnitTests.UTCTime
178185

179186
build-depends:

changelog.md

+31-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
For the latest version of this document, please see [https://github.com/haskell/aeson/blob/master/changelog.md](https://github.com/haskell/aeson/blob/master/changelog.md).
22

3-
### 2.2
4-
5-
* Use `Data.Aeson.Decoding` parsing functions as default in `Data.Aeson`.
6-
* Move `Data.Aeson.Parser` module into separate `attoparsec-aeson` package, as these parsers are not used by `aeson` itself anymore.
7-
* Remove `cffi` flag. Then the C implementation for string unescaping was used for `text <2` versions.
8-
The new native Haskell implementation introduced in version 2.0.3.0 is at least as fast.
9-
* Drop instances for `attoparsec.Number`.
3+
### 2.2.0.0
4+
5+
* Rework how `omitNothingFields` works. Add `allowOmittedFields` as a parsing counterpart.
6+
7+
New type-class members were added: `omitField :: a -> Bool` to `ToJSON` and `omittedField :: Maybe a` to `FromJSON`.
8+
These control which fields can be omitted.
9+
The `.:?=`, `.:!=` and `.?=` operators were added to make use of these new members.
10+
11+
GHC.Generics and Template Haskell deriving has been updated accordingly.
12+
Note: They behave as the parsers have been written with `.:!=`, i.e.
13+
if the field value is `null` it's passed to the underlying parser.
14+
This doesn't make difference for `Maybe` or `Option`, but does make for
15+
types which parser doesn't accept `null`.
16+
(`()` parser accepts everything and `Proxy` accepts `null).
17+
18+
In addition to `Maybe` (and `Option`) fields the `Data.Monoid.First` and `Data.Monoid.Last` are also omitted,
19+
as well as the most newtype wrappers, when their wrap omittable type (e.g. newtypes in `Data.Monoid` and `Data.Semigroup`, `Identity`, `Const`, `Tagged`, `Compose`).
20+
Additionall "boring" types like `()` and `Proxy` are omitted as well.
21+
As the omitting is now uniform, type arguments are also omitted (also in `Generic1` derived instance).
22+
23+
Resolves issues
24+
[#687](https://github.com/haskell/aeson/issues/687),
25+
[#571](https://github.com/haskell/aeson/issues/571),
26+
[#792](https://github.com/haskell/aeson/issues/792).
27+
28+
* Use `Data.Aeson.Decoding` parsing functions (introduced in version 2.1.2.0) as default in `Data.Aeson`.
29+
* Move `Data.Aeson.Parser` module into separate [`attoparsec-aeson`](https://hackage.haskell.org/package/attoparsec-aeson) package, as these parsers are not used by `aeson` itself anymore.
30+
* Use [`text-iso8601`](https://hackage.haskell.org/package/text-iso8601) package for parsing `time` types. These are slightly faster than previously used (copy of) `attoparsec-iso8601`.
31+
* Remove `cffi` flag. Toggling the flag made `aeson` use a C implementation for string unescaping (used for `text <2` versions).
32+
The new native Haskell implementation (introduced in version 2.0.3.0) is at least as fast.
33+
* Drop instances for `Number` from `attoparsec` package.
1034
* Improve `Arbitrary Value` instance.
1135

1236
### 2.1.2.1

src/Data/Aeson.hs

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ module Data.Aeson
7676
, fromJSON
7777
, ToJSON(..)
7878
, KeyValue(..)
79+
, KeyValueOmit(..)
7980
, (<?>)
8081
, JSONPath
8182
-- ** Keys for maps
@@ -91,14 +92,18 @@ module Data.Aeson
9192
-- ** Liftings to unary and binary type constructors
9293
, FromJSON1(..)
9394
, parseJSON1
95+
, omittedField1
9496
, FromJSON2(..)
9597
, parseJSON2
98+
, omittedField2
9699
, ToJSON1(..)
97100
, toJSON1
98101
, toEncoding1
102+
, omitField1
99103
, ToJSON2(..)
100104
, toJSON2
101105
, toEncoding2
106+
, omitField2
102107
-- ** Generic JSON classes and options
103108
, GFromJSON
104109
, FromArgs
@@ -123,6 +128,7 @@ module Data.Aeson
123128
, constructorTagModifier
124129
, allNullaryToStringTag
125130
, omitNothingFields
131+
, allowOmittedFields
126132
, sumEncoding
127133
, unwrapUnaryRecords
128134
, tagSingleConstructors
@@ -151,6 +157,8 @@ module Data.Aeson
151157
, (.:?)
152158
, (.:!)
153159
, (.!=)
160+
, (.:?=)
161+
, (.:!=)
154162
, object
155163
-- * Parsing
156164
, parseIndexedJSON

src/Data/Aeson/Encoding/Internal.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ instance Ord (Encoding' a) where
115115
compare (Encoding a) (Encoding b) =
116116
compare (toLazyByteString a) (toLazyByteString b)
117117

118-
-- | @since 2.2
118+
-- | @since 2.2.0.0
119119
instance IsString (Encoding' a) where
120120
fromString = string
121121

0 commit comments

Comments
 (0)