client: serialize LocalDate as 2009-01-01 (FINERACT-1234)

This commit is contained in:
Michael Vorburger
2020-10-23 06:47:04 +03:00
committed by Petri Tuomola
parent af1509abee
commit ff0db483cd
6 changed files with 254 additions and 8 deletions
@@ -147,6 +147,15 @@ import retrofit2.converter.scalars.ScalarsConverterFactory;
*/
public final class FineractClient {
/**
* Constant to be used in requests where Fineract's API requires a dateFormat to be given. This matches the format
* in which LocalDate instances are serialized. (BTW: In a Java client API, it seems weird to have strong LocalDate
* (not String) instances, and then have to specify its format, see
* https://issues.apache.org/jira/browse/FINERACT-1233.)
*/
// Matching org.apache.fineract.client.util.JSON.LocalDateTypeAdapter.formatter
public static final String DATE_FORMAT = "yyyy-mm-dd";
private final Retrofit retrofit;
public final AccountingClosureApi glClosures;
@@ -97,22 +97,21 @@ class JSON {
}
/**
* GSON TypeAdapter for JSR-310 LocalDate type in Fineract API Format (<tt>[2009,1,1]</tt>).
* GSON TypeAdapter for JSR-310 LocalDate type, which Fineract API's RETURNS as JSON array in the
* <tt>[2009,1,1]</tt> format, but EXPECTS as String not Array and with a locale and dateFormat. Weird, but so it is
* (see FINERACT-1220 & FINERACT-1233).
*/
public static class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
// Now unused: private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
// NB this format is referenced from org.apache.fineract.client.util.FineractClient.DATE_FORMAT
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
@Override
public void write(JsonWriter out, LocalDate date) throws IOException {
if (date == null) {
out.nullValue();
} else {
out.beginArray();
out.value(date.getYear());
out.value(date.getMonthValue());
out.value(date.getDayOfMonth());
out.endArray();
out.value(formatter.format(date));
}
}