-
Notifications
You must be signed in to change notification settings - Fork 69
Json parser #605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Json parser #605
Conversation
9ffbe66
to
ec4f205
Compare
|
||
{-# INLINE match #-} | ||
match :: MonadCatch m => Word8 -> Parser m Word8 () | ||
match w = P.eqBy (==) [w] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be P.satisfy (== w)
.
where | ||
initial = return begin | ||
|
||
step s a = return $ s * 10 + fromIntegral (a - 48) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are assuming that the input is already validated to be digits.
n <- P.takeWhile1 (\w -> w - 48 <= 9) (foldToInteger 0) | ||
when (z == zero && n /= 0) $ do | ||
P.die $ " Leading zero in a number is not accepted in JSON." | ||
return n |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not use a monad here, I am not sure if it fuses, you can check. We can write it in other ways e.g.:
- write it directly as a primitive parser
- use a
tee
let positive = sign == plus || sign /= minus | ||
pr = P.takeWhile1 (\w -> w - 48 <= 9) (foldToInteger 0) | ||
when (sign == plus || sign == minus) (skip 1) | ||
if positive then pr else negate <$> pr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoid using monad.
{-# INLINE parseJsonNumber #-} | ||
parseJsonNumber :: MonadCatch m => Parser m Word8 Scientific | ||
parseJsonNumber = do | ||
sign <- P.peek |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid monad.
|
||
{-# INLINE parseJsonString #-} | ||
parseJsonString :: MonadCatch m => Parser m Word8 JsonString | ||
parseJsonString = do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoid monad.
parseJson :: MonadCatch m => PR.Parser m Word8 Value | ||
parseJson = K.toParserK $ parseJsonValue | ||
|
||
{- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove this?
escapeFoldUtf8With :: Monad m => Fold m Char container -> Fold m Word8 container | ||
escapeFoldUtf8With = Uni.escapeFoldUtf8With 92 jsonEscapes | ||
|
||
type JsonString = Array Char |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't we have this as Array Word8
using UTF-8 encoding? When needed it can be converted to Chars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we using Data.Array
for this? It is very inefficient. We can use the Storable array instead.
No description provided.