@@ -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))
454456pattern (:->) :: ShortText -> Value -> Member
455457pattern 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.
458472objectFromList :: [Member ] -> Value
459473objectFromList 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.
462542object1 :: Member -> Value
463543{-# inline object1 #-}
0 commit comments