@@ -16,6 +16,8 @@ module Json
1616 Value (.. )
1717 , Member (.. )
1818 , SyntaxException (.. )
19+ -- * Classes
20+ , ToValue (.. )
1921 -- * Functions
2022 , decode
2123 , encode
@@ -24,7 +26,18 @@ module Json
2426 -- * Constants
2527 , emptyArray
2628 , emptyObject
27- -- * Construction
29+ -- * Value Construction
30+ , int
31+ , int8
32+ , int16
33+ , int32
34+ , int64
35+ , word8
36+ , word16
37+ , word32
38+ , word64
39+ , bool
40+ -- * Object Construction
2841 , object1
2942 , object2
3043 , object3
@@ -50,10 +63,11 @@ import Data.Bytes.Parser (Parser)
5063import Data.Bytes.Types (Bytes (.. ))
5164import Data.Char (ord )
5265import Data.Number.Scientific (Scientific )
53- import Data.Primitive (ByteArray ,MutableByteArray ,SmallArray )
66+ import Data.Primitive (ByteArray ,MutableByteArray ,SmallArray , Array , PrimArray , Prim )
5467import Data.Text.Short (ShortText )
5568import GHC.Exts (Char (C #),Int (I #),gtWord #,ltWord #,word2Int #,chr #)
56- import GHC.Word (Word8 ,Word16 )
69+ import GHC.Word (Word8 ,Word16 ,Word32 ,Word64 )
70+ import GHC.Int (Int8 ,Int16 ,Int32 ,Int64 )
5771
5872import qualified Prelude
5973import qualified Data.Builder.ST as B
@@ -63,6 +77,7 @@ import qualified Data.Chunks as Chunks
6377import qualified Data.Text.Short.Unsafe as TS
6478import qualified Data.Number.Scientific as SCI
6579import qualified Data.Primitive as PM
80+ import qualified Data.Primitive.Contiguous as Contiguous
6681import qualified Data.Bytes.Parser.Utf8 as Utf8
6782import qualified Data.Bytes.Parser.Latin as Latin
6883import qualified Data.ByteString.Short.Internal as BSS
@@ -582,3 +597,68 @@ object12 a b c d e f g h i j k l = Object $ runSmallArrayST $ do
582597 PM. writeSmallArray dst 10 k
583598 PM. writeSmallArray dst 11 l
584599 PM. unsafeFreezeSmallArray dst
600+
601+ word8 :: Word8 -> Json. Value
602+ {-# inline word8 #-}
603+ word8 = Json. Number . SCI. fromWord8
604+
605+ word16 :: Word16 -> Json. Value
606+ {-# inline word16 #-}
607+ word16 = Json. Number . SCI. fromWord16
608+
609+ word32 :: Word32 -> Json. Value
610+ {-# inline word32 #-}
611+ word32 = Json. Number . SCI. fromWord32
612+
613+ word64 :: Word64 -> Json. Value
614+ {-# inline word64 #-}
615+ word64 = Json. Number . SCI. fromWord64
616+
617+ int8 :: Int8 -> Json. Value
618+ {-# inline int8 #-}
619+ int8 = Json. Number . SCI. fromInt8
620+
621+ int16 :: Int16 -> Json. Value
622+ {-# inline int16 #-}
623+ int16 = Json. Number . SCI. fromInt16
624+
625+ int32 :: Int32 -> Json. Value
626+ {-# inline int32 #-}
627+ int32 = Json. Number . SCI. fromInt32
628+
629+ int64 :: Int64 -> Json. Value
630+ {-# inline int64 #-}
631+ int64 = Json. Number . SCI. fromInt64
632+
633+ int :: Int -> Json. Value
634+ {-# inline int #-}
635+ int = Json. Number . SCI. fromInt
636+
637+ bool :: Prelude. Bool -> Json. Value
638+ {-# inline bool #-}
639+ bool Prelude. True = True
640+ bool _ = False
641+
642+ class ToValue a where
643+ toValue :: a -> Value
644+
645+ instance ToValue Int where {toValue = int}
646+ instance ToValue Int8 where {toValue = int8}
647+ instance ToValue Int16 where {toValue = int16}
648+ instance ToValue Int32 where {toValue = int32}
649+ instance ToValue Int64 where {toValue = int64}
650+ instance ToValue Word8 where {toValue = word8}
651+ instance ToValue Word16 where {toValue = word16}
652+ instance ToValue Word32 where {toValue = word32}
653+ instance ToValue Word64 where {toValue = word64}
654+ instance ToValue ShortText where {toValue = String }
655+ instance ToValue Prelude. Bool where {toValue = bool}
656+
657+ instance ToValue a => ToValue (SmallArray a ) where
658+ toValue ! xs = Json. Array $! Contiguous. map' toValue xs
659+
660+ instance ToValue a => ToValue (Array a ) where
661+ toValue ! xs = Json. Array $! Contiguous. map' toValue xs
662+
663+ instance (Prim a , ToValue a ) => ToValue (PrimArray a ) where
664+ toValue ! xs = Json. Array $! Contiguous. map' toValue xs
0 commit comments