<div dir="ltr"><div><div><div><div><div><div><div><div><div>Dear Cafe,<br><br></div>I need help parsing YAML files with varying structure. The minimal example is: a yaml file that has one key, which can hold either one key-value pair or two key value pairs.  I have data types for the one key, and the two possible sub-keys:<br><br>>data Var = Var {<br>>  v' :: Either MyList Item<br>>  } deriving Show<br>><br>>data MyList = MyList {<br>>  a' :: Int,<br>>  b' :: Int<br>>  } deriving Show<br>><br>>data Item = Item {<br>>  content' :: String<br>>  } deriving Show<br><br></div><div>To read MyList and Item from the file I wrote FromJSON instances<br></div><br>>instance FromJSON MyList where<br>>  parseJSON (Object m) = MyList <$> m .: (pack "a") <*> m .: (pack "b")<br>>  parseJSON  x = fail ("not an object: " ++ show x)<br>><br>>instance FromJSON Item where<br>>  parseJSON (Object m) = Item <$> m .: (pack "c")<br>>  parseJSON x = fail ("not an object: " ++ show x)<br><br>I also have read functions for MyList and Item:<br><br>>readItem :: IO Item<br>>readItem = either (error.show) id <$> decodeFileEither "test.yaml"<br>><br>>readMyList :: IO MyList<br>>readMyList = either (error.show) id <$> decodeFileEither "test.yaml"<br><br></div>The file test.yaml looks like<br><br>>v:<br>>  a: 4<br>>  b: 5<br><br></div>or, alternatively<br><br></div>>v:<br></div>>  c: "test"<br><br></div>The question is how I can decode test.yaml to get Var. Trying to code readVar like readItem (having FromJSON Var like FromJSON Item) failed. For convenience I attached the relevant source files.<br><br></div>Regards,<br></div>Johannes.<br></div>