module JSONPointer.Model
(
JSONPointer,
run,
atIndexOrKey,
)
where
import JSONPointer.Prelude hiding (or)
import qualified Data.Attoparsec.Text
import qualified Data.Text
newtype JSONPointer =
JSONPointer (forall m. Monoid m => (Maybe Int -> Text -> m) -> m)
instance Monoid JSONPointer where
mempty =
JSONPointer $ const mempty
mappend (JSONPointer fn1) (JSONPointer fn2) =
JSONPointer $ \handler -> fn1 handler <> fn2 handler
instance Show JSONPointer where
showsPrec _ (JSONPointer impl) =
appEndo $
impl (\_ text -> Endo (showString "/" . showString (Data.Text.unpack text)))
run :: Monoid m => JSONPointer -> (Maybe Int -> Text -> m) -> m
run (JSONPointer fn) =
fn
atIndexOrKey :: Maybe Int -> Text -> JSONPointer
atIndexOrKey index key =
JSONPointer $ \handler -> handler index key