Skip to content

Commit f355239

Browse files
committed
Add arrayFromList. Add rewrite rules for objectFromList.
1 parent 15b04fe commit f355239

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

CHANGELOG.md

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

3+
## 0.2.6.0 -- 2023-07-26
4+
5+
* Add `objectFromList` and `arrayFromList`.
6+
37
## 0.2.5.0 -- 2023-07-25
48

59
* Add `object(13|14|15|16|17)`.

json-syntax.cabal

Lines changed: 1 addition & 1 deletion
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.5.0
3+
version: 0.2.6.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

scripts/buildjson.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Compile this with -02 to test that the rewrite rule for
2+
-- objectFromList works correctly.
3+
module Buildjson (tripleMember) where
4+
5+
import qualified Json
6+
7+
tripleMember :: Json.Member -> Json.Value
8+
{-# noinline tripleMember #-}
9+
tripleMember x = Json.objectFromList [x,x,x]

src/Json.hs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module Json
3939
, bool
4040
, text
4141
, shortText
42+
-- * Array Construction
43+
, arrayFromList
4244
-- * Object Construction
4345
, objectFromList
4446
, object1
@@ -454,10 +456,88 @@ w16ToChar (GHC.Word.Compat.W16# w) = C# (chr# (word2Int# w))
454456
pattern (:->) :: ShortText -> Value -> Member
455457
pattern key :-> value = Member{key,value}
456458

459+
-- | Construct a JSON array from a list of JSON values.
460+
--
461+
-- Unlike 'objectFromList', this is not currently equipped with a
462+
-- rewrite rule.
463+
arrayFromList :: [Value] -> Value
464+
arrayFromList ms = Array $ PM.smallArrayFromList ms
465+
457466
-- | Construct a JSON object from a list of members.
467+
--
468+
-- Note: When the argument is a list literal with 16 or fewer elements,
469+
-- a rewrite rule transforms this into the appropriate @objectN@ function.
470+
-- When the argument is not a list literal, this function just calls
471+
-- @smallArrayFromList@ on the members, which has poor performance.
458472
objectFromList :: [Member] -> Value
459473
objectFromList ms = Object $ PM.smallArrayFromList ms
460474

475+
{-# NOINLINE objectFromList #-}
476+
{-# RULES "objectFromList/1" forall a.
477+
objectFromList (a : []) =
478+
object1 a
479+
#-}
480+
{-# RULES "objectFromList/2" forall a b.
481+
objectFromList (a : b : []) =
482+
object2 a b
483+
#-}
484+
{-# RULES "objectFromList/3" forall a b c.
485+
objectFromList (a : b : c : []) =
486+
object3 a b c
487+
#-}
488+
{-# RULES "objectFromList/4" forall a b c d.
489+
objectFromList (a : b : c : d : []) =
490+
object4 a b c d
491+
#-}
492+
{-# RULES "objectFromList/5" forall a b c d e.
493+
objectFromList (a : b : c : d : e : []) =
494+
object5 a b c d e
495+
#-}
496+
{-# RULES "objectFromList/6" forall a b c d e f.
497+
objectFromList (a : b : c : d : e : f : []) =
498+
object6 a b c d e f
499+
#-}
500+
{-# RULES "objectFromList/7" forall a b c d e f g.
501+
objectFromList (a : b : c : d : e : f : g : []) =
502+
object7 a b c d e f g
503+
#-}
504+
{-# RULES "objectFromList/8" forall a b c d e f g h.
505+
objectFromList (a : b : c : d : e : f : g : h : []) =
506+
object8 a b c d e f g h
507+
#-}
508+
{-# RULES "objectFromList/9" forall a b c d e f g h i.
509+
objectFromList (a : b : c : d : e : f : g : h : i : []) =
510+
object9 a b c d e f g h i
511+
#-}
512+
{-# RULES "objectFromList/10" forall a b c d e f g h i j.
513+
objectFromList (a : b : c : d : e : f : g : h : i : j : []) =
514+
object10 a b c d e f g h i j
515+
#-}
516+
{-# RULES "objectFromList/11" forall a b c d e f g h i j k.
517+
objectFromList (a : b : c : d : e : f : g : h : i : j : k : []) =
518+
object11 a b c d e f g h i j k
519+
#-}
520+
{-# RULES "objectFromList/12" forall a b c d e f g h i j k l.
521+
objectFromList (a : b : c : d : e : f : g : h : i : j : k : l : []) =
522+
object12 a b c d e f g h i j k l
523+
#-}
524+
{-# RULES "objectFromList/13" forall a b c d e f g h i j k l m.
525+
objectFromList (a : b : c : d : e : f : g : h : i : j : k : l : m : []) =
526+
object13 a b c d e f g h i j k l m
527+
#-}
528+
{-# RULES "objectFromList/14" forall a b c d e f g h i j k l m n.
529+
objectFromList (a : b : c : d : e : f : g : h : i : j : k : l : m : n : []) =
530+
object14 a b c d e f g h i j k l m n
531+
#-}
532+
{-# RULES "objectFromList/15" forall a b c d e f g h i j k l m n o.
533+
objectFromList (a : b : c : d : e : f : g : h : i : j : k : l : m : n : o : []) =
534+
object15 a b c d e f g h i j k l m n o
535+
#-}
536+
{-# RULES "objectFromList/16" forall a b c d e f g h i j k l m n o p.
537+
objectFromList (a : b : c : d : e : f : g : h : i : j : k : l : m : n : o : p : []) =
538+
object16 a b c d e f g h i j k l m n o p
539+
#-}
540+
461541
-- | Construct a JSON object with one member.
462542
object1 :: Member -> Value
463543
{-# inline object1 #-}

0 commit comments

Comments
 (0)