Analizando una matriz con Haskell Aeson
Tengo un documento JSON que se parece a:
{ "series": [[1,2], [2,3], [3,4]] }
Me gustaría analizar esto en un conjunto de tipos de datos:
data Series = Series [DataPoint]
data DataPoint = DataPoint Int Int -- x and y
Tengo muchos problemas tratando de escribir elFromJSON
instancia para DataPoint.
instance FromJSON DataPoint where
parseJSON (Array a) = ???
Intenté usar Lens para destruir el registro de DataPoint, pero no compila:
case a ^.. values . _Integer of -}
[x,y] -> DataPoint <case a ^.. values . _Integer of -}
[x,y] -> DataPoint <$> x <*> y
_ -> mzero
gt; x <*> y
_ -> mzero
Eso falla con este error (las dos primeras líneas me faltan incluso el truco de la lente, solo intento crear unDataPoint <$> 1 <*> 2
):
Couldn't match type ‘aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser
Integer’
with ‘Integer’
Expected type: (aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser
Integer
-> Const
(Data.Monoid.Endo
[aeson-0.7.0.6:Data.Aeson.Types.Internal.Parse
(aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser I
-> Value
-> Const
(Data.Monoid.Endo
[aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser
Value
Actual type: (Integer
-> Const
(Data.Monoid.Endo
[aeson-0.7.0.6:Data.Aeson.Types.Internal.Parse
Integer)
-> Value
-> Const
(Data.Monoid.Endo
[aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser
Value
In the second argument of ‘(.)’, namely ‘_Integer’
In the second argument of ‘(^..)’, namely ‘values . _Integer’
¿Hay una mejor manera de hacer esto?
¿Alguien tiene un ejemplo de análisis de matrices de valores en una estructura más detallada?