Final version of JSON to sum type

After some feedback on my previous post I ended up with the following implementation.

instance FromJSON V.VersionRange where
  parseJSON = withObject "VersionRange" go
    where
      go o =
        V.thisVersion <$> o .: "ThisVersion" <|>
        V.laterVersion <$> o .: "LaterVersion" <|>
        V.earlierVersion <$> o .: "EarlierVersion" <|>
        V.WildcardVersion <$> o .: "WildcardVersion" <|>
        nullaryOp V.anyVersion <$> o .: "AnyVersion" <|>
        binaryOp V.unionVersionRanges <$> o .: "UnionVersionRanges" <|>
        binaryOp V.intersectVersionRanges <$> o .: "IntersectVersionRanges" <|>
        V.VersionRangeParens <$> o .: "VersionRangeParens"

      nullaryOp :: a -> Value -> a
      nullaryOp = const

      binaryOp f [a, b] = f a b

Thanks David for your suggestions.

⟸ From JSON to sum type Qt5+D-Bus+CMake, a complete example ⟹
Leave a comment