Copyright | (c) Dong Han 2019 |
---|---|
License | BSD |
Maintainer | winterland1989@gmail.com |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
This module provides definition and parsers for JSON Value
s, a Haskell JSON representation. The parsers is designed to comply with rfc8258, notable pitfalls are:
- The numeric representation use
Scientific
, which impose a limit on number's exponent part(limited toInt
). - Unescaped control characters(<=0x1F) are NOT accepted, (different from aeson).
- Only
0x20, 0x09, 0x0A, 0x0D
are valid JSON whitespaces,skipSpaces
from this module is different fromskipSpaces
. - A JSON document shouldn't have trailing characters except whitespaces describe above, see
parseValue'
andparseValueChunks'
. - Objects are represented as key-value vectors, key order and duplicated keys are preserved for further processing.
Note that rfc8258 doesn't enforce unique key in objects, it's up to users to decided how to deal with key duplication, e.g. prefer first or last key, see withFlatMap
or withFlatMapR
for example.
There's no lazy parsers here, every pieces of JSON document will be parsed into a normal form Value
. Object
and Array
s payloads are packed into Vector
s to avoid accumulating lists in memory. Read more about why no lazy parsing is needed.
Synopsis
- data Value
- key :: Functor f => Text -> (Value -> f Value) -> Value -> f Value
- nth :: Functor f => Int -> (Value -> f Value) -> Value -> f Value
- parseValue :: Bytes -> (Bytes, Either ParseError Value)
- parseValue' :: Bytes -> Either ParseError Value
- parseValueChunks :: Monad m => ParseChunks m Bytes ParseError Value
- value :: Parser Value
- object :: Parser (Vector (Text, Value))
- array :: Parser (Vector Value)
- string :: Parser Text
- skipSpaces :: Parser ()
- floatToScientific :: Float -> Scientific
- doubleToScientific :: Double -> Scientific
Value type
A JSON value represented as a Haskell value.
The Object'
s payload is a key-value vector instead of a map, which parsed
directly from JSON document. This design choice has following advantages:
- Allow different strategies handling duplicated keys.
- Allow different
Map
type to do further parsing, e.g.FlatMap
- Roundtrip without touching the original key-value order.
- Save time if constructing map is not neccessary, e.g. using a linear scan to find a key if only that key is needed.
Object !(Vector (Text, Value)) | |
Array !(Vector Value) | |
String !Text | |
Number !Scientific | |
Bool !Bool | |
Null |
Instances
parse into JSON Value
parseValue :: Bytes -> (Bytes, Either ParseError Value) Source #
Parse Value
without consuming trailing bytes.
parseValue' :: Bytes -> Either ParseError Value Source #
Parse Value
, and consume all trailing JSON white spaces, if there're
bytes left, parsing will fail.
parseValueChunks :: Monad m => ParseChunks m Bytes ParseError Value Source #
Increamental parse Value
without consuming trailing bytes.
Value Parsers
skipSpaces :: Parser () Source #
The only valid whitespace in a JSON document is space, newline, carriage pure, and tab.
Convert to Scientific
floatToScientific :: Float -> Scientific Source #
Convert IEEE float to scientific notition.
doubleToScientific :: Double -> Scientific Source #
Convert IEEE double to scientific notition.