diff --git a/src/Data/Aeson/Encode/Pretty/ToEncoding.hs b/src/Data/Aeson/Encode/Pretty/ToEncoding.hs index bd9f1e6..953b8e8 100644 --- a/src/Data/Aeson/Encode/Pretty/ToEncoding.hs +++ b/src/Data/Aeson/Encode/Pretty/ToEncoding.hs @@ -24,21 +24,25 @@ where import Data.Aeson (ToJSON, Value, encode, decode) import Data.ByteString.Lazy (ByteString) -import Data.Maybe (fromJust) import Data.Text.Lazy (Text) -import Data.Text.Lazy.Builder (Builder, fromLazyText) -import Data.Text.Lazy.Encoding (decodeUtf8) +import Data.Text.Lazy.Builder (Builder, toLazyText) +import Data.Text.Lazy.Encoding (encodeUtf8) -import qualified Data.Aeson.Encode.Pretty as P (encodePretty) +import qualified Data.Aeson.Encode.Pretty as P encodePretty :: ToJSON a => a -> ByteString -encodePretty = P.encodePretty . fromJust . decodeValue . encode - where - decodeValue :: ByteString -> Maybe Value - decodeValue = decode +encodePretty = encodeUtf8 . encodePrettyToLazyText encodePrettyToLazyText :: ToJSON a => a -> Text -encodePrettyToLazyText = decodeUtf8 . encodePretty +encodePrettyToLazyText = toLazyText . encodePrettyToTextBuilder encodePrettyToTextBuilder :: ToJSON a => a -> Builder -encodePrettyToTextBuilder = fromLazyText . encodePrettyToLazyText +encodePrettyToTextBuilder = + P.encodePrettyToTextBuilder . decodeValue . encode + where + decodeValue :: ByteString -> Value + decodeValue b = + case decode b of + Nothing -> + error "encodePretty: Failed to decode encoded JSON into Value" + Just v -> v