Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Language/JsonGrammar/Grammar.hs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@ liftAeson = Pure f g
prop :: Json a => Text -> Grammar Obj t (a :- t)
prop n = Property n grammar

-- | Like 'prop', but for optional properties
optProp :: Json a => Text -> Grammar Obj t (Maybe a :- t)
optProp propName = just `mappend` nothing
where
just :: Json a => Grammar Obj t (Maybe a :- t)
just = pure fr to . prop propName
where
fr :: (a :- t) -> Parser (Maybe a :- t)
fr (a :- t) = return (Just a :- t)

to :: (Maybe a :- t) -> Maybe (a :- t)
to (Just a :- t) = Just (a :- t)
to (Nothing :- _) = Nothing

nothing :: Grammar Obj t (Maybe a :- t)
nothing = pure fr to
where
fr :: t -> Parser (Maybe a :- t)
fr t = return (Nothing :- t)

to :: (Maybe a :- t) -> Maybe t
to (Nothing :- t) = Just t
to (Just _ :- _) = Nothing

-- | Expect or produce an array 'element' whose value grammar is specified by 'grammar'.
el :: Json a => Grammar Arr t (a :- t)
el = Element grammar
Expand Down