Skip to content

Commit

Permalink
Add various function for encoding to things other than builder
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewthad committed Oct 5, 2023
1 parent 51de0e8 commit 135c070
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion json-syntax.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion src/Json.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ module Json
, decode
, decodeNewlineDelimited
, encode
, toChunks
, toShortText
, toText
, toBytes
, toByteArray
-- * Infix Synonyms
, pattern (:->)
-- * Constants
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 #-}
Expand Down

0 comments on commit 135c070

Please sign in to comment.