Welcome to aeson

Hackage Build Status

aeson is a fast Haskell library for working with JSON data.

Join in!

We are happy to receive bug reports, fixes, documentation enhancements, and other improvements.

Please report bugs via the github issue tracker.

Master git repository:

  • git clone git://github.com/haskell/aeson.git

See what’s changed in recent (and upcoming) releases:

(You can create and contribute changes using either git or Mercurial.)

Authors

This library was originally written by Bryan O’Sullivan.

Changes

For the latest version of this document, please see http://github.com/haskell/aeson/blob/master/changelog.md.

2.2.3.0

  • Support hashable-1.4.6.0.
  • Fix an issue where Hashable Key wasn’t newtype instance over underlying Text, so with -ordered-keymap there were correctness issues.
  • Add instances for Data.Semigroup.Sum, Product, Any, All

2.2.2.0

  • Support GHC-8.6.5…9.10.1
  • Depend on character-ps instead of defining own Word8 pattern synonyms

2.2.1.0

  • Add Data.Aeson.RFC8785, a JSON Canonicalization Scheme implementation http://datatracker.ietf.org/doc/html/rfc8785

  • Add Data.Aeson.Decoding.Text, decodeStrictText :: Text -> …

    We avoid intermediate ByteString copy by not doing decode . TE.encodeUtf8, but instead working on Text value directly. As we know that the stream is valid Unicode (UTF8 or UTF16), we can also take some shortcuts.

    One gotcha is that internal Text values (in Keys or Value Strings) will most likely retain the original input Text value (its underlying Array). It shouldn’t be an issue if the Value is then decoded to something else so these Text values disapper, but if not (e.g. Object keys survive) then users might want to use Data.Text.copy.

2.2.0.0

  • Rework how omitNothingFields works. Add allowOmittedFields as a parsing counterpart.

    New type-class members were added: omitField :: a -> Bool to ToJSON and omittedField :: Maybe a to FromJSON. These control which fields can be omitted. The .:?=, .:!= and .?= operators were added to make use of these new members.

    GHC.Generics and Template Haskell deriving has been updated accordingly. Note: They behave as the parsers have been written with .:!=, i.e. if the field value is null it’s passed to the underlying parser. This doesn’t make difference for Maybe or Option, but does make for types which parser doesn’t accept null. (() parser accepts everything and Proxy accepts `null).

    In addition to Maybe (and Option) fields the Data.Monoid.First and Data.Monoid.Last are also omitted, 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). Additionall “boring” types like () and Proxy are omitted as well. As the omitting is now uniform, type arguments are also omitted (also in Generic1 derived instance).

    Resolves issues:

    • #687 Derived ToJSON1 instance does not respect omitNothingFields = True,
    • #571 omitNothingFields not used in Generic Decode,
    • #792 Make Proxy fields optional.
  • Use Data.Aeson.Decoding parsing functions (introduced in version 2.1.2.0) as default in Data.Aeson. As one side-effect, decode and decode' etc pair functions are operationally the same. All variants use an intermediate Value in normal form.

    The lazier variant could had Value thunks inside Array (i.e. Vector), but the record had been value strict since version 0.4.0.0 (before that the lazy Data.Map was used as Object).

  • Move Data.Aeson.Parser module into separate attoparsec-aeson package, as these parsers are not used by aeson itself anymore.

  • Use text-iso8601 package for parsing time types. These are slightly faster than previously used (copy of) attoparsec-iso8601. Formats accepted is slightly changed:

    • The space between time and timezone offset (in UTCTime and ZonedTime) is disallowed. ISO8601 explictly forbidds it.
    • The timezone offsets can be in range -23:59..23:59. This is how Python, joda-time etc seems to do. (Previously the range was -12..+14)
  • Remove internal Data.Aeson.Internal and Data.Aeson.Internal.Time modules. Everything from the former is exported elsewhere (Data.Aeson.Types), the latter was truly internal.

  • Remove cffi flag. Toggling the flag made aeson use a C implementation for string unescaping (used for text <2 versions). The new native Haskell implementation (introduced in version 2.0.3.0) is at least as fast.

  • Drop instances for Number from attoparsec package.

  • Improve Arbitrary Value instance.

  • Add instances for URI from network-uri.

  • add instances for Down from Data.Ord.

  • Use integer-conversion for converting Text and ByteStrings into Integers.

  • Bump lower bounds of non GHC-boot lib dependencies.

2.1.2.1

  • Support th-abstraction-0.5

2.1.2.0

  • Add throwDecode :: (MonadThrow m, FromJSON a) => ByteString -> m a and variants.
  • Add Data.Aeson.Decoding which uses new underlying tokenizer / parser. This parser seems to be faster, and the intermediate Tokens streams allow to differentiate more than Value if needed. If no critical issues is found, this parser will become the default in next major aeson version.
  • Support deriving for empty datatypes (such as Void and V1) in FromJSON and ToJSON.
  • Add To/FromJSONKey Void instances
  • Fix FromJSONKey Double handling of infinities

2.1.1.0

  • Add Data.Aeson.KeyMap.!? (flipped) alias to Data.Aeson.KeyMap.lookup.
  • Add Data.Aeson.KeyMap.insertWith function.
  • Use unsafeDupablePerformIO instead of incorrect accursedUnutterablePerformIO in creation of keys in TH serialisation. This fixes a bug in TH deriving, e.g. when Strict pragma was enabled.

2.1.0.0

  • Change time instances of types with year (Day, UTCTime) to require years with at least 4 digits.
  • Change KeyValue instances to be more general (and use equality to constraint them) instead of being more lax flexible instances.
  • Export Key type also from Data.Aeson.KeyMap module.
  • Export mapWithKey from Data.Aeson.KeyMap module.
  • Export ifromJSON and iparse from Data.Aeson.Types. Add iparseEither.
  • Add MonadFix Parser instance.
  • Make Semigroup Series slightly lazier
  • Add instances for Generically type

2.0.3.0

  • text-2.0 support
  • bytestring-0.11.2.0 support
  • Rewrite pure text literal unescaper.
  • Add QuickCheck’s Arbitrary, CoArbitrary and Function instances

2.0.2.0

  • Add IsList (KeyMap v) instance.
  • Add toMapText and fromMapText to Data.Aeson.KeyMap.
  • Add ShortText instances
  • Add Solo instances

2.0.1.0

  • Add FromJSON KeyMap instance.
  • Make ordered-keymap on by default.

2.0.0.0

  • Remove forced -O2 and then unneeded fast flag. Also remove most of INLINE pragmas. In the effect, aeson compiles almost twice as fast.

    To get fast compilation effect cabal-install users may specify optimization: False.

  • Make map type used by Object abstract so the underlying implementation can be modified, thanks to Callan McGill

  • Add ordered-keymap flag allowing to change the underlying implementation of object KeyMap.

  • Drop GHC-7 support

  • Remove Data.Aeson.Encode module

  • Double and Float infinities are encoded as "+inf" and "-inf". Change To/FromJSONKey instances to use "+inf" and "-inf" too.

  • FromJSON () and FromJSON (Proxy tag) accept any JSON value.

1.5.6.0

  • Make Show Value instance print object keys in lexicographic order.

1.5.5.1

  • Fix a bug in FromJSON QuarterOfYear instance.

1.5.5.0

  • Add instances for Month, Quarter and QuarterOfYear (from time-1.11), thanks to Oleg Grenrus.

  • The aeson repository has been moved to the haskell github organization!

1.5.4.1

  • Use Text.Encoding.decodeLatin1 to speed up ASCII string decoding, thanks to Dmitry Ivanov.
  • Support bytestring 0.11.* and th-abstraction 0.4.*, thanks to Oleg Grenrus.

1.5.4.0

  • Add instances for ToJSONKey and FromJSONKey to Const, thanks to Dan Fithian.
  • Add support for template-haskell 2.17, thanks to Galen Huntington.
  • Documentation typo fix, thanks to Jean-Baptiste Mazon.

1.5.3.0

  • Add instances for types in strict and data-fix packages, thanks to Oleg Grenrus.
  • CPP cleanup, thanks to Oleg Grenrus.
  • Instances for dlist’s Data.DList.DNonEmpty.DNonEmpty, thanks to Oleg Grenrus.

1.5.2.0

  • Add Ord Value instance, thanks to Oleg Grenrus.
  • Export rejectUnknownFields from Data.Aeson

1.5.1.0

  • Add instances for these, thanks to Oleg Grenrus.

1.5.0.0

  • Fix bug in rejectUnknownFields not respecting fieldLabelModifier, thanks to Markus Schirp.
  • GFromJSON members are no longer exported from Data.Aeson(.Types), if you are using gParseJSON consider switching to gParseJSON', thanks to Oleg Grenrus.
  • Aeson no longer accepts unescaped control characters, thanks to Oleg Grenrus.
  • Remove CoerceText since GHC >=7.8 has Coercible, thanks to Oleg Grenrus.
  • Rename the GToJSON class to GToJSON' and expose it, thanks to Oleg Grenrus.

Closed tickets: http://github.com/bos/aeson/milestone/21

1.4.7.1

  • GHC 8.10 compatibility, thanks to Ryan Scott.

1.4.7.0

Long overdue release (once again), so there’s quite a bit of stuff included even though it’s a “minor” release. Big thanks to all the contributors, the project would not exist without you!

Special thanks to Oleg Grenrus and Xia Li-Yao for reviewing tons of stuff.

New stuff:

  • Add rejectUnknownFields to Options which rejects unknown fields on deserialization. Useful to find errors during development, but enabling this should be considered a breaking change as previously accepted inputs may now be rejected. Thanks to rmanne.
instance FromJSON Foo where
  parseJSON = gParseJSON defaultOptions { rejectUnknownFields = True }
  • FromJSON instance of Ratio a now parses numbers in addtion to standard {numerator=..., denumerator=...} encoding. Thanks to Aleksey Khudyakov.

  • Add more information to parse errors, including a sample of the surrounding text. Hopefully this will lead to less “Failed to read: satisfy” confusion! Thanks to Sasha Bogicevic. We expect some downstream test suites to break because of this, apologies in advance. Hopefully you will like the improvement anyway :-)

  • Add parseFail to Data.Aeson.Types. parseFail = fail but doesn’t require users to know about MonadFail. Thanks to Colin Woodbury.

  • Make Template Haskell type family detection smarter when deriving ToJSON1 instances, thanks to Ryan Scott.

  • Optimize string parsing for the common case of strings without escapes, thanks to Yuras.

Misc:

  • Clean up compiler warnings and switch from base-compat to base-compat-batteries. Thanks to Colin Woodbury & Oleg Grenrus.

  • Clarification & fixes to documentation regarding treatment of Maybe fields, thanks to Roman Cheplyaka.

  • Add documentation for internal development workflows. Thanks to Guru Devanla.

  • Drop support for GHC < 7.8. We’ve chosen to support older GHCs as long as it doesn’t prevent us from adding new features, but now it does! Thanks to Oleg Grenrus for the patch.

  • Allow generic-deriving 1.13 in test suite.

  • Some DRY fixes thanks to Mark Fajkus.

1.4.6.0

  • Provide a clearer error message when a required tagKey for a constructor is missing, thanks to Guru Devanla. The error message now looks like this: Error in $: parsing Types.SomeType failed, expected Object with key "tag" containing one of ["nullary","unary","product","record","list"], key "tag" not found

  • Add formatPath and formatRelativePath functions to turn a JSONPath into a String, thanks to Robbie McMichael

1.4.5.0

  • Expose (<?>), JSONPath and JSONPathElement(..) from Data.Aeson.Types. Previously only available through internal modules. Thanks to Luke Clifton.

  • Support for base-compat 0.11, thanks to Ryan Scott.

  • Travis build for GHC 8.8, thanks to Oleg Grenrus.

1.4.4.0

New features:

  • Adds a parameterized parser jsonWith that can be used to choose how to handle duplicate keys in objects, thanks to Xia Li-Yao.

  • Add generic implementations of FromJSONKey and ToJSONKey, thanks to Xia Li-Yao. Example:

data Foo = Bar
  deriving Generic

opts :: JSONKeyOptions
opts = defaultJSONKeyOptions { keyModifier = toLower }

instance ToJSONKey Foo where
  toJSONKey = genericToJSONKey opts

instance FromJSONKey Foo where
  fromJSONKey = genericFromJSONKey opts

Minor:

  • aeson now uses time-compat instead of time-locale-compat, thanks to Oleg Grenrus.
  • Prepare for MonadFail breakages in GHC 8.8, thanks to Oleg Grenrus.
  • Require bytestring >= 0.10.8.1 for newer GHCs to avoid build failures, thanks to Oleg Grenrus.
  • Support primitive 0.7.*, thanks to Adam Bergmark.
  • Allow semigroups 0.19.* and hashable 1.3.*, thanks to Oleg Grenrus.
  • Fix a typo in the error message when parsing NonEmpty, thanks to Colin Woodbury.
  • Document surprising behavior when using omitNothingFields with type variables, thanks to Xia Li-Yao.

Internal changes:

  • Code cleanup by Oleg Grenrus
  • Fix dependencies of the benchmarks on older GHC’s, thanks to Xia Li-Yao.

1.4.3.0

  • Improve error messages for FromJSON in existing instances and GHC Generic implementation. Thanks to Xia Li-Yao & Igor Pashev.
  • Tweak error-reporting combinators and their documentation. Thanks to Xia Li-Yao.
    • typeMismatch is now about comparing JSON types (i.e., the expected and actual names of the Value constructor).
    • withObject and other with* combinators now also mention the JSON types they expect
    • New unexpected and prependFailure combinators.
  • Add Contravariant ToJSONKeyFunction instance. Thanks to Oleg Grenrus.
  • Add KeyValue instance for Object. Thanks to Robert Hensing.
  • Improve performance when parsing certain large numbers, thanks to Oleg Grenrus.
  • Add Data.Aeson.QQ.Simple - A limited version of aeson-qq. Thanks to Oleg Grenrus.
  • Exposes internal helper functions like <?>, JSONPath, and parseIndexedJSON from Data.Aeson module. Thanks to Abid Uzair.
  • Better error messages when there are syntax errors parsing objects and arrays. Thanks to Fintan Halpenny.
  • Support building with th-abstraction-0.3.0.0 or later. Thanks to Ryan Scott.

1.4.2.0

  • Add Data.Aeson.QQ.Simple which is a simpler version of the aeson-qq package, it does not support interpolation, thanks to Oleg Grenrus.
  • Add Contravariant ToJSONKeyFunction instance, thanks to Oleg Grenrus.
  • Add KeyValue Object instance, thanks to Robert Hensing
  • Improved performance when parsing large numbers, thanks to Oleg Grenrus.

1.4.1.0

  • Optimizations of generics, thanks to Rémy Oudompheng, here are some numbers for GHC 8.4:
    • Compilation time: G/BigProduct.hs is 25% faster, G/BigRecord.hs is 2x faster.
    • Runtime performance: BigRecord/toJSON/generic and BigProduct/encode/generic are more than 2x faster.
  • Added To/FromJSON instances for Void and Generics’s V1, thanks to Will Yager
  • Added To/FromJSON instances for primitive’s Array, SmallArray, PrimArray and UnliftedArray, thanks to Andrew Thad.
  • Fixes handling of UTCTime wrt. leap seconds , thanks to Adam Schønemann
  • Warning and documentation fixes thanks to tom-bop, Gabor Greif, Ian Jeffries, and Mateusz Curyło.

1.4.0.0

This release introduces bounds on the size of Scientific numbers when they are converted to other arbitrary precision types that do not represent them efficiently in memory.

This means that trying to decode a number such as 1e1000000000 into an Integer will now fail instead of using a lot of memory. If you need to represent large numbers you can add a newtype (preferably over Scientific) and providing a parser using withScientific.

The following instances are affected by this:

  • FromJSON Natural
  • FromJSONKey Natural
  • FromJSON Integer
  • FromJSONKey Integer
  • FromJSON NominalDiffTime

For the same reasons the following instances & functions have been removed:

  • Remove FromJSON Data.Attoparsec.Number instance. Note that Data.Attoparsec.Number is deprecated.
  • Remove deprecated withNumber, use withScientific instead.

Finally, encoding integral values with large exponents now uses scientific notation, this saves space for large numbers.

1.3.1.1

  • Catch 0 denominators when parsing Ratio

1.3.1.0

  • Fix bug in generically derived FromJSON instances that are using unwrapUnaryRecords, thanks to Xia Li-yao
  • Allow base-compat 0.10.*, thanks to Oleg Grenrus

1.3.0.0

Breaking changes:

  • GKeyValue has been renamed to KeyValuePair, thanks to Xia Li-yao
  • Removed unused FromJSON constraint in withEmbeddedJson, thanks to Tristan Seligmann

Other improvements:

  • Optimizations of TH toEncoding, thanks to Xia Li-yao
  • Optimizations of hex decoding when using the default/pure unescape implementation, thanks to Xia Li-yao
  • Improved error message on Day parse failures, thanks to Gershom Bazerman
  • Add encodeFile as well as decodeFile* variants, thanks to Markus Hauck
  • Documentation fixes, thanks to Lennart Spitzner
  • CPP cleanup, thanks to Ryan Scott

1.2.4.0

  • Add Ord instance for JSONPathElement, thanks to Simon Hengel.

1.2.3.0

  • Added withEmbeddedJSON to help parse JSON embedded inside a JSON string, thanks to Jesse Kempf.
  • Memory usage improvements to the default (pure) parser, thanks to Jonathan Paugh. Also thanks to Neil Mitchell & Oleg Grenrus for contributing a benchmark.
  • omitNothingFields now works for the Option newtype, thanks to Xia Li-yao.
  • Some documentation fixes, thanks to Jonathan Paug & Philippe Crama.

1.2.2.0

  • Add FromJSON and ToJSON instances for
    • DiffTime, thanks to Víctor López Juan.
    • CTime, thanks to Daniel Díaz.
  • Fix handling of fractions when parsing Natural, thanks to Yuriy Syrovetskiy.
  • Change text in error messages for Integral types to make them follow the common pattern, thanks to Yuriy Syrovetskiy.
  • Add missing INCOHERENT pragma for RecordToPair, thanks to Xia Li-yao.
  • Everything related to Options is now exported from Data.Aeson, thanks to Xia Li-yao.
  • Optimizations to not escape text in clear cases, thanks to Oleg Grenrus.
  • Some documentation fixes, thanks to Phil de Joux & Xia Li-yao.

1.2.1.0

  • Add parserThrowError and parserCatchError combinators, thanks to Oleg Grenrus.

  • Add Generic instance for Value, thanks to Xia Li-yao.

  • Fix a mistake in the 1.2.0.0 changelog, the cffi flag is disabled by default! Thanks to dbaynard.

1.2.0.0

  • tagSingleConstructors, an option to encode single-constructor types as tagged sums was added to Options. It is disabled by default for backward compatibility.

  • The cffi flag is now turned off (False) by default, this means C FFI code is no longer used by default. You can flip the flag to get C implementation.

  • The Options constructor is no longer exposed to prevent new options from being breaking changes, use defaultOptions instead.

  • The contents of GToJSON and GToEncoding are no longer exposed.

  • Some INLINE pragmas were removed to avoid GHC running out of simplifier ticks.

1.1.2.0

  • Fix an accidental change in the format of deriveJSON. Thanks to Xia Li-yao!

  • Documentation improvements regarding ToJSON, FromJSON, and SumEncoding. Thanks to Xia Li-yao and Lennart Spitzner!

1.1.1.0

  • Added a pure implementation of the C FFI code, the C FFI code. If you wish to use the pure haskell version set the cffi flag to False. This should make aeson compile when C isn’t available, such as for GHCJS. Thanks to James Parker & Marcin Tolysz!

  • Using the fast flag can no longer cause a test case to fail. As far as we know this didn’t affect any users of the library itself. Thanks to Xia Li-yao!

1.1.0.0

  • Added instances for UUID.

  • The operators for parsing fields now have named aliases:

    • .: => parseField
    • .:? => parseFieldMaybe
    • .:! => parseFieldMaybe'
    • These functions now also have variants with explicit parser functions: explicitParseField, explicitParseFieldMaybe, “explicitParseFieldMaybe’` Thanks to Oleg Grenrus.
  • ToJSONKey (Identity a) and FromJSONKey (Identity a) no longer require the unnecessary FromJSON a constraint. Thanks to Oleg Grenrus.

  • Added Data.Aeson.Encoding.pair' which is a more general version of Data.Aeson.Encoding.pair. Thanks to Andrew Martin.

  • Days BCE are properly encoded and + is now a valid prefix for Days CE. Thanks to Matt Parsons.

  • Some commonly used ToJSON instances are now specialized in order to improve compile time. Thanks to Bartosz Nitka.

JSONTestSuite cleanups, all motivated by tighter RFC 7159 compliance:

Over 90% of JSONTestSuite tests currently pass. The remainder can be categorised as follows:

  • The string parser is strict with Unicode compliance where the RFC leaves room for