|
| 1 | +{-| |
| 2 | +Module : Eucalypt.Render.Json |
| 3 | +Description : JSON renderer for Eucalypt |
| 4 | +Copyright : (c) Greg Hawkins, 2018 |
| 5 | +License : |
| 6 | + |
| 7 | +Stability : experimental |
| 8 | +-} |
| 9 | +module Eucalypt.Render.Json |
| 10 | + where |
| 11 | + |
| 12 | +import Control.Monad ((>=>)) |
| 13 | +import Data.Aeson |
| 14 | +import Data.Aeson.Encode.Pretty (encodePretty) |
| 15 | +import Data.Maybe |
| 16 | +import Data.Text (Text, pack) |
| 17 | +import Data.Scientific |
| 18 | +import qualified Data.Vector as V |
| 19 | +import qualified Data.HashMap.Strict as HM |
| 20 | +import Eucalypt.Core.Error |
| 21 | +import Eucalypt.Core.Interpreter |
| 22 | +import Eucalypt.Core.Syn |
| 23 | +import Eucalypt.Render.Classes |
| 24 | +import Eucalypt.Render.Common |
| 25 | +import qualified Data.ByteString.Lazy as BS |
| 26 | + |
| 27 | +-- | Render key and value to YAML |
| 28 | +renderKeyValue :: WhnfEvaluator -> CoreExpr -> CoreExpr -> Interpreter (Maybe (Text, Value)) |
| 29 | +renderKeyValue whnfM k v = do |
| 30 | + key <- (whnfM >=> expectText) k |
| 31 | + value <- whnfM v |
| 32 | + case value of |
| 33 | + CoreLambda{} -> return Nothing |
| 34 | + CoreOperator{} -> return Nothing |
| 35 | + CoreBuiltin _ -> return Nothing |
| 36 | + CorePAp {} -> return Nothing |
| 37 | + _ -> toJSONExpr whnfM value >>= \rendered -> return (Just (key, rendered)) |
| 38 | + |
| 39 | + |
| 40 | +-- | Json rendering configuration |
| 41 | +newtype JsonConfig = JsonConfig |
| 42 | + { jsonPretty :: Bool } |
| 43 | + |
| 44 | + |
| 45 | +-- | Generate an Aeson Value model of the required JSON in the |
| 46 | +-- Interpreter monad. |
| 47 | +toJSONExpr :: WhnfEvaluator -> CoreExpr -> Interpreter Value |
| 48 | +toJSONExpr _ (CorePrim p) = |
| 49 | + return $ |
| 50 | + case p of |
| 51 | + CoreString s -> String $ pack s |
| 52 | + CoreSymbol s -> String $ pack s |
| 53 | + CoreInt i -> Number $ fromInteger i |
| 54 | + CoreFloat f -> Number $ fromFloatDigits f |
| 55 | + CoreBoolean b -> Bool b |
| 56 | + CoreNull -> Null |
| 57 | +toJSONExpr whnfM (CoreList items) = |
| 58 | + Array . V.fromList <$> traverse (whnfM >=> toJSONExpr whnfM) items |
| 59 | +toJSONExpr whnfM (CoreBlock list) = do |
| 60 | + content <- whnfM list |
| 61 | + case content of |
| 62 | + CoreList items -> Object . HM.fromList . catMaybes <$> traverse (whnfM >=> pair) items |
| 63 | + e -> throwEvalError $ BadBlockContent (CoreExpShow e) |
| 64 | + where |
| 65 | + pair item = do |
| 66 | + i <- boilAwayMetadata whnfM item |
| 67 | + case i of |
| 68 | + Just (CoreList [k, v]) -> renderKeyValue whnfM k v |
| 69 | + Just expr -> throwEvalError $ BadBlockElement (CoreExpShow expr) |
| 70 | + Nothing -> return Nothing |
| 71 | +toJSONExpr whnfM (CoreMeta _ v) = toJSONExpr whnfM v |
| 72 | +toJSONExpr _ expr = throwEvalError $ NotWeakHeadNormalForm (CoreExpShow expr) |
| 73 | + |
| 74 | + |
| 75 | +-- | Reduce and render |
| 76 | +renderJsonBytes :: |
| 77 | + JsonConfig |
| 78 | + -> WhnfEvaluator |
| 79 | + -> CoreExpr |
| 80 | + -> IO (Either EvaluationError BS.ByteString) |
| 81 | +renderJsonBytes cfg whnfM expr = |
| 82 | + case runInterpreter (whnfM expr >>= toJSONExpr whnfM) of |
| 83 | + Left e -> (return . Left) e |
| 84 | + Right j -> |
| 85 | + return $ |
| 86 | + Right $ |
| 87 | + if jsonPretty cfg |
| 88 | + then encodePretty j |
| 89 | + else encode j |
| 90 | + |
| 91 | + |
| 92 | +instance Renderer JsonConfig where |
| 93 | + renderBytes config whnfM = fmap (fmap BS.toStrict) . renderJsonBytes config whnfM |
0 commit comments