Work in progress ...
This commit is contained in:
@@ -14,7 +14,7 @@ import java.util.Set;
|
||||
|
||||
/**
|
||||
* A custom {@link TypeAdapter} implementation for selectively excluding specified fields during JSON serialization.
|
||||
*
|
||||
* <p>
|
||||
* This adapter facilitates both serialization and deserialization of objects while providing functionality to remove specified
|
||||
* fields from the serialized output. The set of excluded field names is provided during the adapter's initialization.
|
||||
*
|
||||
@@ -22,115 +22,116 @@ import java.util.Set;
|
||||
*/
|
||||
public class ExcludeFieldsAdapter<T> extends TypeAdapter<T> {
|
||||
|
||||
/**
|
||||
* The type of objects that this adapter handles for serialization and deserialization.
|
||||
*
|
||||
* This is a generic class type parameter representing the class of objects that the
|
||||
* {@link ExcludeFieldsAdapter} instance is designed to process. It ensures type
|
||||
* safety and guarantees the compatibility of the adapter with a specific object type.
|
||||
*/
|
||||
private final Class<T> type;
|
||||
/**
|
||||
* A set containing the names of fields to be excluded during JSON serialization.
|
||||
*
|
||||
* This collection is used in the {@link ExcludeFieldsAdapter} to determine which fields should be removed from the
|
||||
* serialized JSON representation of an object. The fields to be excluded are specified during the initialization
|
||||
* of the adapter.
|
||||
*/
|
||||
private final Set<String> excludedFields;
|
||||
/**
|
||||
* The type of objects that this adapter handles for serialization and deserialization.
|
||||
* <p>
|
||||
* This is a generic class type parameter representing the class of objects that the
|
||||
* {@link ExcludeFieldsAdapter} instance is designed to process. It ensures type
|
||||
* safety and guarantees the compatibility of the adapter with a specific object type.
|
||||
*/
|
||||
private final Class<T> type;
|
||||
|
||||
/**
|
||||
* An instance of the Gson library used for JSON serialization and deserialization.
|
||||
*
|
||||
* This variable holds the Gson object that is utilized to handle the conversion processes within the
|
||||
* custom serialization and deserialization logic. If not explicitly provided during initialization,
|
||||
* it can be lazily initialized using a GsonBuilder.
|
||||
*/
|
||||
private Gson gson;
|
||||
/**
|
||||
* A set containing the names of fields to be excluded during JSON serialization.
|
||||
* <p>
|
||||
* This collection is used in the {@link ExcludeFieldsAdapter} to determine which fields should be removed from the
|
||||
* serialized JSON representation of an object. The fields to be excluded are specified during the initialization
|
||||
* of the adapter.
|
||||
*/
|
||||
private final Set<String> excludedFields;
|
||||
|
||||
/**
|
||||
* A {@link GsonBuilder} instance used for configuring and creating {@link Gson} objects within the adapter.
|
||||
*
|
||||
* This variable serves as a reference to a GsonBuilder that allows customization of the Gson instance,
|
||||
* including registering custom type adapters, serializers, and deserializers, as well as adjusting serialization policies.
|
||||
*
|
||||
* The {@link #gsonBuilder} is primarily utilized when an existing {@link Gson} instance is not directly provided.
|
||||
* It ensures that the adapter can defer the creation of a Gson object until it is explicitly required.
|
||||
*/
|
||||
private GsonBuilder gsonBuilder;
|
||||
/**
|
||||
* An instance of the Gson library used for JSON serialization and deserialization.
|
||||
* <p>
|
||||
* This variable holds the Gson object that is utilized to handle the conversion processes within the
|
||||
* custom serialization and deserialization logic. If not explicitly provided during initialization,
|
||||
* it can be lazily initialized using a GsonBuilder.
|
||||
*/
|
||||
private Gson gson;
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@code ExcludeFieldsAdapter} for managing the serialization and deserialization
|
||||
* of objects of the specified {@code type}, while excluding certain fields during serialization.
|
||||
*
|
||||
* @param type the class type of the objects to be serialized and deserialized
|
||||
* @param excludedFields the set of field names to be excluded during serialization
|
||||
* @param gson the Gson instance to be used for serialization and deserialization
|
||||
*/
|
||||
public ExcludeFieldsAdapter(Class<T> type, Set<String> excludedFields, Gson gson) {
|
||||
this.type = type;
|
||||
this.excludedFields = new HashSet<>(excludedFields);
|
||||
this.gsonBuilder = null;
|
||||
this.gson = gson;
|
||||
}
|
||||
/**
|
||||
* A {@link GsonBuilder} instance used for configuring and creating {@link Gson} objects within the adapter.
|
||||
* <p>
|
||||
* This variable serves as a reference to a GsonBuilder that allows customization of the Gson instance,
|
||||
* including registering custom type adapters, serializers, and deserializers, as well as adjusting serialization policies.
|
||||
* <p>
|
||||
* The {@link #gsonBuilder} is primarily utilized when an existing {@link Gson} instance is not directly provided.
|
||||
* It ensures that the adapter can defer the creation of a Gson object until it is explicitly required.
|
||||
*/
|
||||
private GsonBuilder gsonBuilder;
|
||||
|
||||
/**
|
||||
* Constructs an instance of {@code ExcludeFieldsAdapter} that selectively excludes specified fields from
|
||||
* the JSON output based on the configuration provided during initialization.
|
||||
*
|
||||
* @param type the class of the type to be serialized and deserialized
|
||||
* @param excludedFields a set of field names to be excluded from the serialized JSON output
|
||||
* @param gsonBuilder an instance of {@code GsonBuilder} used to create a custom {@code Gson} instance if needed
|
||||
*/
|
||||
public ExcludeFieldsAdapter(Class<T> type, Set<String> excludedFields, GsonBuilder gsonBuilder) {
|
||||
this.type = type;
|
||||
this.excludedFields = new HashSet<>(excludedFields);
|
||||
this.gsonBuilder = gsonBuilder;
|
||||
this.gson = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazily initializes and retrieves the {@link Gson} instance. If the instance is null, it creates a new {@link Gson}
|
||||
* object using the {@link GsonBuilder} provided during the adapter's initialization.
|
||||
*
|
||||
* @return the {@link Gson} instance used for serialization and deserialization
|
||||
*/
|
||||
private Gson getGson() {
|
||||
if (gson == null) {
|
||||
gson = gsonBuilder.create();
|
||||
}
|
||||
return gson;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes an object of type {@code T} into JSON format, excluding certain fields specified during the initialization of this adapter.
|
||||
* The object is first converted into a {@link JsonObject}, then the fields listed in the excluded fields set are removed from the JSON
|
||||
* representation before writing it to the provided {@link JsonWriter}.
|
||||
*
|
||||
* @param out the {@link JsonWriter} to write the serialized JSON data to
|
||||
* @param value the object of type {@code T} to serialize into JSON
|
||||
* @throws IOException if an I/O error occurs during writing
|
||||
*/
|
||||
@Override
|
||||
public void write(JsonWriter out, T value) throws IOException {
|
||||
JsonObject obj = getGson().toJsonTree(value).getAsJsonObject();
|
||||
|
||||
for (String field : excludedFields) {
|
||||
obj.remove(field);
|
||||
/**
|
||||
* Creates a new instance of {@code ExcludeFieldsAdapter} for managing the serialization and deserialization
|
||||
* of objects of the specified {@code type}, while excluding certain fields during serialization.
|
||||
*
|
||||
* @param type the class type of the objects to be serialized and deserialized
|
||||
* @param excludedFields the set of field names to be excluded during serialization
|
||||
* @param gson the Gson instance to be used for serialization and deserialization
|
||||
*/
|
||||
public ExcludeFieldsAdapter(Class<T> type, Set<String> excludedFields, Gson gson) {
|
||||
this.type = type;
|
||||
this.excludedFields = new HashSet<>(excludedFields);
|
||||
this.gsonBuilder = null;
|
||||
this.gson = gson;
|
||||
}
|
||||
|
||||
Streams.write(obj, out);
|
||||
}
|
||||
/**
|
||||
* Constructs an instance of {@code ExcludeFieldsAdapter} that selectively excludes specified fields from
|
||||
* the JSON output based on the configuration provided during initialization.
|
||||
*
|
||||
* @param type the class of the type to be serialized and deserialized
|
||||
* @param excludedFields a set of field names to be excluded from the serialized JSON output
|
||||
* @param gsonBuilder an instance of {@code GsonBuilder} used to create a custom {@code Gson} instance if needed
|
||||
*/
|
||||
public ExcludeFieldsAdapter(Class<T> type, Set<String> excludedFields, GsonBuilder gsonBuilder) {
|
||||
this.type = type;
|
||||
this.excludedFields = new HashSet<>(excludedFields);
|
||||
this.gsonBuilder = gsonBuilder;
|
||||
this.gson = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a JSON input stream and deserializes it into an object of the specified type.
|
||||
*
|
||||
* @param in the {@link JsonReader} to read the JSON input from
|
||||
* @return an instance of type {@code T} deserialized from the JSON input
|
||||
* @throws IOException if an error occurs during reading or deserialization
|
||||
*/
|
||||
@Override
|
||||
public T read(JsonReader in) throws IOException {
|
||||
return getGson().fromJson(in, type);
|
||||
}
|
||||
/**
|
||||
* Serializes an object of type {@code T} into JSON format, excluding certain fields specified during the initialization of this adapter.
|
||||
* The object is first converted into a {@link JsonObject}, then the fields listed in the excluded fields set are removed from the JSON
|
||||
* representation before writing it to the provided {@link JsonWriter}.
|
||||
*
|
||||
* @param out the {@link JsonWriter} to write the serialized JSON data to
|
||||
* @param value the object of type {@code T} to serialize into JSON
|
||||
* @throws IOException if an I/O error occurs during writing
|
||||
*/
|
||||
@Override
|
||||
public void write(JsonWriter out, T value) throws IOException {
|
||||
JsonObject obj = getGson().toJsonTree(value).getAsJsonObject();
|
||||
|
||||
for (String field : excludedFields) {
|
||||
obj.remove(field);
|
||||
}
|
||||
|
||||
Streams.write(obj, out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazily initializes and retrieves the {@link Gson} instance. If the instance is null, it creates a new {@link Gson}
|
||||
* object using the {@link GsonBuilder} provided during the adapter's initialization.
|
||||
*
|
||||
* @return the {@link Gson} instance used for serialization and deserialization
|
||||
*/
|
||||
private Gson getGson() {
|
||||
if (gson == null) {
|
||||
gson = gsonBuilder.create();
|
||||
}
|
||||
return gson;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a JSON input stream and deserializes it into an object of the specified type.
|
||||
*
|
||||
* @param in the {@link JsonReader} to read the JSON input from
|
||||
* @return an instance of type {@code T} deserialized from the JSON input
|
||||
* @throws IOException if an error occurs during reading or deserialization
|
||||
*/
|
||||
@Override
|
||||
public T read(JsonReader in) throws IOException {
|
||||
return getGson().fromJson(in, type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
package de.neitzel.gson.adapter;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.Instant;
|
||||
@@ -18,35 +24,35 @@ import java.time.format.DateTimeParseException;
|
||||
*/
|
||||
public class InstantTypeAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
|
||||
|
||||
/**
|
||||
* Serializes an {@link Instant} object into its ISO-8601 string representation as a {@link JsonElement}.
|
||||
*
|
||||
* @param src the {@link Instant} object to be serialized
|
||||
* @param typeOfSrc the specific genericized type of {@code src} (typically ignored in this implementation)
|
||||
* @param context the context of the serialization process to provide custom serialization logic
|
||||
* @return a {@link JsonPrimitive} containing the ISO-8601 formatted string representation of the {@link Instant}
|
||||
*/
|
||||
@Override
|
||||
public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(src.toString()); // ISO-8601: "2025-04-02T10:15:30Z"
|
||||
}
|
||||
/**
|
||||
* Serializes an {@link Instant} object into its ISO-8601 string representation as a {@link JsonElement}.
|
||||
*
|
||||
* @param src the {@link Instant} object to be serialized
|
||||
* @param typeOfSrc the specific genericized type of {@code src} (typically ignored in this implementation)
|
||||
* @param context the context of the serialization process to provide custom serialization logic
|
||||
* @return a {@link JsonPrimitive} containing the ISO-8601 formatted string representation of the {@link Instant}
|
||||
*/
|
||||
@Override
|
||||
public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(src.toString()); // ISO-8601: "2025-04-02T10:15:30Z"
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes a JSON element into an {@link Instant} object.
|
||||
*
|
||||
* @param json the JSON data being deserialized
|
||||
* @param typeOfT the specific genericized type of the object to deserialize
|
||||
* @param context the deserialization context
|
||||
* @return the deserialized {@link Instant} object
|
||||
* @throws JsonParseException if the JSON element does not represent a valid ISO-8601 instant format
|
||||
*/
|
||||
@Override
|
||||
public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
try {
|
||||
return Instant.parse(json.getAsString());
|
||||
} catch (DateTimeParseException e) {
|
||||
throw new JsonParseException("Invalid Instant format", e);
|
||||
/**
|
||||
* Deserializes a JSON element into an {@link Instant} object.
|
||||
*
|
||||
* @param json the JSON data being deserialized
|
||||
* @param typeOfT the specific genericized type of the object to deserialize
|
||||
* @param context the deserialization context
|
||||
* @return the deserialized {@link Instant} object
|
||||
* @throws JsonParseException if the JSON element does not represent a valid ISO-8601 instant format
|
||||
*/
|
||||
@Override
|
||||
public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
try {
|
||||
return Instant.parse(json.getAsString());
|
||||
} catch (DateTimeParseException e) {
|
||||
throw new JsonParseException("Invalid Instant format", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user