Skip to content

Add properties json adapter #370

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 1 commit into from
Apr 29, 2025
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,11 @@
package org.example.customer.properties;

import io.avaje.jsonb.Json;

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

@Json
public record Props(Properties props) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.example.customer.properties;

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

import java.util.Properties;

import org.junit.jupiter.api.Test;

import io.avaje.jsonb.JsonType;
import io.avaje.jsonb.Jsonb;

class PropsTest {

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

@Test
void toJson_fromJson() {

Properties properties = new Properties();

properties.setProperty("hi", "hey");
Props props = new Props(properties);

String asJson = jsonType.toJson(props);
Props fromJson = jsonType.fromJson(asJson);

assertThat(fromJson.props()).containsEntry("hi", "hey");
}
}
40 changes: 36 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 @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.UUID;

import io.avaje.json.JsonAdapter;
Expand All @@ -37,6 +38,7 @@
import io.avaje.json.JsonWriter;
import io.avaje.jsonb.AdapterFactory;
import io.avaje.jsonb.Jsonb;
import io.avaje.jsonb.Types;

final class BasicTypeAdapters {

Expand All @@ -49,14 +51,15 @@ final class BasicTypeAdapters {
if (type == Byte.class) return new ByteAdapter().nullSafe();
if (type == Character.class) return new CharacterAdapter().nullSafe();
if (type == Short.class) return new ShortAdapter().nullSafe();
if (type == Object.class) return new ObjectJsonAdapter(jsonb).nullSafe();
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 == Properties.class) return new PropertiesAdapter(jsonb).nullSafe();
if (type == InetAddress.class || type == Inet4Address.class || 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();

final Class<?> rawType = Util.rawType(type);
Expand Down Expand Up @@ -121,6 +124,35 @@ public String toString() {
}
}

private static final class PropertiesAdapter implements JsonAdapter<Properties> {
private JsonAdapter<Map<Object, Object>> mapAdapter;

private PropertiesAdapter(Jsonb jsonb) {
this.mapAdapter = jsonb.adapter(Types.mapOf(Object.class));
}

@Override
public Properties fromJson(JsonReader reader) {
var map = mapAdapter.fromJson(reader);
if (map == null) {
return null;
}
var properties = new Properties();
properties.putAll(map);
return properties;
}

@Override
public void toJson(JsonWriter writer, Properties value) {
mapAdapter.toJson(writer, value);
}

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

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