-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Incremental applicative JSON parser -- @package json-stream @version 0.3.2.2 -- | 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. module Data.JsonStream.Parser -- | A representation of the parser. data Parser a -- | Result of parsing. Contains continuations to continue parsing. data ParseOutput a -- | Returns a value from a parser. ParseYield :: a -> (ParseOutput a) -> ParseOutput a -- | Parser needs more data to continue parsing. ParseNeedData :: (ByteString -> ParseOutput a) -> ParseOutput a -- | Parsing failed, error is reported. ParseFailed :: String -> ParseOutput a -- | Parsing finished, unparsed data is returned. ParseDone :: ByteString -> ParseOutput a -- | Match items from the first parser, if none is matched, return items -- from the second parser. Constant-space if second parser returns -- constant number of items. .| is implemented using this -- operator. -- --
-- >>> let json = "[{\"key1\": [1,2], \"key2\": [5,6], \"key3\": [8,9]}]"
-- >>> let parser = arrayOf $ "key1" .: (arrayOf value) >^> "key2" .: (arrayOf value)
-- >>> parseByteString parser json :: [Int]
-- [1,2]
-- >>> let parser = arrayOf $ "key-non" .: (arrayOf value) >^> "key2" .: (arrayOf value)
-- >>> parseByteString parser json :: [Int]
-- [5,6]
--
(>^>) :: Parser a -> Parser a -> Parser a
-- | Run streaming parser, immediately returns ParseNeedData.
runParser :: Parser a -> ParseOutput a
-- | Run streaming parser with initial input.
runParser' :: Parser a -> ByteString -> ParseOutput a
-- | Parse a bytestring, generate lazy list of parsed values. If an error
-- occurs, throws an exception.
--
-- -- parseByteString (arrayOf integer) "[1,2,3,4]" :: [Int] -- [1,2,3,4] ---- --
-- parseByteString (arrayOf ("name" .: string)) "[{\"name\":\"KIWI\"}, {\"name\":\"BIRD\"}]"
-- ["KIWI","BIRD"]
--
parseByteString :: Parser a -> ByteString -> [a]
-- | Parse a lazy bytestring, generate lazy list of parsed values. If an
-- error occurs, throws an exception.
parseLazyByteString :: Parser a -> ByteString -> [a]
-- | Match FromJSON value. Calls parseJSON on the parsed value.
--
--
-- >>> let json = "[{\"key1\": [1,2], \"key2\": [5,6]}]"
-- >>> parseByteString (arrayOf value) json :: [Value]
-- [Object fromList [("key2",Array (fromList [Number 5.0,Number 6.0])),("key1",Array (fromList [Number 1.0,Number 2.0]))]]
--
value :: FromJSON a => Parser a
-- | Parse string value, skip parsing otherwise.
string :: Parser Text
-- | Stops parsing string after the limit is reached. The string will not
-- be matched if it exceeds the size. The size is the size of escaped
-- string including escape characters.
safeString :: Int -> Parser Text
-- | Parse number, return in scientific format.
number :: Parser Scientific
-- | Parse to bounded integer type (not Integer). If you are using
-- integer numbers, use this parser. It skips the conversion JSON ->
-- Scientific -> Int and uses an Int directly.
integer :: (Integral i, Bounded i) => Parser i
-- | Parse to float/double.
real :: RealFloat a => Parser a
-- | Parse bool, skip if the type is not bool.
bool :: Parser Bool
-- | Match a null value.
jNull :: Parser ()
-- | Synonym for objectWithKey. Matches key in an object. The
-- .: operators can be chained.
--
--
-- >>> let json = "{\"key1\": {\"nested-key\": 3}}"
-- >>> parseByteString ("key1" .: "nested-key" .: integer) json :: [Int]
-- [3]
--
(.:) :: Text -> Parser a -> Parser a
-- | Returns Nothing if value is null or does not exist or match.
-- Otherwise returns Just value.
--
-- -- key .:? val = Just <$> key .: val >^> pure Nothing --(.:?) :: Text -> Parser a -> Parser (Maybe a) -- | Return default value if the parsers on the left hand didn't produce a -- result. -- --
-- p .| defval = p >^> pure defval ---- -- The operator works on complete left side, the following statements are -- equal: -- --
-- Record <$> "key1" .: "nested-key" .: value .| defaultValue
-- Record <$> (("key1" .: "nested-key" .: value) .| defaultValue)
--
(.|) :: Parser a -> a -> Parser a
-- | Synonym for arrayWithIndexOf. Matches n-th item in array.
--
-- -- >>> parseByteString (arrayOf (1 .! bool)) "[ [1,true,null], [2,false], [3]]" :: [Bool] -- [True,False] --(.!) :: Int -> Parser a -> Parser a -- | Match only specific key of an object. This function will return only -- the first matched value in an object even if the source JSON defines -- the key multiple times (in violation of the specification). objectWithKey :: Text -> Parser a -> Parser a -- | Match all key-value pairs of an object, return them as a tuple. If the -- source object defines same key multiple times, all values are matched. objectItems :: Parser a -> Parser (Text, a) -- | Match all key-value pairs of an object, return only values. If the -- source object defines same key multiple times, all values are matched. -- Keys are ignored. objectValues :: Parser a -> Parser a -- | Match all items of an array. arrayOf :: Parser a -> Parser a -- | Match nith item in an array. arrayWithIndexOf :: Int -> Parser a -> Parser a -- | Match all items of an array, add index to output. indexedArrayOf :: Parser a -> Parser (Int, a) -- | Parses a field with a possible null value. nullable :: Parser a -> Parser (Maybe a) -- | Let only items matching a condition pass. -- --
-- >>> parseByteString (filterI (>5) $ arrayOf integer) "[1,2,3,4,5,6,7,8,9,0]" :: [Int] -- [6,7,8,9] --filterI :: (a -> Bool) -> Parser a -> Parser a -- | Take maximum n matching items. -- --
-- >>> parseByteString (takeI 3 $ arrayOf integer) "[1,2,3,4,5,6,7,8,9,0]" :: [Int] -- [1,2,3] --takeI :: Int -> Parser a -> Parser a -- | Gather matches and return them as list. -- --
-- >>> let json = "[{\"keys\":[1,2], \"values\":[5,6]}, {\"keys\":[9,8], \"values\":[7,6]}]"
-- >>> let parser = arrayOf $ (,) <$> toList ("keys" .: arrayOf integer)
-- <*> toList ("values" .: arrayOf integer)
-- >>> parseByteString parser json :: [([Int], [Int])]
-- [([1,2],[5,6]),([9,8],[7,6])]
--
toList :: Parser a -> Parser [a]
-- | A back-door for lifting of possibly failing actions. If an action
-- fails with Left value, convert it into failure of parsing
mapWithFailure :: (a -> Either String b) -> Parser a -> Parser b
-- | Generate start/end values when an array is found, in between run a
-- parser. The inner parser is not run if an array is not found.
--
--
-- >>> let test = "[[1,2,3],true,[],false,{\"key\":1}]" :: ByteString
-- >>> parseByteString (arrayOf (arrayFound 10 20 (1 .! integer))) test :: [Int]
-- [10,2,20,10,20]
--
arrayFound :: a -> a -> Parser a -> Parser a
-- | Generate start/end values when an object is found, in between run a
-- parser. The inner parser is not run if an array is not found.
objectFound :: a -> a -> Parser a -> Parser a
instance Alternative Parser
instance Applicative Parser
instance Functor Parser
instance Functor ParseResult