Haskell, Aeson & JSON parsing into custom type

后端 未结 3 1928
春和景丽
春和景丽 2021-01-02 01:26

Following on from a previous post, I\'ve found I\'m totally stuck. I\'m trying to parse a JSON structure into my own type, and not only am I stuck on how to parse the Array,

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 02:30

    You have to follow the type of parseJSON a little bit down a rabbit trail, but once you recognize what (Array a) represents, it should be straightforward.

    parseJSON has type Value -> Parser a, so (Array a) has type Value. One of the variants in the Value type is Array Array, so the a in (Array a) must be of the type Array, which is defined as Vector Value. The Values inside that Vector are what you want to call parseJSON on to return your list, so check out what you can do with a Vector.

    The easiest approach would probably to convert a to a list with Vector.toList, and then use mapM to parse the Values.

    Alternately, you could avoid the Vector to list conversion by changing your ExifArray variant to hold Vector ExifValue, and then using Vector.mapM.

提交回复
热议问题