Skip to content

Commit a490392

Browse files
SentryManrbygrave
andauthored
Fix Json.Creator Interaction with Json.Property (#244)
* fix JsonCreator interaction with Property * Remove duplicate f.fieldName().equals(name) * Add test and add Adapter for Duration --------- Co-authored-by: Rob Bygrave <[email protected]>
1 parent 2c910de commit a490392

File tree

6 files changed

+183
-3
lines changed

6 files changed

+183
-3
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.example.customer.creator;
2+
3+
import io.avaje.jsonb.Json;
4+
5+
import java.time.Duration;
6+
import java.time.Instant;
7+
import java.util.UUID;
8+
9+
@Json
10+
public final class PropertyCreator {
11+
12+
@Json.Property("uid")
13+
private final String identifier;
14+
15+
@Json.Property("stp")
16+
private final Instant startup;
17+
18+
@Json.Creator
19+
public PropertyCreator(
20+
String identifier,
21+
Instant startup) {
22+
this.identifier = identifier;
23+
this.startup = startup;
24+
}
25+
26+
public PropertyCreator(Instant startup) {
27+
this.identifier = Instant.now().toEpochMilli() + "-" + UUID.randomUUID();
28+
this.startup = startup;
29+
}
30+
31+
public String identifier() {
32+
return this.identifier;
33+
}
34+
35+
public Instant startup() {
36+
return this.startup;
37+
}
38+
39+
@Json.Property("upt")
40+
public Duration uptime() {
41+
return Duration.ofMillis(1);
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.example.customer.creator;
2+
3+
import io.avaje.jsonb.Jsonb;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.time.Instant;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
class PropertyCreatorTest {
11+
12+
final Jsonb jsonb = Jsonb.builder().build();
13+
14+
@Test
15+
void asJson() {
16+
var instant = Instant.ofEpochMilli(1);
17+
var pc = new PropertyCreator("strVal", instant);
18+
19+
String json = jsonb.toJson(pc);
20+
PropertyCreator fromJson = jsonb.type(PropertyCreator.class).fromJson(json);
21+
22+
assertThat(fromJson.identifier()).isEqualTo("strVal");
23+
assertThat(fromJson.identifier()).isEqualTo(pc.identifier());
24+
assertThat(fromJson.startup()).isEqualTo(pc.startup());
25+
assertThat(fromJson.uptime()).isEqualTo(pc.uptime());
26+
}
27+
}

jsonb-generator/src/main/java/io/avaje/jsonb/generator/Processor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ private void writeAdapter(TypeElement typeElement, BeanReader beanReader) {
307307
if (beanReader.hasJsonAnnotation()) {
308308
logError("Error JsonAdapter due to nonAccessibleField for %s ", beanReader);
309309
}
310+
logNote("Skipped writing JsonAdapter for %s due to non accessible fields", beanReader);
310311
return;
311312
}
312313
try {

jsonb-generator/src/main/java/io/avaje/jsonb/generator/TypeReader.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,10 @@ void read(TypeElement type) {
136136
for (var param : constructor.getParams()) {
137137
var name = param.name();
138138
var element = param.element();
139-
var matchingField = localFields.stream()
140-
.filter(f -> f.propertyName().equals(name))
141-
.findFirst();
139+
var matchingField =
140+
localFields.stream()
141+
.filter(f -> f.propertyName().equals(name) || f.fieldName().equals(name))
142+
.findFirst();
142143
matchingField.ifPresentOrElse(f -> f.readParam(element), () -> readField(element, localFields));
143144
}
144145
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package io.avaje.jsonb.generator.models.valid;
2+
3+
import java.io.IOException;
4+
import java.io.Serializable;
5+
import java.time.Duration;
6+
import java.time.Instant;
7+
import java.time.temporal.Temporal;
8+
import java.util.ArrayList;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Properties;
12+
import java.util.Set;
13+
import java.util.UUID;
14+
import java.util.function.Consumer;
15+
16+
import io.avaje.jsonb.Json;
17+
18+
@Json
19+
public final class PropertyCreator {
20+
21+
private final transient String[] arguments;
22+
23+
private final transient List<Consumer<?>> hooks;
24+
25+
@Json.Property("uid")
26+
private final String identifier;
27+
28+
@Json.Property("stp")
29+
private final Instant startup;
30+
31+
@Json.Property("cfg")
32+
private final Properties configuration;
33+
34+
@Json.Property("ins")
35+
private final Set<PropertyCreator> instances;
36+
37+
@Json.Creator
38+
public PropertyCreator(
39+
String identifier,
40+
Instant startup,
41+
Properties configuration,
42+
Set<PropertyCreator> instances) {
43+
this.arguments = null;
44+
this.hooks = null;
45+
46+
this.identifier = identifier;
47+
this.startup = startup;
48+
this.configuration = configuration;
49+
this.instances = null;
50+
}
51+
52+
public PropertyCreator(String[] arguments, Instant startup) throws IOException {
53+
this.arguments = arguments != null ? arguments : new String[0];
54+
this.hooks = new ArrayList<>();
55+
56+
this.identifier = Instant.now().toEpochMilli() + "-" + UUID.randomUUID();
57+
this.startup = startup;
58+
59+
this.configuration = new Properties();
60+
this.configuration.putAll(System.getenv());
61+
this.configuration.putAll(System.getProperties());
62+
63+
this.instances = new HashSet<>();
64+
}
65+
66+
public String[] arguments() {
67+
return arguments;
68+
}
69+
70+
public String identifier() {
71+
return this.identifier;
72+
}
73+
74+
public Instant startup() {
75+
return this.startup;
76+
}
77+
78+
public Properties configuration() {
79+
return this.configuration;
80+
}
81+
82+
public Set<PropertyCreator> instances() {
83+
return this.instances;
84+
}
85+
86+
@Json.Property("upt")
87+
public Duration uptime() {
88+
return Duration.ofMillis(1);
89+
}
90+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ final class JavaTimeAdapters {
2828
if (type == ZoneId.class) return JavaTimeAdapters.ZONE_ID_ADAPTER.nullSafe();
2929
if (type == ZoneOffset.class) return JavaTimeAdapters.ZONE_OFFSET_ADAPTER.nullSafe();
3030
if (type == Date.class) return JavaTimeAdapters.UTIL_DATE.nullSafe();
31+
if (type == Duration.class) return JavaTimeAdapters.DURATION_ADAPTER.nullSafe();
3132

3233
return null;
3334
};
@@ -53,6 +54,23 @@ public String toString() {
5354
}
5455
};
5556

57+
private static final JsonAdapter<Duration> DURATION_ADAPTER = new JsonAdapter<>() {
58+
@Override
59+
public Duration fromJson(JsonReader reader) {
60+
return Duration.parse(reader.readString());
61+
}
62+
63+
@Override
64+
public void toJson(JsonWriter writer, Duration value) {
65+
writer.value(value.toString());
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "JsonAdapter(Duration)";
71+
}
72+
};
73+
5674
private static final JsonAdapter<Instant> INSTANT_ADAPTER = new JsonAdapter<>() {
5775
@Override
5876
public Instant fromJson(JsonReader reader) {

0 commit comments

Comments
 (0)