Skip to content

Commit 4a37cda

Browse files
authored
Merge pull request #7 from nats-io/write-utils
Rename to JsonWriteUtils to avoid clashing with extraction original
2 parents d447b87 + d268876 commit 4a37cda

File tree

5 files changed

+74
-73
lines changed

5 files changed

+74
-73
lines changed

src/main/java/io/nats/client/support/JsonValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.util.*;
1919

2020
import static io.nats.client.support.Encoding.jsonEncode;
21-
import static io.nats.client.support.JsonUtils.*;
21+
import static io.nats.client.support.JsonWriteUtils.*;
2222

2323
public class JsonValue implements JsonSerializable {
2424

src/main/java/io/nats/client/support/JsonValueUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static List<JsonValue> readArray(JsonValue jsonValue, String key) {
5353

5454
public static Map<String, String> readStringStringMap(JsonValue jv, String key) {
5555
JsonValue o = readObject(jv, key);
56-
if (o.type == Type.MAP && o.map.size() > 0) {
56+
if (o.type == Type.MAP && !o.map.isEmpty()) {
5757
Map<String, String> temp = new HashMap<>();
5858
for (String k : o.map.keySet()) {
5959
String value = readString(o, k);
@@ -171,6 +171,7 @@ public static List<String> readOptionalStringList(JsonValue jsonValue, String ke
171171
public static List<Long> readLongList(JsonValue jsonValue, String key) {
172172
return read(jsonValue, key, v -> listOf(v, JsonValueUtils::getLong));
173173
}
174+
174175
public static List<Duration> readNanosList(JsonValue jsonValue, String key) {
175176
return readNanosList(jsonValue, key, false);
176177
}

src/main/java/io/nats/client/support/JsonUtils.java renamed to src/main/java/io/nats/client/support/JsonWriteUtils.java

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,19 @@
1818
import java.util.Arrays;
1919
import java.util.List;
2020
import java.util.Map;
21-
import java.util.Objects;
2221

2322
import static io.nats.client.support.DateTimeUtils.DEFAULT_TIME;
2423
import static io.nats.client.support.Encoding.jsonEncode;
2524
import static io.nats.client.support.JsonValueUtils.instance;
2625

27-
/**
28-
* Internal json parsing helpers.
29-
*/
30-
public abstract class JsonUtils {
31-
private static final String Q = "\"";
32-
private static final String QCOLONQ = "\":\"";
33-
private static final String QCOLON = "\":";
34-
private static final String QCOMMA = "\",";
35-
private static final String COMMA = ",";
26+
public abstract class JsonWriteUtils {
27+
public static final String Q = "\"";
28+
public static final String QCOLONQ = "\":\"";
29+
public static final String QCOLON = "\":";
30+
public static final String QCOMMA = "\",";
31+
public static final String COMMA = ",";
3632

37-
private JsonUtils() {} /* ensures cannot be constructed */
33+
private JsonWriteUtils() {} /* ensures cannot be constructed */
3834

3935
// ----------------------------------------------------------------------------------------------------
4036
// BUILD A STRING OF JSON
@@ -472,19 +468,4 @@ public static long safeParseLong(String s, long dflt) {
472468
Long l = safeParseLong(s);
473469
return l == null ? dflt : l;
474470
}
475-
476-
public static boolean mapEquals(Map<String, String> map1, Map<String, String> map2) {
477-
if (map1 == null) {
478-
return map2 == null;
479-
}
480-
if (map2 == null || map1.size() != map2.size()) {
481-
return false;
482-
}
483-
for (String key : map1.keySet()) {
484-
if (!Objects.equals(map1.get(key), map2.get(key))) {
485-
return false;
486-
}
487-
}
488-
return true;
489-
}
490471
}

src/test/java/io/nats/client/support/JsonParsingTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public void testStringParsing() {
8181

8282
for (int i = 0; i < list.size(); i++) {
8383
JsonValue v = list.get(i);
84+
assertEquals(v, v.toJsonValue());
85+
assertEquals(v.toString("JsonParsingTests"), v.toString(this.getClass()));
8486
assertEquals(decodeds.get(i), v.string);
8587
assertEquals(v.toJson(), "\"" + encodeds.get(i) + "\"");
8688
}
@@ -106,6 +108,7 @@ public void testJsonValuePrimitives() throws JsonParseException {
106108
oMap.put("falseKey1", new JsonValue(false));
107109
oMap.put("falseKey2", new JsonValue(Boolean.FALSE));
108110
oMap.put("stringKey", new JsonValue("hello world!"));
111+
oMap.put("charKey", new JsonValue('x'));
109112
oMap.put("escapeStringKey", new JsonValue("h\be\tllo w\u1234orld!"));
110113
oMap.put("nullKey", JsonValue.NULL);
111114
oMap.put("intKey1", new JsonValue(Integer.MAX_VALUE));
@@ -124,6 +127,10 @@ public void testJsonValuePrimitives() throws JsonParseException {
124127
// some coverage here
125128
JsonValue vMap = new JsonValue(oMap);
126129
assertEquals(vMap.toJson(), vMap.toString());
130+
String s = JsonValueUtils.readString(vMap, "stringKey");
131+
byte[] ba = JsonValueUtils.readBytes(vMap, "stringKey");
132+
assertNotNull(ba);
133+
assertEquals(s, new String(ba));
127134

128135
validateMapTypes(oMap, oMap, true);
129136

@@ -146,6 +153,7 @@ private static void validateMapTypes(Map<String, JsonValue> map, Map<String, Jso
146153
assertEquals(JsonValue.Type.BOOL, map.get("falseKey1").type);
147154
assertEquals(JsonValue.Type.BOOL, map.get("falseKey2").type);
148155
assertEquals(JsonValue.Type.STRING, map.get("stringKey").type);
156+
assertEquals(JsonValue.Type.STRING, map.get("charKey").type);
149157
assertEquals(JsonValue.Type.STRING, map.get("escapeStringKey").type);
150158
assertEquals(JsonValue.Type.INTEGER, map.get("intKey1").type);
151159
assertEquals(JsonValue.Type.INTEGER, map.get("intKey2").type);
@@ -228,7 +236,6 @@ public void testArray() throws JsonParseException {
228236
assertTrue(list.contains(v));
229237
}
230238

231-
232239
list.clear();
233240
list.add(new JsonValue(1));
234241
list.add(new JsonValue(Long.MAX_VALUE));

src/test/java/io/nats/client/support/JsonUtilsTests.java renamed to src/test/java/io/nats/client/support/JsonWriteUtilsTests.java

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@
1818

1919
import java.time.Duration;
2020
import java.time.ZonedDateTime;
21-
import java.util.ArrayList;
22-
import java.util.HashMap;
23-
import java.util.List;
24-
import java.util.Map;
21+
import java.util.*;
2522

2623
import static io.nats.client.support.DateTimeUtils.DEFAULT_TIME;
27-
import static io.nats.client.support.JsonUtils.*;
24+
import static io.nats.client.support.JsonWriteUtils.*;
2825
import static org.junit.jupiter.api.Assertions.*;
2926

30-
public final class JsonUtilsTests {
27+
public final class JsonWriteUtilsTests {
3128

3229
@Test
3330
public void testBeginEnd() {
@@ -292,7 +289,7 @@ public void testMiscCoverage() {
292289
String json = ResourceUtils.resourceAsString("StreamInfo.json");
293290
printFormatted(JsonParser.parseUnchecked(json));
294291

295-
assertEquals("\"JsonUtilsTests\":", toKey(this.getClass()));
292+
assertEquals("\"JsonWriteUtilsTests\":", toKey(this.getClass()));
296293
}
297294

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

317-
assertTrue(JsonUtils.mapEquals(null, null));
318-
assertFalse(JsonUtils.mapEquals(map1, null));
319-
assertFalse(JsonUtils.mapEquals(null, map1));
320-
assertFalse(JsonUtils.mapEquals(null, empty1));
321-
assertFalse(JsonUtils.mapEquals(empty1, null));
322-
323-
assertTrue(JsonUtils.mapEquals(map1, map2));
324-
assertFalse(JsonUtils.mapEquals(map1, map3));
325-
assertFalse(JsonUtils.mapEquals(map1, map4));
326-
assertFalse(JsonUtils.mapEquals(map1, empty1));
327-
328-
assertTrue(JsonUtils.mapEquals(map2, map1));
329-
assertFalse(JsonUtils.mapEquals(map2, map3));
330-
assertFalse(JsonUtils.mapEquals(map2, map4));
331-
assertFalse(JsonUtils.mapEquals(map2, empty1));
332-
333-
assertFalse(JsonUtils.mapEquals(map3, map1));
334-
assertFalse(JsonUtils.mapEquals(map3, map2));
335-
assertFalse(JsonUtils.mapEquals(map3, map4));
336-
assertFalse(JsonUtils.mapEquals(map3, empty1));
337-
338-
assertFalse(JsonUtils.mapEquals(map4, map1));
339-
assertFalse(JsonUtils.mapEquals(map4, map2));
340-
assertFalse(JsonUtils.mapEquals(map4, map3));
341-
assertFalse(JsonUtils.mapEquals(map4, empty1));
342-
343-
assertFalse(JsonUtils.mapEquals(empty1, map1));
344-
assertFalse(JsonUtils.mapEquals(empty1, map2));
345-
assertFalse(JsonUtils.mapEquals(empty1, map3));
346-
assertFalse(JsonUtils.mapEquals(empty1, map4));
347-
assertTrue(JsonUtils.mapEquals(empty1, empty2));
348-
349-
assertFalse(JsonUtils.mapEquals(empty2, map1));
350-
assertFalse(JsonUtils.mapEquals(empty2, map2));
351-
assertFalse(JsonUtils.mapEquals(empty2, map3));
352-
assertFalse(JsonUtils.mapEquals(empty2, map4));
353-
assertTrue(JsonUtils.mapEquals(empty2, empty1));
314+
assertTrue(mapEquals(null, null));
315+
assertFalse(mapEquals(map1, null));
316+
assertFalse(mapEquals(null, map1));
317+
assertFalse(mapEquals(null, empty1));
318+
assertFalse(mapEquals(empty1, null));
319+
320+
assertTrue(mapEquals(map1, map2));
321+
assertFalse(mapEquals(map1, map3));
322+
assertFalse(mapEquals(map1, map4));
323+
assertFalse(mapEquals(map1, empty1));
324+
325+
assertTrue(mapEquals(map2, map1));
326+
assertFalse(mapEquals(map2, map3));
327+
assertFalse(mapEquals(map2, map4));
328+
assertFalse(mapEquals(map2, empty1));
329+
330+
assertFalse(mapEquals(map3, map1));
331+
assertFalse(mapEquals(map3, map2));
332+
assertFalse(mapEquals(map3, map4));
333+
assertFalse(mapEquals(map3, empty1));
334+
335+
assertFalse(mapEquals(map4, map1));
336+
assertFalse(mapEquals(map4, map2));
337+
assertFalse(mapEquals(map4, map3));
338+
assertFalse(mapEquals(map4, empty1));
339+
340+
assertFalse(mapEquals(empty1, map1));
341+
assertFalse(mapEquals(empty1, map2));
342+
assertFalse(mapEquals(empty1, map3));
343+
assertFalse(mapEquals(empty1, map4));
344+
assertTrue(mapEquals(empty1, empty2));
345+
346+
assertFalse(mapEquals(empty2, map1));
347+
assertFalse(mapEquals(empty2, map2));
348+
assertFalse(mapEquals(empty2, map3));
349+
assertFalse(mapEquals(empty2, map4));
350+
assertTrue(mapEquals(empty2, empty1));
351+
}
352+
353+
public static boolean mapEquals(Map<String, String> map1, Map<String, String> map2) {
354+
if (map1 == null) {
355+
return map2 == null;
356+
}
357+
if (map2 == null || map1.size() != map2.size()) {
358+
return false;
359+
}
360+
for (String key : map1.keySet()) {
361+
if (!Objects.equals(map1.get(key), map2.get(key))) {
362+
return false;
363+
}
364+
}
365+
return true;
354366
}
355367
}

0 commit comments

Comments
 (0)