Skip to content

Commit

Permalink
fix: reusing gson object
Browse files Browse the repository at this point in the history
  • Loading branch information
tamassoltesz committed Oct 17, 2024
1 parent 8d6e56f commit 7562d9f
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class BulkImportUser {
public Long createdAt;
public Long updatedAt;

private static final Gson gson = new Gson();

public BulkImportUser(String id, String externalUserId, JsonObject userMetadata, List<UserRole> userRoles,
List<TotpDevice> totpDevices, List<LoginMethod> loginMethods) {
this.id = id;
Expand All @@ -49,7 +51,7 @@ public BulkImportUser(String id, String externalUserId, JsonObject userMetadata,
}

public static BulkImportUser forTesting_fromJson(JsonObject jsonObject) {
return new Gson().fromJson(jsonObject, BulkImportUser.class);
return gson.fromJson(jsonObject, BulkImportUser.class);
}

// The bulk_import_users table stores users to be imported via a Cron Job.
Expand All @@ -59,7 +61,7 @@ public static BulkImportUser forTesting_fromJson(JsonObject jsonObject) {
// First, we validate all fields of `raw_data` using the BulkImportUser class, then store this data in the bulk_import_users table.
// This function retrieves the `raw_data` after removing the additional fields.
public String toRawDataForDbStorage() {
JsonObject jsonObject = new Gson().fromJson(new Gson().toJson(this), JsonObject.class);
JsonObject jsonObject = gson.fromJson(new Gson().toJson(this), JsonObject.class);
jsonObject.remove("status");
jsonObject.remove("createdAt");
jsonObject.remove("updatedAt");
Expand All @@ -71,7 +73,7 @@ public String toRawDataForDbStorage() {
// When creating an instance of the BulkImportUser class, the extra fields must be passed separately as they are not part of the `raw_data`.
// This function creates a BulkImportUser instance from a stored bulk_import_user entry.
public static BulkImportUser fromRawDataFromDbStorage(String id, String rawData, BULK_IMPORT_USER_STATUS status, String primaryUserId, String errorMessage, long createdAt, long updatedAt) {
BulkImportUser user = new Gson().fromJson(rawData, BulkImportUser.class);
BulkImportUser user = gson.fromJson(rawData, BulkImportUser.class);
user.id = id;
user.status = status;
user.primaryUserId = primaryUserId;
Expand All @@ -82,7 +84,7 @@ public static BulkImportUser fromRawDataFromDbStorage(String id, String rawData,
}

public JsonObject toJsonObject() {
return new Gson().fromJson(new Gson().toJson(this), JsonObject.class);
return gson.fromJson(gson.toJson(this), JsonObject.class);
}

public static class UserRole {
Expand Down

0 comments on commit 7562d9f

Please sign in to comment.