Skip to content

Add InetAddressAdapter for serialization of all InetAddress variations #288

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
Sep 16, 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
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
Loading