Skip to content

Rename to JsonWriteUtils to avoid clashing with extraction original #7

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

Merged
merged 2 commits into from
Jan 31, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/main/java/io/nats/client/support/JsonValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.*;

import static io.nats.client.support.Encoding.jsonEncode;
import static io.nats.client.support.JsonUtils.*;
import static io.nats.client.support.JsonWriteUtils.*;

public class JsonValue implements JsonSerializable {

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/nats/client/support/JsonValueUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static List<JsonValue> readArray(JsonValue jsonValue, String key) {

public static Map<String, String> readStringStringMap(JsonValue jv, String key) {
JsonValue o = readObject(jv, key);
if (o.type == Type.MAP && o.map.size() > 0) {
if (o.type == Type.MAP && !o.map.isEmpty()) {
Map<String, String> temp = new HashMap<>();
for (String k : o.map.keySet()) {
String value = readString(o, k);
Expand Down Expand Up @@ -171,6 +171,7 @@ public static List<String> readOptionalStringList(JsonValue jsonValue, String ke
public static List<Long> readLongList(JsonValue jsonValue, String key) {
return read(jsonValue, key, v -> listOf(v, JsonValueUtils::getLong));
}

public static List<Duration> readNanosList(JsonValue jsonValue, String key) {
return readNanosList(jsonValue, key, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static io.nats.client.support.DateTimeUtils.DEFAULT_TIME;
import static io.nats.client.support.Encoding.jsonEncode;
import static io.nats.client.support.JsonValueUtils.instance;

/**
* Internal json parsing helpers.
*/
public abstract class JsonUtils {
private static final String Q = "\"";
private static final String QCOLONQ = "\":\"";
private static final String QCOLON = "\":";
private static final String QCOMMA = "\",";
private static final String COMMA = ",";
public abstract class JsonWriteUtils {
public static final String Q = "\"";
public static final String QCOLONQ = "\":\"";
public static final String QCOLON = "\":";
public static final String QCOMMA = "\",";
public static final String COMMA = ",";

private JsonUtils() {} /* ensures cannot be constructed */
private JsonWriteUtils() {} /* ensures cannot be constructed */

// ----------------------------------------------------------------------------------------------------
// BUILD A STRING OF JSON
Expand Down Expand Up @@ -472,19 +468,4 @@ public static long safeParseLong(String s, long dflt) {
Long l = safeParseLong(s);
return l == null ? dflt : l;
}

public static boolean mapEquals(Map<String, String> map1, Map<String, String> map2) {
if (map1 == null) {
return map2 == null;
}
if (map2 == null || map1.size() != map2.size()) {
return false;
}
for (String key : map1.keySet()) {
if (!Objects.equals(map1.get(key), map2.get(key))) {
return false;
}
}
return true;
}
}
9 changes: 8 additions & 1 deletion src/test/java/io/nats/client/support/JsonParsingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public void testStringParsing() {

for (int i = 0; i < list.size(); i++) {
JsonValue v = list.get(i);
assertEquals(v, v.toJsonValue());
assertEquals(v.toString("JsonParsingTests"), v.toString(this.getClass()));
assertEquals(decodeds.get(i), v.string);
assertEquals(v.toJson(), "\"" + encodeds.get(i) + "\"");
}
Expand All @@ -106,6 +108,7 @@ public void testJsonValuePrimitives() throws JsonParseException {
oMap.put("falseKey1", new JsonValue(false));
oMap.put("falseKey2", new JsonValue(Boolean.FALSE));
oMap.put("stringKey", new JsonValue("hello world!"));
oMap.put("charKey", new JsonValue('x'));
oMap.put("escapeStringKey", new JsonValue("h\be\tllo w\u1234orld!"));
oMap.put("nullKey", JsonValue.NULL);
oMap.put("intKey1", new JsonValue(Integer.MAX_VALUE));
Expand All @@ -124,6 +127,10 @@ public void testJsonValuePrimitives() throws JsonParseException {
// some coverage here
JsonValue vMap = new JsonValue(oMap);
assertEquals(vMap.toJson(), vMap.toString());
String s = JsonValueUtils.readString(vMap, "stringKey");
byte[] ba = JsonValueUtils.readBytes(vMap, "stringKey");
assertNotNull(ba);
assertEquals(s, new String(ba));

validateMapTypes(oMap, oMap, true);

Expand All @@ -146,6 +153,7 @@ private static void validateMapTypes(Map<String, JsonValue> map, Map<String, Jso
assertEquals(JsonValue.Type.BOOL, map.get("falseKey1").type);
assertEquals(JsonValue.Type.BOOL, map.get("falseKey2").type);
assertEquals(JsonValue.Type.STRING, map.get("stringKey").type);
assertEquals(JsonValue.Type.STRING, map.get("charKey").type);
assertEquals(JsonValue.Type.STRING, map.get("escapeStringKey").type);
assertEquals(JsonValue.Type.INTEGER, map.get("intKey1").type);
assertEquals(JsonValue.Type.INTEGER, map.get("intKey2").type);
Expand Down Expand Up @@ -228,7 +236,6 @@ public void testArray() throws JsonParseException {
assertTrue(list.contains(v));
}


list.clear();
list.add(new JsonValue(1));
list.add(new JsonValue(Long.MAX_VALUE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@

import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static io.nats.client.support.DateTimeUtils.DEFAULT_TIME;
import static io.nats.client.support.JsonUtils.*;
import static io.nats.client.support.JsonWriteUtils.*;
import static org.junit.jupiter.api.Assertions.*;

public final class JsonUtilsTests {
public final class JsonWriteUtilsTests {

@Test
public void testBeginEnd() {
Expand Down Expand Up @@ -292,7 +289,7 @@ public void testMiscCoverage() {
String json = ResourceUtils.resourceAsString("StreamInfo.json");
printFormatted(JsonParser.parseUnchecked(json));

assertEquals("\"JsonUtilsTests\":", toKey(this.getClass()));
assertEquals("\"JsonWriteUtilsTests\":", toKey(this.getClass()));
}

@Test
Expand All @@ -314,42 +311,57 @@ public void testMapEquals() {
Map<String, String> empty1 = new HashMap<>();
Map<String, String> empty2 = new HashMap<>();

assertTrue(JsonUtils.mapEquals(null, null));
assertFalse(JsonUtils.mapEquals(map1, null));
assertFalse(JsonUtils.mapEquals(null, map1));
assertFalse(JsonUtils.mapEquals(null, empty1));
assertFalse(JsonUtils.mapEquals(empty1, null));

assertTrue(JsonUtils.mapEquals(map1, map2));
assertFalse(JsonUtils.mapEquals(map1, map3));
assertFalse(JsonUtils.mapEquals(map1, map4));
assertFalse(JsonUtils.mapEquals(map1, empty1));

assertTrue(JsonUtils.mapEquals(map2, map1));
assertFalse(JsonUtils.mapEquals(map2, map3));
assertFalse(JsonUtils.mapEquals(map2, map4));
assertFalse(JsonUtils.mapEquals(map2, empty1));

assertFalse(JsonUtils.mapEquals(map3, map1));
assertFalse(JsonUtils.mapEquals(map3, map2));
assertFalse(JsonUtils.mapEquals(map3, map4));
assertFalse(JsonUtils.mapEquals(map3, empty1));

assertFalse(JsonUtils.mapEquals(map4, map1));
assertFalse(JsonUtils.mapEquals(map4, map2));
assertFalse(JsonUtils.mapEquals(map4, map3));
assertFalse(JsonUtils.mapEquals(map4, empty1));

assertFalse(JsonUtils.mapEquals(empty1, map1));
assertFalse(JsonUtils.mapEquals(empty1, map2));
assertFalse(JsonUtils.mapEquals(empty1, map3));
assertFalse(JsonUtils.mapEquals(empty1, map4));
assertTrue(JsonUtils.mapEquals(empty1, empty2));

assertFalse(JsonUtils.mapEquals(empty2, map1));
assertFalse(JsonUtils.mapEquals(empty2, map2));
assertFalse(JsonUtils.mapEquals(empty2, map3));
assertFalse(JsonUtils.mapEquals(empty2, map4));
assertTrue(JsonUtils.mapEquals(empty2, empty1));
assertTrue(mapEquals(null, null));
assertFalse(mapEquals(map1, null));
assertFalse(mapEquals(null, map1));
assertFalse(mapEquals(null, empty1));
assertFalse(mapEquals(empty1, null));

assertTrue(mapEquals(map1, map2));
assertFalse(mapEquals(map1, map3));
assertFalse(mapEquals(map1, map4));
assertFalse(mapEquals(map1, empty1));

assertTrue(mapEquals(map2, map1));
assertFalse(mapEquals(map2, map3));
assertFalse(mapEquals(map2, map4));
assertFalse(mapEquals(map2, empty1));

assertFalse(mapEquals(map3, map1));
assertFalse(mapEquals(map3, map2));
assertFalse(mapEquals(map3, map4));
assertFalse(mapEquals(map3, empty1));

assertFalse(mapEquals(map4, map1));
assertFalse(mapEquals(map4, map2));
assertFalse(mapEquals(map4, map3));
assertFalse(mapEquals(map4, empty1));

assertFalse(mapEquals(empty1, map1));
assertFalse(mapEquals(empty1, map2));
assertFalse(mapEquals(empty1, map3));
assertFalse(mapEquals(empty1, map4));
assertTrue(mapEquals(empty1, empty2));

assertFalse(mapEquals(empty2, map1));
assertFalse(mapEquals(empty2, map2));
assertFalse(mapEquals(empty2, map3));
assertFalse(mapEquals(empty2, map4));
assertTrue(mapEquals(empty2, empty1));
}

public static boolean mapEquals(Map<String, String> map1, Map<String, String> map2) {
if (map1 == null) {
return map2 == null;
}
if (map2 == null || map1.size() != map2.size()) {
return false;
}
for (String key : map1.keySet()) {
if (!Objects.equals(map1.get(key), map2.get(key))) {
return false;
}
}
return true;
}
}