Skip to content

Commit

Permalink
Add Json.Obj.empty and Json.Arr.empty constants (#1241)
Browse files Browse the repository at this point in the history
  • Loading branch information
plokhotnyuk authored Jan 24, 2025
1 parent dfb8184 commit 8b800e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions zio-json/shared/src/main/scala/zio/json/ast/ast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ object Json {
override def mapObjectEntries(f: ((String, Json)) => (String, Json)): Json.Obj = Json.Obj(fields.map(f))
}
object Obj {
val empty: Obj = Obj(Chunk.empty)

def apply(fields: (String, Json)*): Obj = Obj(Chunk(fields: _*))

private lazy val objd = JsonDecoder.keyValueChunk[String, Json]
Expand Down Expand Up @@ -423,6 +425,8 @@ object Json {
override def mapArrayValues(f: Json => Json): Json.Arr = Json.Arr(elements.map(f))
}
object Arr {
val empty: Arr = Arr(Chunk.empty)

def apply(elements: Json*): Arr = Arr(Chunk(elements: _*))

private lazy val arrd = JsonDecoder.chunk[Json]
Expand Down
4 changes: 4 additions & 0 deletions zio-json/shared/src/test/scala/zio/json/ast/JsonSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zio.json.ast

import zio.Chunk
import zio.json._
import zio.test.Assertion._
import zio.test._
Expand Down Expand Up @@ -87,6 +88,8 @@ object JsonSpec extends ZIOSpecDefault {
case fst :: snd :: Nil => fst != snd
case _ => false
})
assertTrue(Json.Obj.empty == Json.Obj(Chunk.empty))
assertTrue(Json.Arr.empty == Json.Arr(Chunk.empty))
},
test("object order does not matter for equality") {
val obj1 = Json.Obj(
Expand Down Expand Up @@ -146,6 +149,7 @@ object JsonSpec extends ZIOSpecDefault {
)

assertTrue(obj1.hashCode == obj2.hashCode)
assertTrue(Json.Obj.empty.hashCode == Json.Obj(Chunk.empty).hashCode)
}
),
suite("foldUp")(
Expand Down

0 comments on commit 8b800e0

Please sign in to comment.