Haskell Data.Decimal as Aeson type

时光怂恿深爱的人放手 提交于 2019-12-05 13:20:41

I know this is an old thread, but might help someone. Here is how I am converting Decimal to/from json (I assembled this code from a bunch of other code which I don't have the source for now):

instance A.ToJSON (DecimalRaw Integer) where
  -- maybe this is not the best, but it works
  toJSON d = A.toJSON $ show d

instance A.FromJSON (DecimalRaw Integer) where
  parseJSON = A.withText "Decimal" (go . (read :: String -> [(DecimalRaw Integer, String)]) . T.unpack)
    where
      go [(v, [])] = return v
      go (_ : xs) = go xs
      go _ = fail "Could not parse number"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!