Generic ToJSON for Statements
This commit is contained in:
parent
fa3093e61c
commit
b28555b0f4
|
@ -27,12 +27,13 @@ module Data.Language.Ruby.AST
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Aeson (parseJSON, Value(Null,Object,Number,Bool), withArray)
|
import Data.Aeson (parseJSON, Value(Null,Object,Number,Bool), withArray)
|
||||||
import Prelude (Bounded)
|
import Prelude (Bounded, drop)
|
||||||
import Frelude hiding (String, Int)
|
import Frelude hiding (String, Int)
|
||||||
import qualified Frelude
|
import qualified Frelude
|
||||||
import qualified Data.Aeson.Types as Aeson
|
import qualified Data.Aeson.Types as Aeson
|
||||||
import qualified Data.Vector as Vector
|
import qualified Data.Vector as Vector
|
||||||
import qualified Data.Scientific as Scientific
|
import qualified Data.Scientific as Scientific
|
||||||
|
import Data.Char (toLower)
|
||||||
|
|
||||||
kebabCase :: Frelude.String -> Frelude.String
|
kebabCase :: Frelude.String -> Frelude.String
|
||||||
kebabCase = Aeson.camelTo2 '-'
|
kebabCase = Aeson.camelTo2 '-'
|
||||||
|
@ -89,25 +90,7 @@ instance ToJSON Statement where
|
||||||
toEncoding = Aeson.genericToEncoding opts
|
toEncoding = Aeson.genericToEncoding opts
|
||||||
where
|
where
|
||||||
opts = aesonOptions { Aeson.constructorTagModifier = go }
|
opts = aesonOptions { Aeson.constructorTagModifier = go }
|
||||||
go = \case
|
go s = toLower <$> drop 4 s
|
||||||
"StmtModule" -> "module"
|
|
||||||
"StmtDef" -> "def"
|
|
||||||
"StmtDefs" -> "defs"
|
|
||||||
"StmtSend" -> "send"
|
|
||||||
"StmtBlock" -> "block"
|
|
||||||
"StmtConst" -> "const"
|
|
||||||
"StmtCasgn" -> "casgn"
|
|
||||||
"StmtArray" -> "array"
|
|
||||||
"StmtSym" -> "sym"
|
|
||||||
"StmtStr" -> "str"
|
|
||||||
"StmtLvasgn" -> "lvasgn"
|
|
||||||
"StmtLvar" -> "lvar"
|
|
||||||
"StmtIvar" -> "ivar"
|
|
||||||
"StmtSelf" -> "self"
|
|
||||||
"StmtNil" -> "nil"
|
|
||||||
"StmtInt" -> "int"
|
|
||||||
"StmtCbase" -> "cbase"
|
|
||||||
x -> x
|
|
||||||
|
|
||||||
instance FromJSON Statement where
|
instance FromJSON Statement where
|
||||||
parseJSON v
|
parseJSON v
|
||||||
|
|
Loading…
Reference in a new issue