Skip to content

Commit

Permalink
Merge pull request #288 from Mechite/main
Browse files Browse the repository at this point in the history
Add InetAddressAdapter for serialization of all InetAddress variations
  • Loading branch information
SentryMan authored Sep 16, 2024
2 parents 2091198 + 9e6f7f1 commit 92e0461
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.example.customer.inetaddress;

import io.avaje.jsonb.Json;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;

@Json
public record MyInetAddress(
Inet4Address ipv4,
Inet6Address ipv6,
InetAddress ipv4Generic,
InetAddress ipv6Generic
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.example.customer.inetaddress;

import io.avaje.jsonb.JsonType;
import io.avaje.jsonb.Jsonb;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;

import static org.assertj.core.api.Assertions.assertThat;

class MyInetAddressTest {

Jsonb jsonb = Jsonb.builder().build();
JsonType<MyInetAddress> jsonType = jsonb.type(MyInetAddress.class);

@Test
void toJson_fromJson() throws IOException {

MyInetAddress myInetAddress = new MyInetAddress(
(Inet4Address) InetAddress.getByName("165.124.194.133"),
(Inet6Address) InetAddress.getByName("1985:5b4d:9a9e:babc:5a1d:b44e:9942:07b0"),
InetAddress.getByName("165.124.194.133"),
InetAddress.getByName("1985:5b4d:9a9e:babc:5a1d:b44e:9942:07b0")
);

String asJson = jsonType.toJson(myInetAddress);
MyInetAddress fromJson = jsonType.fromJson(asJson);

assertThat(fromJson).isEqualTo(myInetAddress);
}
}
29 changes: 25 additions & 4 deletions jsonb/src/main/java/io/avaje/jsonb/core/BasicTypeAdapters.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
import io.avaje.jsonb.*;

import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.*;
import java.util.*;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -51,6 +48,9 @@ final class BasicTypeAdapters {
if (type == UUID.class) return new UuidAdapter().nullSafe();
if (type == URL.class) return new UrlAdapter().nullSafe();
if (type == URI.class) return new UriAdapter().nullSafe();
if (type == InetAddress.class) return new InetAddressAdapter().nullSafe();
if (type == Inet4Address.class) return new InetAddressAdapter().nullSafe();
if (type == Inet6Address.class) return new InetAddressAdapter().nullSafe();
if (type == StackTraceElement.class) return new StackTraceElementAdapter().nullSafe();
if (type == Object.class) return new ObjectJsonAdapter(jsonb).nullSafe();
if (type == Throwable.class) return new ThrowableAdapter(jsonb).nullSafe();
Expand Down Expand Up @@ -117,6 +117,27 @@ public String toString() {
}
}

private static final class InetAddressAdapter implements JsonAdapter<InetAddress> {
@Override
public InetAddress fromJson(JsonReader reader) {
try {
return InetAddress.getByName(reader.readString());
} catch (UnknownHostException e) {
throw new JsonDataException(e);
}
}

@Override
public void toJson(JsonWriter writer, InetAddress value) {
writer.value(value.getHostAddress());
}

@Override
public String toString() {
return "JsonAdapter(InetAddress)";
}
}

private static final class StackTraceElementAdapter implements JsonAdapter<StackTraceElement> {
@Override
public StackTraceElement fromJson(JsonReader reader) {
Expand Down

0 comments on commit 92e0461

Please sign in to comment.