From 1f8c1ece115d6583cd943009d562372ad17a40f7 Mon Sep 17 00:00:00 2001 From: "A.Fink" Date: Tue, 6 Feb 2024 13:40:15 +0300 Subject: [PATCH] JsonObject: better .stream(), use Map.entry; tests --- .../java/io/vertx/core/json/JsonObject.java | 38 +++++----------- .../io/vertx/core/json/JsonObjectTest.java | 45 +++++++++++++++++++ 2 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/main/java/io/vertx/core/json/JsonObject.java b/src/main/java/io/vertx/core/json/JsonObject.java index 1f22d8979f5..2531bfb05d8 100644 --- a/src/main/java/io/vertx/core/json/JsonObject.java +++ b/src/main/java/io/vertx/core/json/JsonObject.java @@ -18,6 +18,7 @@ import java.util.*; import java.util.function.Function; import java.util.stream.Stream; +import java.util.stream.StreamSupport; import static io.vertx.core.json.impl.JsonUtil.*; import static java.time.format.DateTimeFormatter.ISO_INSTANT; @@ -1114,7 +1115,13 @@ public Map getMap() { * @return a Stream */ public Stream> stream() { - return asStream(iterator()); + // JsonUtil.asStream(iterator()) is too generic + return StreamSupport.stream(spliterator(), false); + } + + @Override + public Spliterator> spliterator() { + return Spliterators.spliterator(iterator(), map.size(), Spliterator.DISTINCT | Spliterator.NONNULL); } /** @@ -1275,7 +1282,9 @@ public Map.Entry next() { final Object wrapped = wrapJsonValue(val); if (val != wrapped) { - return new Entry(entry.getKey(), wrapped); + // Map.entry disallows null keys and values: we disallow null keys, + // (val != wrapped) skips null values (wrapJsonValue doesn't wrap null) + return Map.entry(entry.getKey(), wrapped); } return entry; @@ -1286,29 +1295,4 @@ public void remove() { mapIter.remove(); } } - - private static final class Entry implements Map.Entry { - final String key; - final Object value; - - public Entry(String key, Object value) { - this.key = key; - this.value = value; - } - - @Override - public String getKey() { - return key; - } - - @Override - public Object getValue() { - return value; - } - - @Override - public Object setValue(Object value) { - throw new UnsupportedOperationException(); - } - } } diff --git a/src/test/java/io/vertx/core/json/JsonObjectTest.java b/src/test/java/io/vertx/core/json/JsonObjectTest.java index 040d765feb6..ae70c7d1e19 100644 --- a/src/test/java/io/vertx/core/json/JsonObjectTest.java +++ b/src/test/java/io/vertx/core/json/JsonObjectTest.java @@ -12,6 +12,7 @@ package io.vertx.core.json; import io.vertx.core.buffer.Buffer; +import io.vertx.core.json.impl.JsonUtil; import io.vertx.core.shareddata.Shareable; import io.vertx.test.core.TestUtils; import org.junit.Before; @@ -1973,4 +1974,48 @@ public void testJsonObjectOfArgs() { public void testJsonObjectOfEmpty() { assertEquals(new JsonObject(), JsonObject.of()); } + + @Test + public void testNull() { + assertEquals(JsonObject.of(), JsonObject.of()); + assertFalse(JsonObject.of().equals(null)); + assertFalse(JsonObject.of().equals(new JsonObject(){})); + assertNull(JsonUtil.wrapJsonValue(null)); + + JsonObject jo = JsonObject.of("k", null, "", null); + + AtomicInteger cnt = new AtomicInteger(); + jo.iterator().forEachRemaining(e -> { + assertNotNull(e.getKey()); + assertNull(e.getValue());// null is ok + cnt.incrementAndGet(); + }); + assertEquals(2, cnt.get()); + + assertEquals("=null, k=null", + jo.stream().sorted(Map.Entry.comparingByKey()) + .map(e -> e.getKey()+"="+e.getValue()).collect(Collectors.joining(", "))); + } + + @Test + public void testBuffer() { + JsonObject jo = new JsonObject("{\"k\": 1}"); + assertEquals("{\"k\":1}", jo.toString()); + assertEquals("{\"k\":1}", jo.toBuffer().toString()); + assertEquals("{\"k\":1}", jo.toBuffer().toJson().toString()); + + Buffer buf = Buffer.buffer(); + buf.appendLong(Long.MIN_VALUE); + jo.writeToBuffer(buf); + JsonObject jo0 = JsonObject.of("aaa", 111, "BBB", 222); + jo0.writeToBuffer(buf); + + assertEquals(Long.MIN_VALUE, buf.getLong(0)); + JsonObject jo1 = JsonObject.of("x", 0); + int index = jo1.readFromBuffer(8, buf); + assertEquals(jo, jo1); + JsonObject jo2 = JsonObject.of("x", 0); + jo2.readFromBuffer(index, buf); + assertEquals(jo0, jo2); + } }