Skip to content

Commit 134deb1

Browse files
committed
Start 3.0.0 API Changes and Improvements.
1 parent fc14734 commit 134deb1

17 files changed

+1614
-815
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugins {
1111
id 'signing'
1212
}
1313

14-
def jarVersion = "2.21.0"
14+
def jarVersion = "3.0.0"
1515
group = 'io.nats'
1616

1717
def isMerge = System.getenv("BUILD_EVENT") == "push"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package io.nats.json;
15+
16+
import java.util.ArrayList;
17+
import java.util.Collection;
18+
19+
public class ArrayBuilder implements JsonSerializable {
20+
21+
public final JsonValue jv = new JsonValue(new ArrayList<>());
22+
23+
public static ArrayBuilder instance() {
24+
return new ArrayBuilder();
25+
}
26+
27+
public ArrayBuilder add(Object o) {
28+
//noinspection DataFlowIssue // NO ISSUE, WE KNOW jv.array is NOT NULL
29+
jv.array.add(JsonValue.instance(o));
30+
return this;
31+
}
32+
33+
public ArrayBuilder addItems(Collection<?> c) {
34+
if (c != null) {
35+
for (Object o : c) {
36+
//noinspection DataFlowIssue // NO ISSUE, WE KNOW jv.array is NOT NULL
37+
jv.array.add(JsonValue.instance(o));
38+
}
39+
}
40+
return this;
41+
}
42+
43+
@Override
44+
public String toJson() {
45+
return jv.toJson();
46+
}
47+
48+
@Override
49+
public JsonValue toJsonValue() {
50+
return jv;
51+
}
52+
}

src/main/java/io/nats/json/DateTimeUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2024 The NATS Authors
1+
// Copyright 2020-2025 The NATS Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at:

src/main/java/io/nats/json/Encoding.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2024 The NATS Authors
1+
// Copyright 2020-2025 The NATS Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at:
@@ -13,11 +13,12 @@
1313

1414
package io.nats.json;
1515

16+
import org.apache.commons.codec.binary.Base64;
17+
1618
import java.net.URLDecoder;
19+
import java.nio.charset.Charset;
1720
import java.nio.charset.StandardCharsets;
1821

19-
import org.apache.commons.codec.binary.Base64;
20-
2122
/**
2223
* Utilities for encoding, i.e., Base64, URI and JSON
2324
*/
@@ -52,6 +53,16 @@ public static String base64BasicEncodeToString(String input) {
5253
return Base64.encodeBase64String(input.getBytes(StandardCharsets.UTF_8));
5354
}
5455

56+
/**
57+
* base64 url encode a byte array to a byte array
58+
* @param input the input byte array to encode
59+
* @param charset the charset of the input string
60+
* @return the encoded byte array
61+
*/
62+
public static String base64BasicEncodeToString(String input, Charset charset) {
63+
return Base64.encodeBase64String(input.getBytes(charset));
64+
}
65+
5566
/**
5667
* base64 url encode a byte array to a byte array
5768
* @param input the input byte array to encode
@@ -79,6 +90,16 @@ public static String base64UrlEncodeToString(String input) {
7990
return Base64.encodeBase64URLSafeString(input.getBytes(StandardCharsets.UTF_8));
8091
}
8192

93+
/**
94+
* base64 url encode a byte array to a byte array
95+
* @param input the input byte array to encode
96+
* @param charset the charset of the input string
97+
* @return the encoded byte array
98+
*/
99+
public static String base64UrlEncodeToString(String input, Charset charset) {
100+
return Base64.encodeBase64URLSafeString(input.getBytes(charset));
101+
}
102+
82103
/**
83104
* base64 decode a byte array
84105
* @param input the input byte array to decode
@@ -103,7 +124,17 @@ public static byte[] base64BasicDecode(String input) {
103124
* @return the decoded string
104125
*/
105126
public static String base64BasicDecodeToString(String input) {
106-
return new String(Base64.decodeBase64(input));
127+
return new String(Base64.decodeBase64(input), StandardCharsets.UTF_8);
128+
}
129+
130+
/**
131+
* base64 decode a base64 encoded string
132+
* @param input the input string to decode
133+
* @param charset the charset to use when decoding the decoded bytes to string
134+
* @return the decoded string
135+
*/
136+
public static String base64BasicDecodeToString(String input, Charset charset) {
137+
return new String(Base64.decodeBase64(input), charset);
107138
}
108139

109140
/**
@@ -115,6 +146,15 @@ public static byte[] base64UrlDecode(byte[] input) {
115146
return Base64.decodeBase64(input);
116147
}
117148

149+
/**
150+
* base64 url decode a byte array
151+
* @param input the input byte array to decode
152+
* @return the decoded byte array
153+
*/
154+
public static String base64UrlDecodeToString(byte[] input) {
155+
return new String(Base64.decodeBase64(input));
156+
}
157+
118158
/**
119159
* base64 url decode a base64 url encoded string
120160
* @param input the input string to decode

src/main/java/io/nats/json/JsonParser.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 The NATS Authors
1+
// Copyright 2023-2025 The NATS Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at:
@@ -135,14 +135,7 @@ public JsonParser(char[] json, Option... options) {
135135
public JsonParser(char[] json, int startIndex, Option... options) {
136136
this.json = json;
137137

138-
boolean kn = false;
139-
for (Option o : options) {
140-
if (o == Option.KEEP_NULLS) {
141-
kn = true;
142-
break; // b/c only option currently
143-
}
144-
}
145-
keepNulls = kn;
138+
keepNulls = options != null && options.length > 0; // KEEP_NULLS is currently the only option
146139

147140
len = json == null ? 0 : json.length;
148141
idx = startIndex;
@@ -311,7 +304,7 @@ private char peekToken() {
311304
return next;
312305
}
313306

314-
// next string assumes you have already seen the starting quote
307+
// nextString() assumes you have already seen the starting quote
315308
private String nextString() throws JsonParseException {
316309
StringBuilder sb = new StringBuilder();
317310
while (true) {

src/main/java/io/nats/json/JsonSerializable.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021-2024 The NATS Authors
1+
// Copyright 2021-2025 The NATS Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at:
@@ -15,13 +15,32 @@
1515

1616
import java.nio.charset.StandardCharsets;
1717

18+
/**
19+
* Interface for objects that can automatically render as JSON
20+
*/
1821
public interface JsonSerializable {
22+
23+
/**
24+
* Get the String version of the JSON object
25+
* @return the string
26+
*/
1927
String toJson();
2028

29+
/**
30+
* Get the byte[] version of the JSON object
31+
* The built-in default implementation uses the toJson() and converts it to a string.
32+
* @return the byte array
33+
*/
2134
default byte[] serialize() {
2235
return toJson().getBytes(StandardCharsets.UTF_8);
2336
}
2437

38+
/**
39+
* Get the JsonValue version of the JSON object
40+
* The built-in default implementation uses the toJson() and parse it to a JsonValue.
41+
* It assumes that you have valid JSON
42+
* @return the JsonValue
43+
*/
2544
default JsonValue toJsonValue() {
2645
return JsonParser.parseUnchecked(toJson());
2746
}

0 commit comments

Comments
 (0)