如何使用透镜在 Aeson.Object 中对所有 key/value 对进行 concatMap

How to concatMap over all key/value pairs in an Aeson.Object using lenses

我一直在玩Control.Lens.Indexed中的组合器,尤其是iconcatMap, but I haven't been able to come-up with a function with the following type-sig using the Data.Aeson.Lens.members镜头:

func 

  -- list of key/value pairs, essentially
  :: Aeson.Object                  

  -- function for the concatMap operation to which the 
  -- key (Text) and value is passed
  -> (Text -> Aeson.Value -> [a])  

  -- resultant concatenated list
  -> [a]

所需函数是iconcatMapOf。另外,请注意 members 遍历具有 AsValue 实例的类型,而 Object 没有这样的实例,因此我们需要将其包装到 Value

import qualified Data.Aeson as Aeson
import Control.Lens
import Data.Aeson.Lens
import Data.Text

func :: Aeson.Object -> (Text -> Aeson.Value -> [a]) -> [a]
func obj f = iconcatMapOf members f (Aeson.Object obj)