aeson-yak- Handle JSON that may or may not be a list, or exist

Safe HaskellNone



There are two ways to use this library: the way with extra boilerplate, or the way with a leaky abstraction. A truly good solution would fix it all with Template Haskell; but the author is not a truly good Haskeller, so this will have to do for now.

The boilerplate way:

data Hideous = Hideous { yak :: [String] }
instance ToJSON Hideous where
  toJSON x = object [ "yak" .= hairy (yak x) ]
instance FromJSON Hideous where
  parseJSON (Object o) = Hideous <$> (shave <$> o .:? "yak")
>>> encode $ Hideous ["foo","bar"]
>>> encode $ Hideous ["baz"]
>>> encode $ Hideous []
>>> yak <$> decode "{\"yak\":[\"foo\",\"bar\"]}"
Just ["foo", "bar"]
>>> yak <$> decode "{\"yak\":\"baz\"}"
Just ["baz"]
>>> yak <$> decode "{}"
Just []

The leaky way:

data Abhorrent = Abhorrent { yak :: Yak String }
$(deriveJSON defaultOptions{omitNothingFields=True} ''Abhorrent)
>>> encode . Abhorrent . hairy $ ["shaven","shorn","sheared"]
>>> shave . yak <$> decode "{}"
Just []

Which to prefer depends on how many yaks you need to deal with vs. how much you hate cleaning up yak droppings in the rest of your codebase.



type Yak a = Maybe (Lousy a) Source #

Data whose JSON representation may legally be an array, a single element, or null/absent. No, please, calm down. It'll be okay. Mostly.

(Lousy is not exposed to avoid namespace infestation. This is open for discussion if a use case can be shown.)

hairy :: Foldable f => f a -> Yak a Source #

Convert a Foldable to a Yak. Should probably be specific to lists, but what's life without a little adventure?

shave :: Yak a -> [a] Source #

Convert a Yak to a list. Relax, and allow yourself to breathe.