@@ -14,7 +14,9 @@
14module Data.Aeson.Parser
14module Data.Aeson.Parser
@@ -47,6 +49,19 @@ json = do
47 then object_
49 then object_
48 else array_
50 else array_
52+-- | Parse a top-level JSON value. This must be either an object or
55+-- This is a strict parser version of 'json' which avoids
56+-- building up thunks during parsing. Prefer this version if most of
57+-- the JSON data needs to be accessed.
60+ c <- skipSpace *> satisfy (`B8.elem` "{[")
50object_ :: Parser Value
65object_ :: Parser Value
51object_ = {-# SCC "object_" #-} do
66object_ = {-# SCC "object_" #-} do
@@ -57,12 +72,28 @@ object_ = {-# SCC "object_" #-} do
57 vals <- ((pair <* skipSpace) `sepBy` (char ',' *> skipSpace)) <* char '}'
72 vals <- ((pair <* skipSpace) `sepBy` (char ',' *> skipSpace)) <* char '}'
58 return . Object $ H.fromList vals
73 return . Object $ H.fromList vals
75+object_' :: Parser Value
76+object_' = {-# SCC "object_'" #-} do
79+ a <- jstring <* skipSpace
80+ b <- char ':' *> skipSpace *> value'
82+ vals <- ((pair <* skipSpace) `sepBy` (char ',' *> skipSpace)) <* char '}'
83+ return $! Object $ H.fromList vals
60array_ :: Parser Value
85array_ :: Parser Value
61array_ = {-# SCC "array_" #-} do
86array_ = {-# SCC "array_" #-} do
63 vals <- ((value <* skipSpace) `sepBy` (char ',' *> skipSpace)) <* char ']'
88 vals <- ((value <* skipSpace) `sepBy` (char ',' *> skipSpace)) <* char ']'
64 return . Array $ Vector.fromList vals
89 return . Array $ Vector.fromList vals
91+array_' :: Parser Value
92+array_' = {-# SCC "array_'" #-} do
94+ vals <- ((value' <* skipSpace) `sepBy` (char ',' *> skipSpace)) <* char ']'
95+ return $! Array $ Vector.fromList vals
66-- | Parse any JSON value. Use 'json' in preference to this function
97-- | Parse any JSON value. Use 'json' in preference to this function
67-- if you are parsing data from an untrusted source.
98-- if you are parsing data from an untrusted source.
68value :: Parser Value
99value :: Parser Value
@@ -79,6 +110,26 @@ value = most <|> (Number <$> number)
79 'n' -> string "ull" *> pure Null
110 'n' -> string "ull" *> pure Null
80 _ -> error "attoparsec panic! the impossible happened!"
111 _ -> error "attoparsec panic! the impossible happened!"
113+-- | Strict version of 'value'. See also 'json''.
114+value' :: Parser Value
115+value' = most <|> num
121+ c <- satisfy (`B8.elem` "{[\"ftn")
128+ 'f' -> string "alse" *> pure (Bool False)
129+ 't' -> string "rue" *> pure (Bool True)
130+ 'n' -> string "ull" *> pure Null
131+ _ -> error "attoparsec panic! the impossible happened!"
82doubleQuote, backslash :: Word8
133doubleQuote, backslash :: Word8
83doubleQuote = 34
134doubleQuote = 34
84backslash = 92
135backslash = 92