Skip to content

Commit 56173d5

Browse files
committed
Add properties json adapter
1 parent 94e47bc commit 56173d5

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.example.customer.properties;
2+
3+
import io.avaje.jsonb.Json;
4+
5+
import java.net.Inet4Address;
6+
import java.net.Inet6Address;
7+
import java.net.InetAddress;
8+
import java.util.Properties;
9+
10+
@Json
11+
public record Props(Properties props) {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.example.customer.properties;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Properties;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
import io.avaje.jsonb.JsonType;
10+
import io.avaje.jsonb.Jsonb;
11+
12+
class PropsTest {
13+
14+
Jsonb jsonb = Jsonb.builder().build();
15+
JsonType<Props> jsonType = jsonb.type(Props.class);
16+
17+
@Test
18+
void toJson_fromJson() {
19+
20+
Properties properties = new Properties();
21+
22+
properties.setProperty("hi", "hey");
23+
Props props = new Props(properties);
24+
25+
String asJson = jsonType.toJson(props);
26+
Props fromJson = jsonType.fromJson(asJson);
27+
28+
assertThat(fromJson.props()).containsEntry("hi", "hey");
29+
}
30+
}

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.Optional;
32+
import java.util.Properties;
3233
import java.util.UUID;
3334

3435
import io.avaje.json.JsonAdapter;
@@ -37,6 +38,7 @@
3738
import io.avaje.json.JsonWriter;
3839
import io.avaje.jsonb.AdapterFactory;
3940
import io.avaje.jsonb.Jsonb;
41+
import io.avaje.jsonb.Types;
4042

4143
final class BasicTypeAdapters {
4244

@@ -49,14 +51,15 @@ final class BasicTypeAdapters {
4951
if (type == Byte.class) return new ByteAdapter().nullSafe();
5052
if (type == Character.class) return new CharacterAdapter().nullSafe();
5153
if (type == Short.class) return new ShortAdapter().nullSafe();
54+
if (type == Object.class) return new ObjectJsonAdapter(jsonb).nullSafe();
5255
if (type == UUID.class) return new UuidAdapter().nullSafe();
5356
if (type == URL.class) return new UrlAdapter().nullSafe();
5457
if (type == URI.class) return new UriAdapter().nullSafe();
55-
if (type == InetAddress.class) return new InetAddressAdapter().nullSafe();
56-
if (type == Inet4Address.class) return new InetAddressAdapter().nullSafe();
57-
if (type == Inet6Address.class) return new InetAddressAdapter().nullSafe();
58+
if (type == Properties.class) return new PropertiesAdapter(jsonb).nullSafe();
59+
if (type == InetAddress.class || type == Inet4Address.class || type == Inet6Address.class) {
60+
return new InetAddressAdapter().nullSafe();
61+
}
5862
if (type == StackTraceElement.class) return new StackTraceElementAdapter().nullSafe();
59-
if (type == Object.class) return new ObjectJsonAdapter(jsonb).nullSafe();
6063
if (type == Throwable.class) return new ThrowableAdapter(jsonb).nullSafe();
6164

6265
final Class<?> rawType = Util.rawType(type);
@@ -121,6 +124,35 @@ public String toString() {
121124
}
122125
}
123126

127+
private static final class PropertiesAdapter implements JsonAdapter<Properties> {
128+
private JsonAdapter<Map<Object, Object>> mapAdapter;
129+
130+
private PropertiesAdapter(Jsonb jsonb) {
131+
this.mapAdapter = jsonb.adapter(Types.mapOf(Object.class));
132+
}
133+
134+
@Override
135+
public Properties fromJson(JsonReader reader) {
136+
var map = mapAdapter.fromJson(reader);
137+
if (map == null) {
138+
return null;
139+
}
140+
var properties = new Properties();
141+
properties.putAll(map);
142+
return properties;
143+
}
144+
145+
@Override
146+
public void toJson(JsonWriter writer, Properties value) {
147+
mapAdapter.toJson(writer, value);
148+
}
149+
150+
@Override
151+
public String toString() {
152+
return "JsonAdapter(Properties)";
153+
}
154+
}
155+
124156
private static final class InetAddressAdapter implements JsonAdapter<InetAddress> {
125157
@Override
126158
public InetAddress fromJson(JsonReader reader) {

0 commit comments

Comments
 (0)