Skip to content

Commit

Permalink
feat(UserManager): New methods and deprecation
Browse files Browse the repository at this point in the history
Changed UserManager#initializeGsonBuilder to UserManager#registerObjectMapSerializer
Deprecated UserManager#registerObjectMapSerializer (won't be removed)
Added two new methods to register TypeAdapters
  • Loading branch information
GeorgeV220 committed Apr 8, 2023
1 parent 9468dea commit e6bbb19
Showing 1 changed file with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.annotations.Beta;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import org.bson.Document;
Expand All @@ -23,7 +24,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -78,16 +78,51 @@ public UserManager(@NotNull Type type, Object obj, @Nullable String collectionNa
}
}

public UserManager initializeGsonBuilder() {
/**
* Adds a type adapter for ObjectMap to the GsonBuilder. Deprecated; use {@link #registerTypeAdaptersByClass} or {@link #registerTypeAdaptersByTypeToken} instead.
* <p>
* This method will not be removed, but it is recommended to use the newer methods for registering type adapters.
*
* @return this UserManager
* @deprecated Use {@link #registerTypeAdaptersByClass(ObjectMap.PairDocument)} or {@link #registerTypeAdaptersByTypeToken(ObjectMap.PairDocument)} instead.
*/
@Deprecated
public UserManager registerObjectMapSerializer() {
gsonBuilder.registerTypeAdapter(ObjectMap.class, new ObjectMapSerializerDeserializer());
return this;
}

public UserManager registerTypeAdapters(@NotNull List<ObjectMap.Pair<Class<?>, Object>> pairs) {
pairs.forEach(pair -> gsonBuilder.registerTypeAdapter(pair.key(), pair.value()));
/**
* Registers type adapters for the specified classes using the GsonBuilder.
*
* @param pairs a PairDocument containing the Class and type adapter pairs to register
* @return this UserManager
*/
public UserManager registerTypeAdaptersByClass(@NotNull ObjectMap.PairDocument<Class<?>, Object> pairs) {
for (ObjectMap.Pair<Class<?>, Object> pair : pairs.objectPairs()) {
gsonBuilder.registerTypeAdapter(pair.key(), pair.value());
}
return this;
}

/**
* Registers type adapters for the specified TypeTokens using the GsonBuilder.
*
* @param pairs a PairDocument containing the TypeToken and type adapter pairs to register
* @return this UserManager
*/
public UserManager registerTypeAdaptersByTypeToken(@NotNull ObjectMap.PairDocument<TypeToken<?>, Object> pairs) {
for (ObjectMap.Pair<TypeToken<?>, Object> pair : pairs.objectPairs()) {
gsonBuilder.registerTypeAdapter(pair.key().getType(), pair.value());
}
return this;
}

/**
* Builds a new Gson instance with the registered type adapters and pretty printing enabled.
*
* @return the new Gson instance
*/
public Gson getGson() {
return gsonBuilder.setPrettyPrinting().create();
}
Expand Down

0 comments on commit e6bbb19

Please sign in to comment.