I'm parsing a third party JSON structure into my own set of types. I'd like to parse in the most efficient way possible (I'm parsing data sent over a unix socket with Network.Socket)
Aeson's documentation claims that parsing with toEncoding yields up to a 3x speedup over toJSON, however I don't understand how to write a valid instance using toEncoding for my simple sum type.
for example:
data NodeLayout =
SplitHorizontalLayout
| SplitVerticalLayout
| StackedLayout
| TabbedLayout
| DockAreaLayout
| OutputLayout
deriving (Eq, Generic)
instance ToJSON NodeLayout where
toJSON = \case
SplitHorizontalLayout -> "splith"
SplitVerticalLayout -> "splitv"
StackedLayout -> "stacked"
TabbedLayout -> "tabbed"
DockAreaLayout -> "dockarea"
OutputLayout -> "output"
instance FromJSON NodeLayout where
parseJSON (String s) = pure $ case s of
"splith" -> SplitHorizontalLayout
"splitv" -> SplitVerticalLayout
"stacked" -> StackedLayout
"tabbed" -> TabbedLayout
"dockarea" -> DockAreaLayout
"output" -> OutputLayout
_ -> error "Received unrecognized NodeLayout"
parseJSON _ = error "Error parsing NodeLayout"
I have other data types that will receive Vectors of this, but sometimes I receive a single value that needs to be parsed individually. How does one efficiently parse strings into sum types using toEncoding?