json - haskell record parameter or alternative
问题描述
I am using aeson for parsing a https://jsonlines.org/ files. Those files are dumps of Elastic Search indexes so the outermost structure is the same in all JSON objects. If I understood right, record syntax do not allow parameter like in the Document
record below, right? What would be the alternative?
One possible solution would be to use the JSON AST to read the JSON innermost objects and parse them latter, is it possible? How?
import Data.Aeson
import qualified Data.ByteString.Lazy as L
import Data.Either
import Data.List
import GHC.Generics
data Suggestion =
Suggestion { ... }
deriving (Show, Generic)
data Synset =
Synset { ...}
deriving (Show, Generic)
data Document a =
Document a
{ _index :: String
, _type :: String
, _id :: String
, _score :: Int
, _source :: a
}
deriving (Show, Generic)
readJ :: L.ByteString -> Either String Document
readJ s = eitherDecode s :: Either String Document
readJL :: FilePath -> IO [Either String Document]
readJL path = do
content <- L.readFile path
return (map readJ $ L.split 10 content)
解决方案
是的,记录语法确实允许类型参数,您只是不应该在构造函数名称之后重复它们:
data Document a =
Document
{ _index :: String
, _type :: String
, _id :: String
, _score :: Int
, _source :: a
}
deriving (Show, Generic)
推荐阅读
- spring - 什么是属性 management.health.redis.enabled 以及如何使用它?
- c# - 如何在 c# 函数中声明参数以使用 oracle db 获取输入输出?
- ios - Xamarin iOS:找不到 iPad mini 的模拟器,也没有最新的 iOS 版本 11.4.1
- swift - POD 安装问题:“`ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES”
- nginx - 使用 nginx 代理 S3 请求以进行授权
- reactjs - fetch api 或 whatwg-fetch 是否修改状态码为 304 到 200 的响应?
- python - 如何使用 pandas DataFrame 计算列表的字典?
- mysql - 在计算时间间隔时,有没有办法不改变 varchar 的类型?
- php - 从页面 A 在页面 B 上回显一个变量而不包含
- react-native - React native - 显示启动视频的最佳方式