你能否使用Aeson包解析JSON中的Data.Decimal?
假设我有以下JSON:
{
"foo": 5.231,
"bar": "smth"
}
还有以下记录类型:
data test { foo :: Data.Decimal
, bar :: String } deriving Generic
使用
instance FromJSON test
instance ToJSON test
如果没有 "foo" 的
Data.Decimal
值,这将起作用。据我了解,我需要手动创建一个 Data.Decimal 的
FromJSON
和 ToJSON
实例(用于转换回 JSON),因为它不是从 Generic 派生的。我该怎么做?提前感谢。
Decimal
包装在newtype
中,然后按照aeson文档中的“手写实例”部分进行操作。然而,更明智的做法很可能是从Data.Decimal
切换到Data.Scientific
(来自于scientific包),因为它具有由aeson提供的实例。 - duplode