om-show: Utilities for showing string-like things.

[ library, mit, text ] [ Propose Tags ]

Utilities for showing string-like things.

[Skip to Readme]


[Index] [Quick Jump]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS],,
Dependencies aeson (>= && <2.3), base (>= && <4.20), text (>= && <2.2) [details]
License MIT
Copyright 2021 Owens Murray, LLC.
Author Rick Owens
Category Text
Home page
Uploaded by rickowens at 2023-11-22T04:29:20Z
Distributions NixOS:, Stackage:
Reverse Dependencies 6 direct, 1 indirect [details]
Downloads 200 total (10 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for om-show-

[back to package description]


Haskell utilities for turning values into string representations. It is mainly just a couple of very useful functions that eliminate a lot of conversion to from text types. It's so small that I can just post the entire source code in this README:

{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

{- | Utilities for showing string-like things. -}
module OM.Show (
) where

import Data.Aeson (encode, ToJSON)
import Data.String (IsString, fromString)
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TLE

{- | Like 'show', but for any string-like thing. -}
showt :: (Show a, IsString b) => a -> b
showt = fromString . show

{- |
  Show the JSON representation as any kind of string-like thing.
  Primarily useful for dumping JSON values into log messages without having to
  jump through too many hoops.
showj :: (ToJSON a, IsString b) => a -> b
showj = fromString . TL.unpack . TLE.decodeUtf8 . encode

{- |
  Wrapper whose 'Show' instance outputs JSON.

  Especially useful with `-XDerivingVia`

  > newtype Foo = Foo SomeType
  >   deriving Show via (ShowJ SomeType)

  This will cause @show (foo :: Foo) to output the JSON representation
  of SomeType.
newtype ShowJ a = ShowJ a
  deriving stock (Eq, Ord)
  deriving newtype (ToJSON)
instance (ToJSON a) => Show (ShowJ a) where
  show = showj