diff --git a/builtin/iterator.mbt b/builtin/iterator.mbt index 60718df2b..4ac9c7e0a 100644 --- a/builtin/iterator.mbt +++ b/builtin/iterator.mbt @@ -75,7 +75,9 @@ pub impl[X : Show] Show for Iter[X] with output(self, logger) { ///| pub impl[X : ToJson] ToJson for Iter[X] with to_json(self) { - Json::array(self.map(x => x.to_json()).collect()) + [ + for x in self => x + ] } ///| diff --git a/deque/deque.mbt b/deque/deque.mbt index 6cd6d3431..7f4bc059b 100644 --- a/deque/deque.mbt +++ b/deque/deque.mbt @@ -1675,7 +1675,9 @@ pub fn Deque::join(self : Deque[String], separator : StringView) -> String { /// ``` /// pub impl[A : ToJson] ToJson for Deque[A] with to_json(self : Deque[A]) -> Json { - Json::array([ for x in self => x.to_json() ]) + [ + for x in self => x + ] } ///| diff --git a/hashset/hashset.mbt b/hashset/hashset.mbt index 4ba3437dd..de0748900 100644 --- a/hashset/hashset.mbt +++ b/hashset/hashset.mbt @@ -672,13 +672,9 @@ pub fn[K] HashSet::copy(self : HashSet[K]) -> HashSet[K] { ///| /// ToJson implementation for hashset pub impl[X : ToJson] ToJson for HashSet[X] with to_json(self) { - let res = Array::new(capacity=self.size) - for entry in self.entries { - if entry is Some({ key, .. }) { - res.push(key.to_json()) - } - } - Json::array(res) + [ + for key in self => key + ] } ///| diff --git a/immut/priority_queue/types.mbt b/immut/priority_queue/types.mbt index f4375e294..45d6eee2d 100644 --- a/immut/priority_queue/types.mbt +++ b/immut/priority_queue/types.mbt @@ -30,5 +30,7 @@ priv enum Node[A] { pub impl[A : ToJson + Compare] ToJson for PriorityQueue[A] with to_json( self : PriorityQueue[A], ) { - Json::array([ for item in self => item.to_json() ]) + [ + for item in self => item + ] } diff --git a/immut/sorted_set/immutable_set.mbt b/immut/sorted_set/immutable_set.mbt index baef33676..50d129590 100644 --- a/immut/sorted_set/immutable_set.mbt +++ b/immut/sorted_set/immutable_set.mbt @@ -791,7 +791,9 @@ pub impl[A : Show] Show for SortedSet[A] with output(self, logger) { ///| pub impl[A : ToJson] ToJson for SortedSet[A] with to_json(self) { - Json::array([ for a in self => a.to_json() ]) + [ + for a in self => a.to_json() + ] } ///| diff --git a/immut/vector/vector.mbt b/immut/vector/vector.mbt index 8c55c2f6e..a23314630 100644 --- a/immut/vector/vector.mbt +++ b/immut/vector/vector.mbt @@ -666,7 +666,9 @@ pub impl[A : Show] Show for Vector[A] with output(self, logger) { ///| pub impl[A : ToJson] ToJson for Vector[A] with to_json(self) { - Json::array([ for value in self => value.to_json() ]) + [ + for value in self => value + ] } ///| diff --git a/immut/vector/vector_test.mbt b/immut/vector/vector_test.mbt index 770db86e3..82fbaada1 100644 --- a/immut/vector/vector_test.mbt +++ b/immut/vector/vector_test.mbt @@ -79,7 +79,7 @@ test "to_array empty" { ///| test "to_json" { let v = @vector.from_iter((0).until(40)) - let expected = Json::array(Array::makei(40, i => i.to_json())) + let expected : Json = [ for i in 0..<40 => i ] @json.json_inspect(v, content=expected) let empty : @vector.Vector[Int] = @vector.new() @json.json_inspect(empty, content=[]) diff --git a/list/list.mbt b/list/list.mbt index 766a850be..bfd46ed81 100644 --- a/list/list.mbt +++ b/list/list.mbt @@ -94,13 +94,9 @@ pub impl[A : Show] Show for List[A] with output(xs, logger) { /// ToJson implementation for List. /// Converts a list to a JSON array. pub impl[A : ToJson] ToJson for List[A] with to_json(self) { - let capacity = self.length() - guard capacity != 0 else { return [] } - let jsons = Array::new(capacity~) - for a in self { - jsons.push(a.to_json()) - } - Json::array(jsons) + [ + for a in self => a + ] } ///| diff --git a/priority_queue/moon.pkg b/priority_queue/moon.pkg index 4437e2f80..3c969ea88 100644 --- a/priority_queue/moon.pkg +++ b/priority_queue/moon.pkg @@ -4,12 +4,12 @@ import { "moonbitlang/core/debug", "moonbitlang/core/quickcheck", "moonbitlang/core/quickcheck/splitmix", - "moonbitlang/core/json", } import { "moonbitlang/core/random", "moonbitlang/core/cmp", + "moonbitlang/core/json", } for "test" options( diff --git a/priority_queue/priority_queue.mbt b/priority_queue/priority_queue.mbt index a80aff59c..2ad65990c 100644 --- a/priority_queue/priority_queue.mbt +++ b/priority_queue/priority_queue.mbt @@ -142,7 +142,9 @@ pub fn[A : Compare] PriorityQueue::iter(self : PriorityQueue[A]) -> Iter[A] { ///| pub impl[A : ToJson + Compare] ToJson for PriorityQueue[A] with to_json(self) { - Json::array([ for x in self => x.to_json() ]) + [ + for x in self => x + ] } ///| diff --git a/set/linked_hash_set.mbt b/set/linked_hash_set.mbt index b067d816e..f9f416828 100644 --- a/set/linked_hash_set.mbt +++ b/set/linked_hash_set.mbt @@ -594,11 +594,9 @@ pub fn[K : Hash + Eq] Set::intersection( ///| pub impl[X : ToJson] ToJson for Set[X] with to_json(self) { - let res = Array::new(capacity=self.size) - for v in self { - res.push(v.to_json()) - } - Json::array(res) + [ + for v in self => v + ] } ///|