[gson] Advertise (and fix) transient instead of @Ignore
This commit is contained in:
parent
8bba3a33d5
commit
5ca20446c6
@ -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();
|
||||
}
|
||||
}
|
@ -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 {
|
||||
}
|
@ -16,8 +16,7 @@ import java.util.function.*;
|
||||
public class GsonHolder {
|
||||
private static final GsonBuilder builder = new GsonBuilder()
|
||||
.registerTypeAdapter(ComparableVersion.class, new ComparableVersionAdapter())
|
||||
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
|
||||
.excludeFieldsWithModifiers(Modifier.PRIVATE)
|
||||
.excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.PRIVATE)
|
||||
.setExclusionStrategies(new GsonIgnoreExclusionStrategy())
|
||||
.setLenient()
|
||||
.setPrettyPrinting();
|
||||
|
@ -5,8 +5,10 @@ import java.lang.annotation.*;
|
||||
/**
|
||||
* Mark a class/field to be ignored by Gson.
|
||||
* May be used for metadata in serialized classes
|
||||
* @deprecated Use {@code transient} for fields instead
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.TYPE})
|
||||
@Deprecated
|
||||
public @interface Ignore {
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
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 com.google.gson.ExclusionStrategy;
|
||||
import com.google.gson.FieldAttributes;
|
||||
|
||||
/**
|
||||
* An exclusion strategy that ignores fields with the GsonIgnore attribute
|
||||
@ -11,15 +9,12 @@ import com.google.gson.FieldAttributes;
|
||||
public class GsonIgnoreExclusionStrategy implements ExclusionStrategy {
|
||||
@Override
|
||||
public boolean shouldSkipClass(Class<?> clazz) {
|
||||
return clazz.isAnnotationPresent(GsonIgnore.class)
|
||||
|| clazz.isAnnotationPresent(Ignore.class);
|
||||
return clazz.isAnnotationPresent(Ignore.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldSkipField(FieldAttributes f) {
|
||||
return f.getAnnotation(GsonIgnore.class) != null
|
||||
|| f.getDeclaringClass().isAnnotationPresent(GsonIgnore.class)
|
||||
|| f.getAnnotation(Ignore.class) != null
|
||||
return f.getAnnotation(Ignore.class) != null
|
||||
|| f.getDeclaringClass().isAnnotationPresent(Ignore.class);
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ public class GsonTest {
|
||||
assertEquals("""
|
||||
{
|
||||
"id": "Yes",
|
||||
"one": 1,
|
||||
"shown": {
|
||||
"shouldShow": "Yes"
|
||||
}
|
||||
@ -40,7 +39,7 @@ public class GsonTest {
|
||||
String id = "Yes";
|
||||
@Ignore
|
||||
String other = "No";
|
||||
Integer one = 1;
|
||||
transient Integer one = 1;
|
||||
ExampleTypeHidden type = new ExampleTypeHidden();
|
||||
TransitiveHiddenType shown = new TransitiveHiddenType();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user