| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.Aeson.Utils
Contents
Description
This module provides a few small functions to make working with aeson easier. Hopefully at some point they won't be needed anymore.
- module Data.Aeson
- module Data.Aeson.Types
- decodeV :: FromJSON a => ByteString -> Maybe a
- eitherDecodeV :: FromJSON a => ByteString -> Either String a
- fromFloatDigits :: RealFloat a => a -> Scientific
- (.=?) :: ToJSON a => Text -> Maybe a -> Maybe Pair
- parseNumber :: Scientific -> Either Integer Double
- floatingOrInteger :: (RealFloat r, Integral i) => Scientific -> Either r i
Documentation
module Data.Aeson
module Data.Aeson.Types
Parsing values
decodeV :: FromJSON a => ByteString -> Maybe a #
Deserialize any JSON value. Allows atomic values on the top level
eitherDecodeV :: FromJSON a => ByteString -> Either String a #
Like decodeV, but returns an error message when decoding fails.
Utilities
fromFloatDigits :: RealFloat a => a -> Scientific #
Convert a RealFloat (like a Double or Float) into a Scientific
number.
Note that this function uses floatToDigits to compute the digits
and exponent of the RealFloat number. Be aware that the algorithm used in
floatToDigits doesn't work as expected for some numbers, e.g. as
the Double 1e23 is converted to 9.9999999999999991611392e22, and that
value is shown as 9.999999999999999e22 rather than the shorter 1e23; the
algorithm doesn't take the rounding direction for values exactly half-way
between two adjacent representable values into account, so if you have a
value with a short decimal representation exactly half-way between two
adjacent representable values, like 5^23*2^e for e close to 23, the
algorithm doesn't know in which direction the short decimal representation
would be rounded and computes more digits
parseNumber :: Scientific -> Either Integer Double #
Deprecated: Use Data.Scientific.floatingOrInteger instead
Convert a Scientific into an Integer if it doesn't have decimal points, otherwise to a Double.
floatingOrInteger :: (RealFloat r, Integral i) => Scientific -> Either r i #
floatingOrInteger determines if the scientific is floating point or
integer.
In case it's floating-point the scientific is converted to the desired
RealFloat using toRealFloat and wrapped in Left.
In case it's integer to scientific is converted to the desired Integral and
wrapped in Right.
WARNING: To convert the scientific to an integral the magnitude 10^e
needs to be computed. If applied to a huge exponent this could take a long
time. Even worse, when the destination type is unbounded (i.e. Integer) it
could fill up all space and crash your program! So don't apply this function
to untrusted input but use toBoundedInteger instead.
Also see: isFloating or isInteger.