diff --git a/lib/Bril.hs b/lib/Bril.hs index b0453f2..a7a08f7 100644 --- a/lib/Bril.hs +++ b/lib/Bril.hs @@ -5,7 +5,7 @@ module Bril ( parseBrilFromPath, parseBrilJSON, )where -import Data.Aeson (FromJSON, parseJSON, withObject, withText, withScientific, (.:), (.:?), eitherDecode) +import Data.Aeson (FromJSON, parseJSON, withObject, withText, withBool, withScientific, (.:), (.:?), eitherDecode) import Data.Aeson.Types (Parser, modifyFailure) import qualified Data.Aeson (Value, Object, Key) import Data.Aeson.KeyMap @@ -203,19 +203,8 @@ instance FromJSON Type where parseValue :: Type -> Data.Aeson.Value -> Parser Value parseValue Bool = - withText "BoolValue" $ \v -> - case v of - "true" -> return $ BoolValue True - "false" -> return $ BoolValue False - _ -> fail $ "invalid bool literal: " ++ (T.unpack v) ---parseValue Int = --- withText "IntValue" $ \v -> --- case ((signed decimal :: Reader Int64) v) of --- Left x -> fail $ "invalid int literal: " ++ x --- Right (val, t) -> --- if (T.null t) --- then return $ IntValue val --- else fail $ "invalid int literal: " ++ (T.unpack v) + withBool "BoolValue" (return . BoolValue) + parseValue Int = withScientific "IntValue" $ \v -> if (not (isInteger v)) diff --git a/tests/Main.hs b/tests/Main.hs index 61e1709..55c0bdb 100644 --- a/tests/Main.hs +++ b/tests/Main.hs @@ -20,11 +20,9 @@ testTypes = TestCase $ do testLiterals :: Test testLiterals = TestCase $ do assertEqual "true literal" - (parseLiteral B.Bool ("true"::Text)) (Success $ B.BoolValue True) + (parseLiteral B.Bool True) (Success $ B.BoolValue True) assertEqual "false literal" - (parseLiteral B.Bool ("false"::Text)) (Success $ B.BoolValue False) - assertEqual "wrong bool literal" - (parseLiteral B.Bool ("xxx"::Text)) (Error "invalid bool literal: xxx") + (parseLiteral B.Bool False) (Success $ B.BoolValue False) assertEqual "int literal" (parseLiteral B.Int (1434 :: Int)) (Success $ B.IntValue 1434)