Skip to content

Commit

Permalink
Create custom datetime deserializer and return LocalDateTime for audi…
Browse files Browse the repository at this point in the history
…t fields
  • Loading branch information
yaphet17 committed Apr 23, 2023
1 parent 304873d commit b8f319b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/yaphet/chapa/Chapa.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.yaphet.chapa.client.ChapaClient;
import com.yaphet.chapa.client.ChapaClientImpl;
import com.yaphet.chapa.model.*;
import com.yaphet.chapa.utility.Util;

import java.util.HashMap;
import java.util.List;
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/com/yaphet/chapa/model/VerifyResponseData.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.yaphet.chapa.model;

import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.yaphet.chapa.utility.LocalDateTimeDeserializer;

import java.math.BigDecimal;
import java.time.LocalDateTime;

public class VerifyResponseData extends ResponseData {

Expand Down Expand Up @@ -56,13 +59,16 @@ public static class Data {
private String type;
private String status;
private String reference;
@SerializedName("tx_ref")
private String txRef;
private Customization customization;
private String meta;
@SerializedName("created_at")
private String createdAt;
@JsonAdapter(LocalDateTimeDeserializer.class)
private LocalDateTime createdAt;
@SerializedName("updated_at")
private String updatedAt;
@JsonAdapter(LocalDateTimeDeserializer.class)
private LocalDateTime updatedAt;

@SerializedName("first_name")
public String getFirstName() {
Expand Down Expand Up @@ -191,20 +197,20 @@ public VerifyResponseData.Data setMeta(String meta) {
return this;
}

public String getCreatedAt() {
public LocalDateTime getCreatedAt() {
return createdAt;
}

public VerifyResponseData.Data setCreatedAt(String createdAt) {
public VerifyResponseData.Data setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
return this;
}

public String getUpdatedAt() {
public LocalDateTime getUpdatedAt() {
return updatedAt;
}

public VerifyResponseData.Data setUpdatedAt(String updatedAt) {
public VerifyResponseData.Data setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.yaphet.chapa.utility;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeDeserializer implements JsonDeserializer<LocalDateTime> {

@Override
public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String dateTimeString = json.getAsString();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
return dateTime;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yaphet.chapa;
package com.yaphet.chapa.utility;

import java.lang.reflect.Type;
import java.math.BigDecimal;
Expand Down Expand Up @@ -89,7 +89,7 @@ public static SubAccountResponseData jsonToSubAccountResponseData(String jsonDat
return JSON_MAPPER.fromJson(jsonData, SubAccountResponseData.class);
}

static List<Bank> extractBanks(String jsonData) {
public static List<Bank> extractBanks(String jsonData) {
JsonObject jsonObject = JSON_MAPPER.fromJson(jsonData, JsonObject.class);
Type bankListType = new TypeToken<List<Bank>>() {}.getType();

Expand All @@ -104,7 +104,7 @@ public static String generateToken() {
return UUID.randomUUID().toString().substring(0, 8) + "_" + FORMATTER.format(now);
}

static boolean notNullAndEmpty(String value) {
public static boolean notNullAndEmpty(String value) {
return value != null && !value.isEmpty();
}
}

0 comments on commit b8f319b

Please sign in to comment.