[gson] Advertise (and fix) transient instead of @Ignore

This commit is contained in:
Johannes Frohnmeyer 2022-06-13 14:19:31 +02:00
parent 8bba3a33d5
commit 5ca20446c6
Signed by: Johannes
GPG Key ID: E76429612C2929F4
6 changed files with 7 additions and 81 deletions

View File

@ -1,53 +0,0 @@
package io.gitlab.jfronny.commons.serialize.gson;
import com.google.gson.*;
import java.lang.reflect.*;
import java.util.function.*;
/**
* Holds a common instance of the Gson object.
* Supports registering type adapters/etc. as needed
*/
@Deprecated(forRemoval = true)
public class GsonHolder {
/**
* Get the current gson instance or build it if needed
* @return The Gson instance
*/
public static Gson getGson() {
return io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder.getGson();
}
/**
* Register a type adapter and mark the gson instance as unclean
* @param type The type for which to register the adapter
* @param typeAdapter The adapter type
*/
public static void registerTypeAdapter(Type type, Object typeAdapter) {
io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder.registerTypeAdapter(type, typeAdapter);
}
/**
* Register a type adapter factory and mark the gson instance as unclean
* @param factory The factory to register
*/
public static void registerTypeAdapterFactory(TypeAdapterFactory factory) {
io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder.registerTypeAdapterFactory(factory);
}
/**
* Run a function on the builder for modifying it and mark the gson instance as unclean
* @param func The function to run
*/
public static void modifyBuilder(Consumer<GsonBuilder> func) {
io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder.modifyBuilder(func);
}
/**
* Register this in {@code SerializerHolder}
*/
public static void register() {
io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder.register();
}
}

View File

@ -1,16 +0,0 @@
package io.gitlab.jfronny.commons.serialize.gson;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Mark a class/field to be ignored by Gson.
* May be used for metadata in serialized classes
*/
@Deprecated(forRemoval = true)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface GsonIgnore {
}

View File

@ -16,8 +16,7 @@ import java.util.function.*;
public class GsonHolder { public class GsonHolder {
private static final GsonBuilder builder = new GsonBuilder() private static final GsonBuilder builder = new GsonBuilder()
.registerTypeAdapter(ComparableVersion.class, new ComparableVersionAdapter()) .registerTypeAdapter(ComparableVersion.class, new ComparableVersionAdapter())
.excludeFieldsWithModifiers(Modifier.TRANSIENT) .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.PRIVATE)
.excludeFieldsWithModifiers(Modifier.PRIVATE)
.setExclusionStrategies(new GsonIgnoreExclusionStrategy()) .setExclusionStrategies(new GsonIgnoreExclusionStrategy())
.setLenient() .setLenient()
.setPrettyPrinting(); .setPrettyPrinting();

View File

@ -5,8 +5,10 @@ import java.lang.annotation.*;
/** /**
* Mark a class/field to be ignored by Gson. * Mark a class/field to be ignored by Gson.
* May be used for metadata in serialized classes * May be used for metadata in serialized classes
* @deprecated Use {@code transient} for fields instead
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE}) @Target({ElementType.FIELD, ElementType.TYPE})
@Deprecated
public @interface Ignore { public @interface Ignore {
} }

View File

@ -1,9 +1,7 @@
package io.gitlab.jfronny.commons.serialize.gson.impl; package io.gitlab.jfronny.commons.serialize.gson.impl;
import io.gitlab.jfronny.commons.serialize.gson.*; import com.google.gson.*;
import io.gitlab.jfronny.commons.serialize.gson.api.*; import io.gitlab.jfronny.commons.serialize.gson.api.*;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
/** /**
* An exclusion strategy that ignores fields with the GsonIgnore attribute * An exclusion strategy that ignores fields with the GsonIgnore attribute
@ -11,15 +9,12 @@ import com.google.gson.FieldAttributes;
public class GsonIgnoreExclusionStrategy implements ExclusionStrategy { public class GsonIgnoreExclusionStrategy implements ExclusionStrategy {
@Override @Override
public boolean shouldSkipClass(Class<?> clazz) { public boolean shouldSkipClass(Class<?> clazz) {
return clazz.isAnnotationPresent(GsonIgnore.class) return clazz.isAnnotationPresent(Ignore.class);
|| clazz.isAnnotationPresent(Ignore.class);
} }
@Override @Override
public boolean shouldSkipField(FieldAttributes f) { public boolean shouldSkipField(FieldAttributes f) {
return f.getAnnotation(GsonIgnore.class) != null return f.getAnnotation(Ignore.class) != null
|| f.getDeclaringClass().isAnnotationPresent(GsonIgnore.class)
|| f.getAnnotation(Ignore.class) != null
|| f.getDeclaringClass().isAnnotationPresent(Ignore.class); || f.getDeclaringClass().isAnnotationPresent(Ignore.class);
} }
} }

View File

@ -23,7 +23,6 @@ public class GsonTest {
assertEquals(""" assertEquals("""
{ {
"id": "Yes", "id": "Yes",
"one": 1,
"shown": { "shown": {
"shouldShow": "Yes" "shouldShow": "Yes"
} }
@ -40,7 +39,7 @@ public class GsonTest {
String id = "Yes"; String id = "Yes";
@Ignore @Ignore
String other = "No"; String other = "No";
Integer one = 1; transient Integer one = 1;
ExampleTypeHidden type = new ExampleTypeHidden(); ExampleTypeHidden type = new ExampleTypeHidden();
TransitiveHiddenType shown = new TransitiveHiddenType(); TransitiveHiddenType shown = new TransitiveHiddenType();
} }