I have no experience in Haskell. I'm trying to parse many .json
files to a data structure in Haskell using aeson. However, by reasons beyond my control, I need to store the name of the file from where the data was parsed as one of the fields in my data. A simple example of what I have so far is:
data Observation = Observation { id :: Integer
, value :: Integer
, filename :: String}
instance FromJSON Observation where
parseJson (Object v) =
Observation <$> (read <$> v .: "id")
<*> v .: "value"
<*> ????
My question is: what is a smart way to be able to serialize my data when parsing a json file having access to the name of the file?
What comes in my mind is to define another data
like NotNamedObservation
, initialize it and then having a function that converts NotNamedObservation -> String -> Observation
(where String is the filename) but that sounds like a very poor approach.
Thanks.
NotNamedObservation -> FilePath -> Observation
, then it's obvious what's going on. – Jordan Mackie