Skip to content

Commit 92e0461

Browse files
authored
Merge pull request #288 from Mechite/main
Add InetAddressAdapter for serialization of all InetAddress variations
2 parents 2091198 + 9e6f7f1 commit 92e0461

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.example.customer.inetaddress;
2+
3+
import io.avaje.jsonb.Json;
4+
5+
import java.net.Inet4Address;
6+
import java.net.Inet6Address;
7+
import java.net.InetAddress;
8+
9+
@Json
10+
public record MyInetAddress(
11+
Inet4Address ipv4,
12+
Inet6Address ipv6,
13+
InetAddress ipv4Generic,
14+
InetAddress ipv6Generic
15+
) {
16+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.example.customer.inetaddress;
2+
3+
import io.avaje.jsonb.JsonType;
4+
import io.avaje.jsonb.Jsonb;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.IOException;
8+
import java.net.Inet4Address;
9+
import java.net.Inet6Address;
10+
import java.net.InetAddress;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
class MyInetAddressTest {
15+
16+
Jsonb jsonb = Jsonb.builder().build();
17+
JsonType<MyInetAddress> jsonType = jsonb.type(MyInetAddress.class);
18+
19+
@Test
20+
void toJson_fromJson() throws IOException {
21+
22+
MyInetAddress myInetAddress = new MyInetAddress(
23+
(Inet4Address) InetAddress.getByName("165.124.194.133"),
24+
(Inet6Address) InetAddress.getByName("1985:5b4d:9a9e:babc:5a1d:b44e:9942:07b0"),
25+
InetAddress.getByName("165.124.194.133"),
26+
InetAddress.getByName("1985:5b4d:9a9e:babc:5a1d:b44e:9942:07b0")
27+
);
28+
29+
String asJson = jsonType.toJson(myInetAddress);
30+
MyInetAddress fromJson = jsonType.fromJson(asJson);
31+
32+
assertThat(fromJson).isEqualTo(myInetAddress);
33+
}
34+
}

jsonb/src/main/java/io/avaje/jsonb/core/BasicTypeAdapters.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
import io.avaje.jsonb.*;
1919

2020
import java.lang.reflect.Type;
21-
import java.net.MalformedURLException;
22-
import java.net.URI;
23-
import java.net.URISyntaxException;
24-
import java.net.URL;
21+
import java.net.*;
2522
import java.util.*;
2623

2724
import static java.util.Objects.requireNonNull;
@@ -51,6 +48,9 @@ final class BasicTypeAdapters {
5148
if (type == UUID.class) return new UuidAdapter().nullSafe();
5249
if (type == URL.class) return new UrlAdapter().nullSafe();
5350
if (type == URI.class) return new UriAdapter().nullSafe();
51+
if (type == InetAddress.class) return new InetAddressAdapter().nullSafe();
52+
if (type == Inet4Address.class) return new InetAddressAdapter().nullSafe();
53+
if (type == Inet6Address.class) return new InetAddressAdapter().nullSafe();
5454
if (type == StackTraceElement.class) return new StackTraceElementAdapter().nullSafe();
5555
if (type == Object.class) return new ObjectJsonAdapter(jsonb).nullSafe();
5656
if (type == Throwable.class) return new ThrowableAdapter(jsonb).nullSafe();
@@ -117,6 +117,27 @@ public String toString() {
117117
}
118118
}
119119

120+
private static final class InetAddressAdapter implements JsonAdapter<InetAddress> {
121+
@Override
122+
public InetAddress fromJson(JsonReader reader) {
123+
try {
124+
return InetAddress.getByName(reader.readString());
125+
} catch (UnknownHostException e) {
126+
throw new JsonDataException(e);
127+
}
128+
}
129+
130+
@Override
131+
public void toJson(JsonWriter writer, InetAddress value) {
132+
writer.value(value.getHostAddress());
133+
}
134+
135+
@Override
136+
public String toString() {
137+
return "JsonAdapter(InetAddress)";
138+
}
139+
}
140+
120141
private static final class StackTraceElementAdapter implements JsonAdapter<StackTraceElement> {
121142
@Override
122143
public StackTraceElement fromJson(JsonReader reader) {

0 commit comments

Comments
 (0)