From 2daf0146d84841a8820ab5a62a7ec43ee9dbb2f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Thu, 7 Dec 2023 22:36:00 -0300 Subject: [PATCH] Replace aeson with json --- liquidhaskell-boot/liquidhaskell-boot.cabal | 2 +- .../Language/Haskell/Liquid/Types/Errors.hs | 69 +++++++++---------- .../Language/Haskell/Liquid/UX/DiffCheck.hs | 49 +++++-------- 3 files changed, 52 insertions(+), 68 deletions(-) diff --git a/liquidhaskell-boot/liquidhaskell-boot.cabal b/liquidhaskell-boot/liquidhaskell-boot.cabal index 0bfafb7c1d..9bf39cb39c 100644 --- a/liquidhaskell-boot/liquidhaskell-boot.cabal +++ b/liquidhaskell-boot/liquidhaskell-boot.cabal @@ -123,7 +123,6 @@ library build-depends: base >= 4.11.1.0 && < 5 , Diff >= 0.3 && < 0.5 - , aeson , binary , bytestring >= 0.10 , Cabal < 3.11 @@ -143,6 +142,7 @@ library , gitrev , hashable >= 1.3 && < 1.5 , hscolour >= 1.22 + , json , liquid-fixpoint == 0.9.4.7 , mtl >= 2.1 , optparse-applicative < 0.19 diff --git a/liquidhaskell-boot/src/Language/Haskell/Liquid/Types/Errors.hs b/liquidhaskell-boot/src/Language/Haskell/Liquid/Types/Errors.hs index 8e948f3edf..2692665768 100644 --- a/liquidhaskell-boot/src/Language/Haskell/Liquid/Types/Errors.hs +++ b/liquidhaskell-boot/src/Language/Haskell/Liquid/Types/Errors.hs @@ -9,7 +9,7 @@ {-# LANGUAGE TypeApplications #-} {-# OPTIONS_GHC -Wno-incomplete-patterns #-} -- TODO(#1918): Only needed for GHC <9.0.1. -{-# OPTIONS_GHC -Wno-orphans #-} -- PPrint and aeson instances. +{-# OPTIONS_GHC -Wno-orphans #-} -- PPrint and json instances. -- | This module contains the *types* related creating Errors. -- It depends only on Fixpoint and basic haskell libraries, @@ -61,7 +61,6 @@ import Data.Typeable (Typeable) import Data.Generics (Data) import qualified Data.Binary as B import qualified Data.Maybe as Mb -import Data.Aeson hiding (Result) import Data.Hashable import qualified Data.HashMap.Strict as M import qualified Data.List as L @@ -93,6 +92,7 @@ import qualified Language.Fixpoint.Misc as Misc import qualified Language.Haskell.Liquid.Misc as Misc import Language.Haskell.Liquid.Misc ((<->)) import Language.Haskell.Liquid.Types.Generics() +import Text.JSON type ParseError = P.ParseError String Void @@ -680,16 +680,26 @@ ppPropInContext td p c ] ] -instance ToJSON RealSrcSpan where - toJSON sp = object [ "filename" .= f - , "startLine" .= l1 - , "startCol" .= c1 - , "endLine" .= l2 - , "endCol" .= c2 +instance JSON RealSrcSpan where + showJSON sp = makeObj [ ("filename", showJSON f) + , ("startLine", showJSON l1) + , ("startCol", showJSON c1) + , ("endLine", showJSON l2) + , ("endCol", showJSON c2) ] where (f, l1, c1, l2, c2) = unpackRealSrcSpan sp + readJSON (JSObject v) = + packRealSrcSpan + <$> valFromObj "filename" v + <*> valFromObj "startLine" v + <*> valFromObj "startCol" v + <*> valFromObj "endLine" v + <*> valFromObj "endCol" v + readJSON _ = Error "Cannot parse JSON for RealSrcSpan" + + unpackRealSrcSpan :: RealSrcSpan -> (String, Int, Int, Int, Int) unpackRealSrcSpan rsp = (f, l1, c1, l2, c2) where @@ -700,16 +710,6 @@ unpackRealSrcSpan rsp = (f, l1, c1, l2, c2) c2 = srcSpanEndCol rsp -instance FromJSON RealSrcSpan where - parseJSON (Object v) = - packRealSrcSpan - <$> v .: "filename" - <*> v .: "startLine" - <*> v .: "startCol" - <*> v .: "endLine" - <*> v .: "endCol" - parseJSON _ = mempty - packRealSrcSpan :: FilePath -> Int -> Int -> Int -> Int -> RealSrcSpan packRealSrcSpan f l1 c1 l2 c2 = mkRealSrcSpan loc1 loc2 where @@ -721,30 +721,25 @@ srcSpanFileMb (RealSrcSpan s _) = Just $ unpackFS $ srcSpanFile s srcSpanFileMb _ = Nothing -instance ToJSON SrcSpan where - toJSON (RealSrcSpan rsp _) = object [ "realSpan" .= True, "spanInfo" .= rsp ] - toJSON (UnhelpfulSpan _) = object [ "realSpan" .= False ] +instance JSON SrcSpan where + showJSON (RealSrcSpan rsp _) = makeObj [ ("realSpan", showJSON True), ("spanInfo", showJSON rsp) ] + showJSON (UnhelpfulSpan _) = makeObj [ ("realSpan", showJSON False) ] -instance FromJSON SrcSpan where - parseJSON (Object v) = do tag <- v .: "realSpan" + readJSON (JSObject v) = do + tag <- valFromObj "realSpan" v if tag - then RealSrcSpan <$> v .: "spanInfo" <*> pure strictNothing + then RealSrcSpan <$> valFromObj "spanInfo" v <*> pure strictNothing else return noSrcSpan - parseJSON _ = mempty - --- Default definition use ToJSON and FromJSON -instance ToJSONKey SrcSpan -instance FromJSONKey SrcSpan + readJSON _ = Error "Cannot parse JSON for SrcSpan" -instance (PPrint a, Show a) => ToJSON (TError a) where - toJSON e = object [ "pos" .= pos e - , "msg" .= render (ppError' Full empty e) - ] +instance (PPrint a, Show a) => JSON (TError a) where + showJSON e = makeObj [ ("pos", showJSON $ pos e) + , ("msg", showJSON $ render (ppError' Full empty e)) + ] -instance FromJSON (TError a) where - parseJSON (Object v) = errSaved <$> v .: "pos" - <*> v .: "msg" - parseJSON _ = mempty + readJSON (JSObject v) = errSaved <$> valFromObj "pos" v + <*> valFromObj "msg" v + readJSON _ = Error "Cannot parse JSON for TError" errSaved :: SrcSpan -> String -> TError a errSaved sp body | n : m <- lines body = ErrSaved sp (text n) (text $ unlines m) diff --git a/liquidhaskell-boot/src/Language/Haskell/Liquid/UX/DiffCheck.hs b/liquidhaskell-boot/src/Language/Haskell/Liquid/UX/DiffCheck.hs index 7da4dacd36..951401f43e 100644 --- a/liquidhaskell-boot/src/Language/Haskell/Liquid/UX/DiffCheck.hs +++ b/liquidhaskell-boot/src/Language/Haskell/Liquid/UX/DiffCheck.hs @@ -39,7 +39,6 @@ module Language.Haskell.Liquid.UX.DiffCheck ( import Prelude hiding (error) -import Data.Aeson import qualified Data.Text as T import Data.Algorithm.Diff import Data.Maybe (maybeToList, listToMaybe, mapMaybe, fromMaybe) @@ -61,6 +60,7 @@ import qualified Data.ByteString as B import qualified Data.ByteString.Lazy as LB import Language.Haskell.Liquid.Types hiding (Def, LMap) +import Text.JSON -------------------------------------------------------------------------------- -- | Data Types ---------------------------------------------------------------- @@ -477,7 +477,7 @@ saveResult :: FilePath -> Output Doc -> IO () -------------------------------------------------------------------------------- saveResult target res = do copyFile target saveF - B.writeFile errF $ LB.toStrict $ encode res + writeFile errF $ encode res where saveF = extFileName Saved target errF = extFileName Cache target @@ -488,10 +488,12 @@ loadResult :: FilePath -> IO (Output Doc) loadResult f = do ex <- doesFileExist jsonF if ex - then convert <$> B.readFile jsonF + then convert <$> readFile jsonF else return mempty where - convert = fromMaybe mempty . decode . LB.fromStrict + convert s = case decode s of + Ok a -> a + Error _ -> mempty jsonF = extFileName Cache f -------------------------------------------------------------------------------- @@ -568,41 +570,28 @@ checkedItv chDefs = foldr (`IM.insert` ()) IM.empty is -- | Aeson instances ----------------------------------------------------------- -------------------------------------------------------------------------------- -instance ToJSON SourcePos where - toJSON p = object [ "sourceName" .= f - , "sourceLine" .= unPos l - , "sourceColumn" .= unPos c +instance JSON SourcePos where + showJSON p = makeObj [ ("sourceName", showJSON f) + , ("sourceLine", showJSON $ unPos l) + , ("sourceColumn", showJSON $ unPos c) ] where f = sourceName p l = sourceLine p c = sourceColumn p -instance FromJSON SourcePos where - parseJSON (Object v) = safeSourcePos <$> v .: "sourceName" - <*> v .: "sourceLine" - <*> v .: "sourceColumn" - parseJSON _ = mempty + readJSON (JSObject v) = safeSourcePos <$> valFromObj "sourceName" v + <*> valFromObj "sourceLine" v + <*> valFromObj "sourceColumn" v + readJSON _ = Error "Expected Object for SourcePos value" -instance FromJSON ErrorResult +instance JSON Doc where + showJSON = showJSON . render + readJSON v = text <$> readJSON v -instance ToJSON Doc where - toJSON = String . T.pack . render +instance JSON a => JSON (AnnInfo a) -instance FromJSON Doc where - parseJSON (String s) = return $ text $ T.unpack s - parseJSON _ = mempty - -instance ToJSON a => ToJSON (AnnInfo a) where - toJSON = genericToJSON defaultOptions - toEncoding = genericToEncoding defaultOptions -instance FromJSON a => FromJSON (AnnInfo a) - -instance ToJSON (Output Doc) where - toJSON = genericToJSON defaultOptions - toEncoding = genericToEncoding defaultOptions -instance FromJSON (Output Doc) where - parseJSON = genericParseJSON defaultOptions +instance JSON (Output Doc) where file :: Located a -> FilePath file = sourceName . loc