{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}

module Data.Aeson.Encoding.Internal
    (
    -- * Encoding
      Encoding' (..)
    , Encoding
    , encodingToLazyByteString
    , unsafeToEncoding
    , retagEncoding
    , Series (..)
    , pairs
    , pair
    , pairStr
    , pair'
    -- * Predicates
    , nullEncoding
    -- * Encoding constructors
    , emptyArray_
    , emptyObject_
    , wrapObject
    , wrapArray
    , null_
    , bool
    , text
    , lazyText
    , string
    , list
    , dict
    , tuple
    , (>*<)
    , InArray
    , empty
    , (><)
    , econcat
    -- ** Decimal numbers
    , int8, int16, int32, int64, int
    , word8, word16, word32, word64, word
    , integer, float, double, scientific
    -- ** Decimal numbers as Text
    , int8Text, int16Text, int32Text, int64Text, intText
    , word8Text, word16Text, word32Text, word64Text, wordText
    , integerText, floatText, doubleText, scientificText
    -- ** Time
    , day
    , localTime
    , utcTime
    , timeOfDay
    , zonedTime
    -- ** value
    , value
    -- ** JSON tokens
    , comma, colon, openBracket, closeBracket, openCurly, closeCurly
    ) where

import Prelude.Compat

import Data.Aeson.Types.Internal (Value)
import Data.ByteString.Builder (Builder, char7, toLazyByteString)
import Data.Int
import Data.Scientific (Scientific)
import Data.Semigroup (Semigroup ((<>)))
import Data.Text (Text)
import Data.Time (Day, LocalTime, TimeOfDay, UTCTime, ZonedTime)
import Data.Typeable (Typeable)
import Data.Word
import qualified Data.Aeson.Encoding.Builder as EB
import qualified Data.ByteString.Builder as B
import qualified Data.ByteString.Lazy as BSL
import qualified Data.Text.Lazy as LT

-- | An encoding of a JSON value.
--
-- @tag@ represents which kind of JSON the Encoding is encoding to,
-- we reuse 'Text' and 'Value' as tags here.
newtype Encoding' tag = Encoding {
      Encoding' tag -> Builder
fromEncoding :: Builder
      -- ^ Acquire the underlying bytestring builder.
    } deriving (Typeable)

-- | Often used synonym for 'Encoding''.
type Encoding = Encoding' Value

-- | Make Encoding from Builder.
--
-- Use with care! You have to make sure that the passed Builder
-- is a valid JSON Encoding!
unsafeToEncoding :: Builder -> Encoding' a
unsafeToEncoding :: Builder -> Encoding' a
unsafeToEncoding = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding

encodingToLazyByteString :: Encoding' a -> BSL.ByteString
encodingToLazyByteString :: Encoding' a -> ByteString
encodingToLazyByteString = Builder -> ByteString
toLazyByteString (Builder -> ByteString)
-> (Encoding' a -> Builder) -> Encoding' a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding' a -> Builder
forall tag. Encoding' tag -> Builder
fromEncoding
{-# INLINE encodingToLazyByteString #-}

retagEncoding :: Encoding' a -> Encoding' b
retagEncoding :: Encoding' a -> Encoding' b
retagEncoding = Builder -> Encoding' b
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' b)
-> (Encoding' a -> Builder) -> Encoding' a -> Encoding' b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding' a -> Builder
forall tag. Encoding' tag -> Builder
fromEncoding

-------------------------------------------------------------------------------
-- Encoding instances
-------------------------------------------------------------------------------

instance Show (Encoding' a) where
    show :: Encoding' a -> String
show (Encoding e :: Builder
e) = ByteString -> String
forall a. Show a => a -> String
show (Builder -> ByteString
toLazyByteString Builder
e)

instance Eq (Encoding' a) where
    Encoding a :: Builder
a == :: Encoding' a -> Encoding' a -> Bool
== Encoding b :: Builder
b = Builder -> ByteString
toLazyByteString Builder
a ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== Builder -> ByteString
toLazyByteString Builder
b

instance Ord (Encoding' a) where
    compare :: Encoding' a -> Encoding' a -> Ordering
compare (Encoding a :: Builder
a) (Encoding b :: Builder
b) =
      ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Builder -> ByteString
toLazyByteString Builder
a) (Builder -> ByteString
toLazyByteString Builder
b)

-- | A series of values that, when encoded, should be separated by
-- commas. Since 0.11.0.0, the '.=' operator is overloaded to create
-- either @(Text, Value)@ or 'Series'. You can use Series when
-- encoding directly to a bytestring builder as in the following
-- example:
--
-- > toEncoding (Person name age) = pairs ("name" .= name <> "age" .= age)
data Series = Empty
            | Value (Encoding' Series)
            deriving (Typeable)

pair :: Text -> Encoding -> Series
pair :: Text -> Encoding -> Series
pair name :: Text
name val :: Encoding
val = Encoding' Text -> Encoding -> Series
pair' (Text -> Encoding' Text
forall a. Text -> Encoding' a
text Text
name) Encoding
val
{-# INLINE pair #-}

pairStr :: String -> Encoding -> Series
pairStr :: String -> Encoding -> Series
pairStr name :: String
name val :: Encoding
val = Encoding' Text -> Encoding -> Series
pair' (String -> Encoding' Text
forall a. String -> Encoding' a
string String
name) Encoding
val
{-# INLINE pairStr #-}

pair' :: Encoding' Text -> Encoding -> Series
pair' :: Encoding' Text -> Encoding -> Series
pair' name :: Encoding' Text
name val :: Encoding
val = Encoding' Series -> Series
Value (Encoding' Series -> Series) -> Encoding' Series -> Series
forall a b. (a -> b) -> a -> b
$ Encoding -> Encoding' Series
forall a b. Encoding' a -> Encoding' b
retagEncoding (Encoding -> Encoding' Series) -> Encoding -> Encoding' Series
forall a b. (a -> b) -> a -> b
$ Encoding' Text -> Encoding
forall a b. Encoding' a -> Encoding' b
retagEncoding Encoding' Text
name Encoding -> Encoding -> Encoding
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding
forall a. Encoding' a
colon Encoding -> Encoding -> Encoding
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding
val

instance Semigroup Series where
    Empty   <> :: Series -> Series -> Series
<> a :: Series
a       = Series
a
    a :: Series
a       <> Empty   = Series
a
    Value a :: Encoding' Series
a <> Value b :: Encoding' Series
b = Encoding' Series -> Series
Value (Encoding' Series
a Encoding' Series -> Encoding' Series -> Encoding' Series
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' Series
forall a. Encoding' a
comma Encoding' Series -> Encoding' Series -> Encoding' Series
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' Series
b)

instance Monoid Series where
    mempty :: Series
mempty  = Series
Empty
    mappend :: Series -> Series -> Series
mappend = Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
(<>)

nullEncoding :: Encoding' a -> Bool
nullEncoding :: Encoding' a -> Bool
nullEncoding = ByteString -> Bool
BSL.null (ByteString -> Bool)
-> (Encoding' a -> ByteString) -> Encoding' a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
toLazyByteString (Builder -> ByteString)
-> (Encoding' a -> Builder) -> Encoding' a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding' a -> Builder
forall tag. Encoding' tag -> Builder
fromEncoding

emptyArray_ :: Encoding
emptyArray_ :: Encoding
emptyArray_ = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding Builder
EB.emptyArray_

emptyObject_ :: Encoding
emptyObject_ :: Encoding
emptyObject_ = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding Builder
EB.emptyObject_

wrapArray :: Encoding' a -> Encoding
wrapArray :: Encoding' a -> Encoding
wrapArray e :: Encoding' a
e = Encoding' a -> Encoding
forall a b. Encoding' a -> Encoding' b
retagEncoding (Encoding' a -> Encoding) -> Encoding' a -> Encoding
forall a b. (a -> b) -> a -> b
$ Encoding' a
forall a. Encoding' a
openBracket Encoding' a -> Encoding' a -> Encoding' a
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' a
e Encoding' a -> Encoding' a -> Encoding' a
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' a
forall a. Encoding' a
closeBracket

wrapObject :: Encoding' a -> Encoding
wrapObject :: Encoding' a -> Encoding
wrapObject e :: Encoding' a
e = Encoding' a -> Encoding
forall a b. Encoding' a -> Encoding' b
retagEncoding (Encoding' a -> Encoding) -> Encoding' a -> Encoding
forall a b. (a -> b) -> a -> b
$ Encoding' a
forall a. Encoding' a
openCurly Encoding' a -> Encoding' a -> Encoding' a
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' a
e Encoding' a -> Encoding' a -> Encoding' a
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' a
forall a. Encoding' a
closeCurly

null_ :: Encoding
null_ :: Encoding
null_ = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding Builder
EB.null_

bool :: Bool -> Encoding
bool :: Bool -> Encoding
bool True = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding "true"
bool False = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding "false"

-- | Encode a series of key/value pairs, separated by commas.
pairs :: Series -> Encoding
pairs :: Series -> Encoding
pairs (Value v :: Encoding' Series
v) = Encoding
forall a. Encoding' a
openCurly Encoding -> Encoding -> Encoding
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' Series -> Encoding
forall a b. Encoding' a -> Encoding' b
retagEncoding Encoding' Series
v Encoding -> Encoding -> Encoding
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding
forall a. Encoding' a
closeCurly
pairs Empty     = Encoding
emptyObject_
{-# INLINE pairs #-}

list :: (a -> Encoding) -> [a] -> Encoding
list :: (a -> Encoding) -> [a] -> Encoding
list _  []     = Encoding
emptyArray_
list to' :: a -> Encoding
to' (x :: a
x:xs :: [a]
xs) = Encoding
forall a. Encoding' a
openBracket Encoding -> Encoding -> Encoding
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< a -> Encoding
to' a
x Encoding -> Encoding -> Encoding
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< [a] -> Encoding
commas [a]
xs Encoding -> Encoding -> Encoding
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding
forall a. Encoding' a
closeBracket
  where
    commas :: [a] -> Encoding
commas = (a -> Encoding -> Encoding) -> Encoding -> [a] -> Encoding
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\v :: a
v vs :: Encoding
vs -> Encoding
forall a. Encoding' a
comma Encoding -> Encoding -> Encoding
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< a -> Encoding
to' a
v Encoding -> Encoding -> Encoding
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding
vs) Encoding
forall a. Encoding' a
empty
{-# INLINE list #-}

-- | Encode as JSON object
dict
    :: (k -> Encoding' Text)                     -- ^ key encoding
    -> (v -> Encoding)                                -- ^ value encoding
    -> (forall a. (k -> v -> a -> a) -> a -> m -> a)  -- ^ @foldrWithKey@ - indexed fold
    -> m                                              -- ^ container
    -> Encoding
dict :: (k -> Encoding' Text)
-> (v -> Encoding)
-> (forall a. (k -> v -> a -> a) -> a -> m -> a)
-> m
-> Encoding
dict encodeKey :: k -> Encoding' Text
encodeKey encodeVal :: v -> Encoding
encodeVal foldrWithKey :: forall a. (k -> v -> a -> a) -> a -> m -> a
foldrWithKey = Series -> Encoding
pairs (Series -> Encoding) -> (m -> Series) -> m -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k -> v -> Series -> Series) -> Series -> m -> Series
forall a. (k -> v -> a -> a) -> a -> m -> a
foldrWithKey k -> v -> Series -> Series
go Series
forall a. Monoid a => a
mempty
  where
    go :: k -> v -> Series -> Series
go k :: k
k v :: v
v c :: Series
c = Encoding' Series -> Series
Value (k -> v -> Encoding' Series
forall a. k -> v -> Encoding' a
encodeKV k
k v
v) Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Series
c
    encodeKV :: k -> v -> Encoding' a
encodeKV k :: k
k v :: v
v = Encoding' Text -> Encoding' a
forall a b. Encoding' a -> Encoding' b
retagEncoding (k -> Encoding' Text
encodeKey k
k) Encoding' a -> Encoding' a -> Encoding' a
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' a
forall a. Encoding' a
colon Encoding' a -> Encoding' a -> Encoding' a
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding -> Encoding' a
forall a b. Encoding' a -> Encoding' b
retagEncoding (v -> Encoding
encodeVal v
v)
{-# INLINE dict #-}

-- | Type tag for tuples contents, see 'tuple'.
data InArray

infixr 6 >*<
-- | See 'tuple'.
(>*<) :: Encoding' a -> Encoding' b -> Encoding' InArray
a :: Encoding' a
a >*< :: Encoding' a -> Encoding' b -> Encoding' InArray
>*< b :: Encoding' b
b = Encoding' a -> Encoding' InArray
forall a b. Encoding' a -> Encoding' b
retagEncoding Encoding' a
a Encoding' InArray -> Encoding' InArray -> Encoding' InArray
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' InArray
forall a. Encoding' a
comma Encoding' InArray -> Encoding' InArray -> Encoding' InArray
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' b -> Encoding' InArray
forall a b. Encoding' a -> Encoding' b
retagEncoding Encoding' b
b
{-# INLINE (>*<) #-}

empty :: Encoding' a
empty :: Encoding' a
empty = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding Builder
forall a. Monoid a => a
mempty

econcat :: [Encoding' a] -> Encoding' a
econcat :: [Encoding' a] -> Encoding' a
econcat = (Encoding' a -> Encoding' a -> Encoding' a)
-> Encoding' a -> [Encoding' a] -> Encoding' a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Encoding' a -> Encoding' a -> Encoding' a
forall a. Encoding' a -> Encoding' a -> Encoding' a
(><) Encoding' a
forall a. Encoding' a
empty

infixr 6 ><
(><) :: Encoding' a -> Encoding' a -> Encoding' a
Encoding a :: Builder
a >< :: Encoding' a -> Encoding' a -> Encoding' a
>< Encoding b :: Builder
b = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder
a Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
b)
{-# INLINE (><) #-}

-- | Encode as a tuple.
--
-- @
-- toEncoding (X a b c) = tuple $
--     toEncoding a >*<
--     toEncoding b >*<
--     toEncoding c
tuple :: Encoding' InArray -> Encoding
tuple :: Encoding' InArray -> Encoding
tuple b :: Encoding' InArray
b = Encoding' InArray -> Encoding
forall a b. Encoding' a -> Encoding' b
retagEncoding (Encoding' InArray -> Encoding) -> Encoding' InArray -> Encoding
forall a b. (a -> b) -> a -> b
$ Encoding' InArray
forall a. Encoding' a
openBracket Encoding' InArray -> Encoding' InArray -> Encoding' InArray
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' InArray
b Encoding' InArray -> Encoding' InArray -> Encoding' InArray
forall a. Encoding' a -> Encoding' a -> Encoding' a
>< Encoding' InArray
forall a. Encoding' a
closeBracket
{-# INLINE tuple #-}

text :: Text -> Encoding' a
text :: Text -> Encoding' a
text = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Text -> Builder) -> Text -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
EB.text

lazyText :: LT.Text -> Encoding' a
lazyText :: Text -> Encoding' a
lazyText t :: Text
t = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a) -> Builder -> Encoding' a
forall a b. (a -> b) -> a -> b
$
    Char -> Builder
B.char7 '"' Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
    (Text -> Builder -> Builder) -> Builder -> Text -> Builder
forall a. (Text -> a -> a) -> a -> Text -> a
LT.foldrChunks (\x :: Text
x xs :: Builder
xs -> Text -> Builder
EB.unquoted Text
x Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
xs) (Char -> Builder
B.char7 '"') Text
t

string :: String -> Encoding' a
string :: String -> Encoding' a
string = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (String -> Builder) -> String -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Builder
EB.string

-------------------------------------------------------------------------------
-- chars
-------------------------------------------------------------------------------

comma, colon, openBracket, closeBracket, openCurly, closeCurly :: Encoding' a
comma :: Encoding' a
comma        = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a) -> Builder -> Encoding' a
forall a b. (a -> b) -> a -> b
$ Char -> Builder
char7 ','
colon :: Encoding' a
colon        = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a) -> Builder -> Encoding' a
forall a b. (a -> b) -> a -> b
$ Char -> Builder
char7 ':'
openBracket :: Encoding' a
openBracket  = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a) -> Builder -> Encoding' a
forall a b. (a -> b) -> a -> b
$ Char -> Builder
char7 '['
closeBracket :: Encoding' a
closeBracket = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a) -> Builder -> Encoding' a
forall a b. (a -> b) -> a -> b
$ Char -> Builder
char7 ']'
openCurly :: Encoding' a
openCurly    = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a) -> Builder -> Encoding' a
forall a b. (a -> b) -> a -> b
$ Char -> Builder
char7 '{'
closeCurly :: Encoding' a
closeCurly   = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a) -> Builder -> Encoding' a
forall a b. (a -> b) -> a -> b
$ Char -> Builder
char7 '}'

-------------------------------------------------------------------------------
-- Decimal numbers
-------------------------------------------------------------------------------

int8 :: Int8 -> Encoding
int8 :: Int8 -> Encoding
int8 = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Int8 -> Builder) -> Int8 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> Builder
B.int8Dec

int16 :: Int16 -> Encoding
int16 :: Int16 -> Encoding
int16 = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Int16 -> Builder) -> Int16 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> Builder
B.int16Dec

int32 :: Int32 -> Encoding
int32 :: Int32 -> Encoding
int32 = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Int32 -> Builder) -> Int32 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Builder
B.int32Dec

int64 :: Int64 -> Encoding
int64 :: Int64 -> Encoding
int64 = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Int64 -> Builder) -> Int64 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Builder
B.int64Dec

int :: Int -> Encoding
int :: Int -> Encoding
int = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Int -> Builder) -> Int -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Builder
B.intDec

word8 :: Word8 -> Encoding
word8 :: Word8 -> Encoding
word8 = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Word8 -> Builder) -> Word8 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Builder
B.word8Dec

word16 :: Word16 -> Encoding
word16 :: Word16 -> Encoding
word16 = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Word16 -> Builder) -> Word16 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Builder
B.word16Dec

word32 :: Word32 -> Encoding
word32 :: Word32 -> Encoding
word32 = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Word32 -> Builder) -> Word32 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Builder
B.word32Dec

word64 :: Word64 -> Encoding
word64 :: Word64 -> Encoding
word64 = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Word64 -> Builder) -> Word64 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Builder
B.word64Dec

word :: Word -> Encoding
word :: Word -> Encoding
word = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Word -> Builder) -> Word -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Builder
B.wordDec

integer :: Integer -> Encoding
integer :: Integer -> Encoding
integer = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding)
-> (Integer -> Builder) -> Integer -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Builder
B.integerDec

float :: Float -> Encoding
float :: Float -> Encoding
float = (Float -> Encoding) -> Float -> Encoding
forall a. RealFloat a => (a -> Encoding) -> a -> Encoding
realFloatToEncoding ((Float -> Encoding) -> Float -> Encoding)
-> (Float -> Encoding) -> Float -> Encoding
forall a b. (a -> b) -> a -> b
$ Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Float -> Builder) -> Float -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Builder
B.floatDec

double :: Double -> Encoding
double :: Double -> Encoding
double = (Double -> Encoding) -> Double -> Encoding
forall a. RealFloat a => (a -> Encoding) -> a -> Encoding
realFloatToEncoding ((Double -> Encoding) -> Double -> Encoding)
-> (Double -> Encoding) -> Double -> Encoding
forall a b. (a -> b) -> a -> b
$ Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Double -> Builder) -> Double -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Builder
B.doubleDec

scientific :: Scientific -> Encoding
scientific :: Scientific -> Encoding
scientific = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding)
-> (Scientific -> Builder) -> Scientific -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scientific -> Builder
EB.scientific

realFloatToEncoding :: RealFloat a => (a -> Encoding) -> a -> Encoding
realFloatToEncoding :: (a -> Encoding) -> a -> Encoding
realFloatToEncoding e :: a -> Encoding
e d :: a
d
    | a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
d Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
d = Encoding
null_
    | Bool
otherwise               = a -> Encoding
e a
d
{-# INLINE realFloatToEncoding #-}

-------------------------------------------------------------------------------
-- Decimal numbers as Text
-------------------------------------------------------------------------------

int8Text :: Int8 -> Encoding' a
int8Text :: Int8 -> Encoding' a
int8Text = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Int8 -> Builder) -> Int8 -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Int8 -> Builder) -> Int8 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> Builder
B.int8Dec

int16Text :: Int16 -> Encoding' a
int16Text :: Int16 -> Encoding' a
int16Text = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Int16 -> Builder) -> Int16 -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Int16 -> Builder) -> Int16 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> Builder
B.int16Dec

int32Text :: Int32 -> Encoding' a
int32Text :: Int32 -> Encoding' a
int32Text = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Int32 -> Builder) -> Int32 -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Int32 -> Builder) -> Int32 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Builder
B.int32Dec

int64Text :: Int64 -> Encoding' a
int64Text :: Int64 -> Encoding' a
int64Text = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Int64 -> Builder) -> Int64 -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Int64 -> Builder) -> Int64 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Builder
B.int64Dec

intText :: Int -> Encoding' a
intText :: Int -> Encoding' a
intText = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a) -> (Int -> Builder) -> Int -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Int -> Builder) -> Int -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Builder
B.intDec

word8Text :: Word8 -> Encoding' a
word8Text :: Word8 -> Encoding' a
word8Text = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Word8 -> Builder) -> Word8 -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Word8 -> Builder) -> Word8 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Builder
B.word8Dec

word16Text :: Word16 -> Encoding' a
word16Text :: Word16 -> Encoding' a
word16Text = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Word16 -> Builder) -> Word16 -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Word16 -> Builder) -> Word16 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Builder
B.word16Dec

word32Text :: Word32 -> Encoding' a
word32Text :: Word32 -> Encoding' a
word32Text = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Word32 -> Builder) -> Word32 -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Word32 -> Builder) -> Word32 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Builder
B.word32Dec

word64Text :: Word64 -> Encoding' a
word64Text :: Word64 -> Encoding' a
word64Text = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Word64 -> Builder) -> Word64 -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Word64 -> Builder) -> Word64 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Builder
B.word64Dec

wordText :: Word -> Encoding' a
wordText :: Word -> Encoding' a
wordText = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Word -> Builder) -> Word -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Word -> Builder) -> Word -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Builder
B.wordDec

integerText :: Integer -> Encoding' a
integerText :: Integer -> Encoding' a
integerText = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Integer -> Builder) -> Integer -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Integer -> Builder) -> Integer -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Builder
B.integerDec

floatText :: Float -> Encoding' a
floatText :: Float -> Encoding' a
floatText = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Float -> Builder) -> Float -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Float -> Builder) -> Float -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Builder
B.floatDec

doubleText :: Double -> Encoding' a
doubleText :: Double -> Encoding' a
doubleText = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Double -> Builder) -> Double -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Double -> Builder) -> Double -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Builder
B.doubleDec

scientificText :: Scientific -> Encoding' a
scientificText :: Scientific -> Encoding' a
scientificText = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (Scientific -> Builder) -> Scientific -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder)
-> (Scientific -> Builder) -> Scientific -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scientific -> Builder
EB.scientific

-------------------------------------------------------------------------------
-- time
-------------------------------------------------------------------------------

day :: Day -> Encoding' a
day :: Day -> Encoding' a
day = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a) -> (Day -> Builder) -> Day -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (Day -> Builder) -> Day -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Day -> Builder
EB.day

localTime :: LocalTime -> Encoding' a
localTime :: LocalTime -> Encoding' a
localTime = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (LocalTime -> Builder) -> LocalTime -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder)
-> (LocalTime -> Builder) -> LocalTime -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LocalTime -> Builder
EB.localTime

utcTime :: UTCTime -> Encoding' a
utcTime :: UTCTime -> Encoding' a
utcTime = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (UTCTime -> Builder) -> UTCTime -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder) -> (UTCTime -> Builder) -> UTCTime -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> Builder
EB.utcTime

timeOfDay :: TimeOfDay -> Encoding' a
timeOfDay :: TimeOfDay -> Encoding' a
timeOfDay = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (TimeOfDay -> Builder) -> TimeOfDay -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder)
-> (TimeOfDay -> Builder) -> TimeOfDay -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeOfDay -> Builder
EB.timeOfDay

zonedTime :: ZonedTime -> Encoding' a
zonedTime :: ZonedTime -> Encoding' a
zonedTime = Builder -> Encoding' a
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding' a)
-> (ZonedTime -> Builder) -> ZonedTime -> Encoding' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
EB.quote (Builder -> Builder)
-> (ZonedTime -> Builder) -> ZonedTime -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ZonedTime -> Builder
EB.zonedTime

-------------------------------------------------------------------------------
-- Value
-------------------------------------------------------------------------------

value :: Value -> Encoding
value :: Value -> Encoding
value = Builder -> Encoding
forall tag. Builder -> Encoding' tag
Encoding (Builder -> Encoding) -> (Value -> Builder) -> Value -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Builder
EB.encodeToBuilder