License | BSD-style |
---|---|
Maintainer | palkovsky.ondrej@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
An incremental applicative-style JSON parser, suitable for high performance memory efficient stream parsing.
The parser is using Data.Aeson types and FromJSON
instance, it can be
easily combined with aeson monadic parsing instances when appropriate.
- data Parser a
- data ParseOutput a
- = ParseYield a (ParseOutput a)
- | ParseNeedData (ByteString -> ParseOutput a)
- | ParseFailed String
- | ParseDone ByteString
- runParser :: Parser a -> ParseOutput a
- runParser' :: Parser a -> ByteString -> ParseOutput a
- parseByteString :: Parser a -> ByteString -> [a]
- parseLazyByteString :: Parser a -> ByteString -> [a]
- value :: FromJSON a => Parser a
- objectWithKey :: Text -> Parser a -> Parser a
- objectItems :: Parser a -> Parser (Text, a)
- objectValues :: Parser a -> Parser a
- array :: Parser a -> Parser a
- arrayWithIndex :: Int -> Parser a -> Parser a
- indexedArray :: Parser a -> Parser (Int, a)
- filterI :: (a -> Bool) -> Parser a -> Parser a
- toList :: Parser a -> Parser [a]
- defaultValue :: a -> Parser a -> Parser a
- catchFail :: Parser a -> Parser a
How to use this library
>>> parseByteString value "[1,2,3]" :: [[Int]] [[1,2,3]]
The value
parser matches any FromJSON
value. The above command is essentially
identical to the aeson decode function; the parsing process can generate more
objects, therefore the results is [a].
json-stream style parsing would rather look like this:
>>> parseByteString (array value) "[1,2,3]" :: [Int] [1,2,3]
Parsers can be combinated using <*>
and <|>
operators. These operators cause
parallel parsing and yield some combination of the parsed values.
JSON: text = [{"name": "John", "age": 20}, {"age": 30, "name": "Frank"} ] >>> let parser = array $ (,) <$> objectWithKey "name" value <*> objectWithKey "age" value >>> parseByteString parser text :: [(Text,Int)] [("John",20),("Frank",30)]
When parsing larger values, it is advisable to use lazy ByteStrings as the chunking of the ByteStrings causes the parsing to continue more efficently because less state is needed to be held in memory with parallel parsers.
More examples are available on https://github.com/ondrap/json-stream.
The Parser
type
A representation of the parser.
data ParseOutput a Source
Result of parsing. Contains continuations to continue parsing.
ParseYield a (ParseOutput a) | Returns a value from a parser. |
ParseNeedData (ByteString -> ParseOutput a) | Parser needs more data to continue parsing. |
ParseFailed String | Parsing failed, error is reported. |
ParseDone ByteString | Parsing finished, unparsed data is returned. |
Parsing functions
runParser :: Parser a -> ParseOutput a Source
Run streaming parser, immediately returns ParseNeedData
.
runParser' :: Parser a -> ByteString -> ParseOutput a Source
Run streaming parser with initial input.
parseByteString :: Parser a -> ByteString -> [a] Source
Parse a bytestring, generate lazy list of parsed values. If an error occurs, throws an exception.
parseLazyByteString :: Parser a -> ByteString -> [a] Source
Parse a lazy bytestring, generate lazy list of parsed values. If an error occurs, throws an exception.
Basic JSON parsers
objectWithKey :: Text -> Parser a -> Parser a Source
Match only specific key of an object.
objectItems :: Parser a -> Parser (Text, a) Source
Match all key-value pairs of an object, return them as a tuple.
objectValues :: Parser a -> Parser a Source
Match all key-value pairs of an object, return only values.
arrayWithIndex :: Int -> Parser a -> Parser a Source
Match n'th item of an array.
indexedArray :: Parser a -> Parser (Int, a) Source
Match all items of an array, add index to output.
Parsing modifiers
defaultValue :: a -> Parser a -> Parser a Source
Returns a value if none is found upstream.