A note about the workaround because the `` `@ JSONHint``` annotation did not work well when using JSONIC and lombok together.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>net.arnx</groupId>
<artifactId>jsonic</artifactId>
<version>1.3.10</version>
</dependency>
Suppose you want to parse a JSON that contains a date string in the following format as a sample.
String json = "{dateTime: \"2018-03-29T11:22:33.123+09:00\"}";
SampleData s = JSON.decode(json, SampleData.class);
A class that uses lombok looks like this.
import java.time.LocalDateTime;
import lombok.Data;
import net.arnx.jsonic.JSONHint;
@Data
public class SampleData {
@JSONHint(format="yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ")
private LocalDateTime dateTime;
}
This is an exception as shown below, and you can see that the `` `@ JSONHint``` annotation is not enabled.
Exception in thread "main" net.arnx.jsonic.JSONException: {dateTime=2018-03-29T11:22:33.123+09:00}Is class asdf.asdfasd.Could not convert to SampleData: $.dateTime
at net.arnx.jsonic.JSON$Context.convertInternal(JSON.java:1775)
at net.arnx.jsonic.JSON.parse(JSON.java:1155)
at net.arnx.jsonic.JSON.parse(JSON.java:1130)
at net.arnx.jsonic.JSON.decode(JSON.java:665)
at asdf.asdfasd.App.main(App.java:15)
Caused by: java.time.format.DateTimeParseException: Text '2018-03-29T11:22:33.123+09:00' could not be parsed, unparsed text found at index 23
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
The cause of this is that https://ja.osdn.net/projects/jsonic/ticket/35184 states that if there is a getter / setter, those annotations will take precedence. lombok automatically generates getters / setters, but since there is no `` `@ JSONHint``` there, this happens.
So, as a workaround, I decided to define getter / setter for the target property by myself.
@Data
public class SampleData {
/*Getter for this property/Since setter is unnecessary, automatic generation is suppressed*/
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private LocalDateTime d;
public LocalDateTime getDateTime() {
return d;
}
@JSONHint(format="yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ")
public void setDateTime(LocalDateTime dateTime) {
this.d = dateTime;
}
}
Recommended Posts