Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonObject: better .stream(), use Map.entry #5097

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions src/main/java/io/vertx/core/json/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1114,7 +1115,13 @@ public Map<String, Object> getMap() {
* @return a Stream
*/
public Stream<Map.Entry<String, Object>> stream() {
return asStream(iterator());
// JsonUtil.asStream(iterator()) is too generic
return StreamSupport.stream(spliterator(), false);
}

@Override
public Spliterator<Map.Entry<String,Object>> spliterator() {
return Spliterators.spliterator(iterator(), map.size(), Spliterator.DISTINCT | Spliterator.NONNULL);
}

/**
Expand Down Expand Up @@ -1275,7 +1282,9 @@ public Map.Entry<String, Object> 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;
Expand All @@ -1286,29 +1295,4 @@ public void remove() {
mapIter.remove();
}
}

private static final class Entry implements Map.Entry<String, Object> {
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();
}
}
}
45 changes: 45 additions & 0 deletions src/test/java/io/vertx/core/json/JsonObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Loading