From 90cb4fcd88ab66a614ecda0aec054b3c4a3e7316 Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Sat, 29 Jun 2019 22:39:42 +0000 Subject: [PATCH] Data.Aeson.Encode.Pretty.ToEncoding: Use encodePrettyToTextBuilder as base aeson-pretty implements by formatting using a text Builder, and the ByteString is encoded from that. So instead of decoding the ByteString to produce Text or Builder, use the Builder as the starting point, to match how aeson-pretty works and save computation and weird backwards-decoding stuff. --- src/Data/Aeson/Encode/Pretty/ToEncoding.hs | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) 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