From 135c0707f69a03219a9f6906dd8a3006b0165e8b Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Thu, 5 Oct 2023 13:46:48 -0400 Subject: [PATCH] Add various function for encoding to things other than builder --- CHANGELOG.md | 3 ++- json-syntax.cabal | 2 +- src/Json.hs | 30 +++++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adea336..1926a51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ # Revision history for json-syntax -## 0.2.7.0 -- 2023-??-?? +## 0.2.7.0 -- 2023-10-05 * Add `decodeNewlineDelimited`. +* Add `toChunks`, `toBytes`, `toText`, `toShortText`, `toByteArray`. ## 0.2.6.1 -- 2023-07-28 diff --git a/json-syntax.cabal b/json-syntax.cabal index bb5ad6f..e597ccb 100644 --- a/json-syntax.cabal +++ b/json-syntax.cabal @@ -43,7 +43,7 @@ library , primitive >=0.7 && <0.10 , run-st >=0.1.1 && <0.2 , scientific-notation >=0.1.6 && <0.2 - , text >=1.2 + , text >=2.0 , text-short >=0.1.3 && <0.2 , transformers >=0.5.6.2 , word-compat >=0.0.3 diff --git a/src/Json.hs b/src/Json.hs index d08752f..0c83153 100644 --- a/src/Json.hs +++ b/src/Json.hs @@ -22,6 +22,11 @@ module Json , decode , decodeNewlineDelimited , encode + , toChunks + , toShortText + , toText + , toBytes + , toByteArray -- * Infix Synonyms , pattern (:->) -- * Constants @@ -73,7 +78,7 @@ import Data.Bytes.Parser (Parser) import Data.Bytes.Types (Bytes(..)) import Data.Char (ord) import Data.Number.Scientific (Scientific) -import Data.Primitive (ByteArray,MutableByteArray,SmallArray,Array,PrimArray,Prim) +import Data.Primitive (ByteArray(ByteArray),MutableByteArray,SmallArray,Array,PrimArray,Prim) import Data.Text.Short (ShortText) import GHC.Exts (Char(C#),Int(I#),gtWord#,ltWord#,word2Int#,chr#) import GHC.Word (Word8,Word16,Word32,Word64) @@ -82,10 +87,12 @@ import Data.Text (Text) import Data.Foldable (foldlM) import Control.Monad.Trans.Except (runExceptT,except) import Control.Monad.Trans.Class (lift) +import Data.Bytes.Chunks (Chunks) import qualified Prelude import qualified Data.Builder.ST as B import qualified Data.Bytes as Bytes +import qualified Data.Bytes.Chunks as ByteChunks import qualified Data.Bytes.Builder as BLDR import qualified Data.Bytes.Parser as P import qualified Data.Chunks as Chunks @@ -229,6 +236,27 @@ decodeNewlineDelimited !everything = dst' <- lift $ PM.unsafeFreezeSmallArray dst pure dst' +toChunks :: Value -> Chunks +{-# inline toChunks #-} +toChunks = BLDR.run 512 . encode + +toBytes :: Value -> Bytes +{-# inline toBytes #-} +toBytes = ByteChunks.concat . toChunks + +toByteArray :: Value -> ByteArray +{-# inline toByteArray #-} +toByteArray = ByteChunks.concatU . toChunks + +toShortText :: Value -> ShortText +{-# inline toShortText #-} +toShortText v = case toByteArray v of + ByteArray x -> TS.fromShortByteStringUnsafe (BSS.SBS x) + +toText :: Value -> Text +{-# inline toText #-} +toText = TS.toText . toShortText + -- | Encode a JSON syntax tree. encode :: Value -> BLDR.Builder {-# noinline encode #-}