我在用
aeson-schemas-1.0.3
我想构建
没有通过外部序列化表示的往返。这看起来像是一次黑客攻击,我很担心对性能的影响。
type Example = [schema|
{
example: Text,
}
|]
我想写这样的东西:
coerceJson $ object [ "example" .= ("Example" :: Text) ]
我有一个允许这样做的解决方案,但它涉及到对ByteString的编码和对所需SchemaType的对象的解码,这看起来既昂贵又不雅观:
coerceJson :: FromJSON a => Value -> a
coerceJson = fromJust . decode . encode
SSCCE (Short, Self Contained, Correct (Compilable), Example)
我的黑客解决方案被雇佣了。它起作用了,但我相信有更好的解决办法。
#!/usr/bin/env stack
{- stack
runghc
--resolver lts-14.15
--package aeson-schemas-1.0.3
--package aeson
--package text
-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TypeFamilies #-}
import Data.Aeson (decode, encode, object, (.=), FromJSON, Value)
import Data.Aeson.Schema
import Data.Aeson.Text (encodeToLazyText)
import Data.Maybe (fromJust)
import qualified Data.Text.IO as T
import Data.Text(Text)
import Data.Text.Lazy (toStrict)
main :: IO ()
main = do
let example = coerceJson $ object [ "example" .= ("Example" :: Text) ]
useExample example
useExample :: Object Example -> IO ()
useExample example = T.putStrLn $ toStrict $ encodeToLazyText $ object [
"example" .= [get| example.example|]
]
coerceJson :: FromJSON a => Value -> a
coerceJson = fromJust . decode . encode
type Example = [schema|
{
example: Text,
}
|]
在aeson模式中,如何构造SchemaType的对象而不进行文本编码和解码?