aeson-yak 0.1.1.2 → 0.1.1.3
raw patch · 3 files changed
+48/−45 lines, 3 files
Files
- CHANGELOG +2/−0
- aeson-yak.cabal +1/−1
- src/Data/Aeson/Yak.hs +45/−44
CHANGELOG view
@@ -1,3 +1,5 @@+0.1.1.3: Fix haddocks+ 0.1.1.2: Eliminate upper bounds on Aeson 0.1.1.1: Bump upper bounds on Aeson
aeson-yak.cabal view
@@ -1,5 +1,5 @@ name: aeson-yak-version: 0.1.1.2+version: 0.1.1.3 synopsis: Handle JSON that may or may not be a list, or exist description: According to the standard promoted by Schema.org, the same JSON object
src/Data/Aeson/Yak.hs view
@@ -1,44 +1,45 @@--- |--- 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"]--- "{\"yak\":[\"foo\",\"bar\"]}"--- >>> encode $ Hideous ["baz"]--- "{\"yak\":\"baz\"}"--- >>> encode $ Hideous []--- "{\"yak\":null}"--- >>> 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"]--- "{\"yak\":[\"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.+{-|+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"]+"{\"yak\":[\"foo\",\"bar\"]}"+>>> encode $ Hideous ["baz"]+"{\"yak\":\"baz\"}"+>>> encode $ Hideous []+"{\"yak\":null}"+>>> 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"]+"{\"yak\":[\"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.+-} module Data.Aeson.Yak ( Yak , hairy@@ -50,7 +51,7 @@ -- | 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.)/ type Yak a = Maybe (Lousy a)@@ -64,7 +65,7 @@ -- | Convert a 'Yak' to a list. Relax, and allow yourself to breathe. shave :: Yak a -> [a]-shave Nothing = []+shave Nothing = [] shave (Just nit) = pick nit newtype Lousy a = Lousy { pick :: [a] } deriving (Eq)@@ -78,4 +79,4 @@ instance (FromJSON a) => FromJSON (Lousy a) where parseJSON vs@(Array _) = Lousy <$> parseJSON vs- parseJSON v = (\a -> Lousy [a]) <$> parseJSON v+ parseJSON v = (\a -> Lousy [a]) <$> parseJSON v