'Print structure of a data type by its type

I want to parse a JSON file into a data type and print an error in case of parsing error along with the expected data structure.

Let's take an example data record:

data Foo = {
  bar :: String
}

I want to print the structure of this data into console when it fails to parse, to show the user what structure the JSON should be. Also, I don't want to hardcode the structure into the error log, to make it typesafe (in a meaning: when I add a new field to the data, it should be printed in case of parse error, too. When it would be hardcoded I would forget to modify the string about the structure to be printed).

It can be easily printed to console when I have a value of this data by simply having the deriving Show on the Foo. But how to print the structure without a value?

It is enough if I would be able to print the type definition literally. But if you have a nice type safe solution that could be nicely formatted it is surely welcome :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source