Copyright | (c) 2011-2016 Bryan O'Sullivan (c) 2011 MailRank Inc. |
---|---|
License | BSD3 |
Maintainer | Bryan O'Sullivan <bos@serpentine.com> |
Stability | experimental |
Portability | portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Efficiently and correctly parse a JSON string. The string must be encoded as UTF-8.
Synopsis
- json :: Parser Value
- jsonEOF :: Parser Value
- jsonWith :: ([(Key, Value)] -> Either String Object) -> Parser Value
- jsonLast :: Parser Value
- jsonAccum :: Parser Value
- jsonNoDup :: Parser Value
- value :: Parser Value
- jstring :: Parser Text
- jstring_ :: Parser Text
- scientific :: Parser Scientific
- json' :: Parser Value
- jsonEOF' :: Parser Value
- jsonWith' :: ([(Key, Value)] -> Either String Object) -> Parser Value
- jsonLast' :: Parser Value
- jsonAccum' :: Parser Value
- jsonNoDup' :: Parser Value
- value' :: Parser Value
- decodeWith :: Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
- decodeStrictWith :: Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
- eitherDecodeWith :: Parser Value -> (Value -> IResult a) -> ByteString -> Either (JSONPath, String) a
- eitherDecodeStrictWith :: Parser Value -> (Value -> IResult a) -> ByteString -> Either (JSONPath, String) a
- fromListAccum :: [(Key, Value)] -> Object
- parseListNoDup :: [(Key, Value)] -> Either String Object
- unescapeText :: ByteString -> Either UnicodeException Text
Lazy parsers
Parse any JSON value.
The conversion of a parsed value to a Haskell value is deferred until the Haskell value is needed. This may improve performance if only a subset of the results of conversions are needed, but at a cost in thunk allocation.
This function is an alias for value
. In aeson 0.8 and earlier, it
parsed only object or array types, in conformance with the
now-obsolete RFC 4627.
Warning
If an object contains duplicate keys, only the first one will be kept.
For a more flexible alternative, see jsonWith
.
jsonEOF :: Parser Value Source #
Parse a top-level JSON value followed by optional whitespace and
end-of-input. See also: json
.
jsonWith :: ([(Key, Value)] -> Either String Object) -> Parser Value Source #
Parse any JSON value.
This parser is parameterized by a function to construct an Value
from a raw list of key-value pairs, where duplicates are preserved.
The pairs appear in reverse order from the source.
Examples
json
keeps only the first occurrence of each key, using fromList
.
json
=jsonWith
(Right
.
fromList
)
jsonLast
keeps the last occurrence of each key, using
.fromListWith
(const
id
)
jsonLast
=jsonWith
(Right
.
fromListWith
(const
id
))
jsonAccum
keeps wraps all values in arrays to keep duplicates, using
fromListAccum
.
jsonAccum
=jsonWith
(Right
.fromListAccum
)
jsonNoDup
fails if any object contains duplicate keys, using parseListNoDup
.
jsonNoDup
=jsonWith
parseListNoDup
jsonNoDup :: Parser Value Source #
Variant of json
which fails if any object contains duplicate keys.
scientific :: Parser Scientific Source #
Parse a JSON number.
Strict parsers
json' :: Parser Value Source #
Parse any JSON value.
This is a strict version of json
which avoids building up thunks
during parsing; it performs all conversions immediately. Prefer
this version if most of the JSON data needs to be accessed.
This function is an alias for value'
. In aeson 0.8 and earlier, it
parsed only object or array types, in conformance with the
now-obsolete RFC 4627.
Warning
If an object contains duplicate keys, only the first one will be kept.
For a more flexible alternative, see jsonWith'
.
jsonEOF' :: Parser Value Source #
Parse a top-level JSON value followed by optional whitespace and
end-of-input. See also: json'
.
jsonWith' :: ([(Key, Value)] -> Either String Object) -> Parser Value Source #
Strict version of jsonWith
.
jsonLast' :: Parser Value Source #
Variant of json'
which keeps only the last occurrence of every key.
jsonAccum' :: Parser Value Source #
jsonNoDup' :: Parser Value Source #
Variant of json'
which fails if any object contains duplicate keys.
Helpers
decodeWith :: Parser Value -> (Value -> Result a) -> ByteString -> Maybe a Source #
decodeStrictWith :: Parser Value -> (Value -> Result a) -> ByteString -> Maybe a Source #
eitherDecodeWith :: Parser Value -> (Value -> IResult a) -> ByteString -> Either (JSONPath, String) a Source #
eitherDecodeStrictWith :: Parser Value -> (Value -> IResult a) -> ByteString -> Either (JSONPath, String) a Source #
Handling objects with duplicate keys
fromListAccum :: [(Key, Value)] -> Object Source #
is an object mapping keys to arrays containing all
associated values from the original list fromListAccum
kvskvs
.
>>>
fromListAccum [("apple", Bool True), ("apple", Bool False), ("orange", Bool False)]
fromList [("apple",Array [Bool False,Bool True]),("orange",Array [Bool False])]
parseListNoDup :: [(Key, Value)] -> Either String Object Source #
fails if fromListNoDup
kvskvs
contains duplicate keys.
Text literal unescaping
unescapeText :: ByteString -> Either UnicodeException Text #
Unescape JSON text literal.
This function is exporeted mostly for testing and benchmarking purposes.