Skip to content

Commit

Permalink
Replace aeson with json
Browse files Browse the repository at this point in the history
  • Loading branch information
facundominguez committed Dec 8, 2023
1 parent 60e5e56 commit 2daf014
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 68 deletions.
2 changes: 1 addition & 1 deletion liquidhaskell-boot/liquidhaskell-boot.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
69 changes: 32 additions & 37 deletions liquidhaskell-boot/src/Language/Haskell/Liquid/Types/Errors.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
49 changes: 19 additions & 30 deletions liquidhaskell-boot/src/Language/Haskell/Liquid/UX/DiffCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 ----------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand All @@ -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

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2daf014

Please sign in to comment.