在 aeson-schemas 中,你如何构造一个 SchemaType 的对象而不编码为文本并解码回来?
In aeson-schemas how do you construct an Object of a SchemaType without encoding to text and decoding back?
我正在使用 aeson-schemas-1.0.3 并且我想构造 Object Example 的值而不通过外部序列化表示进行往返。这似乎是一个 hack,我担心性能影响。
我定义了这个模式:
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),其中采用了我的 hack 解决方法。它有效,但我相信有更好的解决方案。
#!/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-schemas 中,如何构造 SchemaType 的对象而不编码为文本并解码回来?
我是 aeson-schemas
的作者。目前没有办法制作文字Object
。您尝试做的问题是,您如何知道文字 Object
与模式匹配?我有可能制作一个 unsafeObject
准引号,假设该对象与您键入它的模式相匹配。
我知道这已经过时了,但如果您对此仍有疑问,那么您的用例到底是什么?通常,您会从外部源加载 JSON 数据,例如 API 或文件。
我正在使用 aeson-schemas-1.0.3 并且我想构造 Object Example 的值而不通过外部序列化表示进行往返。这似乎是一个 hack,我担心性能影响。
我定义了这个模式:
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),其中采用了我的 hack 解决方法。它有效,但我相信有更好的解决方案。
#!/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-schemas 中,如何构造 SchemaType 的对象而不编码为文本并解码回来?
我是 aeson-schemas
的作者。目前没有办法制作文字Object
。您尝试做的问题是,您如何知道文字 Object
与模式匹配?我有可能制作一个 unsafeObject
准引号,假设该对象与您键入它的模式相匹配。
我知道这已经过时了,但如果您对此仍有疑问,那么您的用例到底是什么?通常,您会从外部源加载 JSON 数据,例如 API 或文件。