Skip to content

Commit 77c8665

Browse files
committed
Add ToValue typeclass and construction helpers
1 parent fb9698d commit 77c8665

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Revision history for json-syntax
22

3+
## 0.2.4.0 -- 2023-??-??
4+
5+
* Add typeclass `ToValue` for encoding.
6+
* Add functions `int`, `(int|word)(8|16|32|64)`, `bool` for constructing
7+
objects.
8+
39
## 0.2.3.0 -- 2022-03-22
410

511
* Add `Json.Flatten` module.

json-syntax.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.2
22
name: json-syntax
3-
version: 0.2.3.0
3+
version: 0.2.4.0
44
synopsis: High-performance JSON parser and encoder
55
description:
66
This library parses JSON into a @Value@ type that is consistent with the
@@ -42,7 +42,7 @@ library
4242
, contiguous >=0.6 && <0.7
4343
, primitive >=0.7 && <0.10
4444
, run-st >=0.1.1 && <0.2
45-
, scientific-notation >=0.1.5 && <0.2
45+
, scientific-notation >=0.1.6 && <0.2
4646
, text-short >=0.1.3 && <0.2
4747
, word-compat >=0.0.3
4848
, zigzag >=0.0.1

src/Json.hs

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
5063
import Data.Bytes.Types (Bytes(..))
5164
import Data.Char (ord)
5265
import Data.Number.Scientific (Scientific)
53-
import Data.Primitive (ByteArray,MutableByteArray,SmallArray)
66+
import Data.Primitive (ByteArray,MutableByteArray,SmallArray,Array,PrimArray,Prim)
5467
import Data.Text.Short (ShortText)
5568
import 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

5872
import qualified Prelude
5973
import qualified Data.Builder.ST as B
@@ -63,6 +77,7 @@ import qualified Data.Chunks as Chunks
6377
import qualified Data.Text.Short.Unsafe as TS
6478
import qualified Data.Number.Scientific as SCI
6579
import qualified Data.Primitive as PM
80+
import qualified Data.Primitive.Contiguous as Contiguous
6681
import qualified Data.Bytes.Parser.Utf8 as Utf8
6782
import qualified Data.Bytes.Parser.Latin as Latin
6883
import 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

Comments
 (0)