--
You received this message because you are subscribed to the Google Groups "Genson user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to genson+un...@googlegroups.com.
To post to this group, send email to gen...@googlegroups.com.
Visit this group at http://groups.google.com/group/genson.
To view this discussion on the web visit https://groups.google.com/d/msgid/genson/64cc2811-34d2-4352-bf24-232551ee1990%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
public class IgnoreFieldContextualFactory implements ContextualFactory<Object>
{
@Override
public Converter<Object> create(BeanProperty property, Genson genson)
{
return new RuntimePropertyFilter(property);
}
}
public class RuntimePropertyFilter implements Converter<Object>
{
private final BeanProperty property;
public RuntimePropertyFilter(BeanProperty property)
{
this.property = property;
}
@Override
public void serialize(Object object, ObjectWriter writer, Context ctx) throws TransformationException, IOException
{
QueryOptions queryOptions = A.PP.registryGet(FilterInjectableProvider.QUERY_OPTIONS_KEY);
if (queryOptions != null)
{
List<String> includeFields = queryOptions.getFieldsToInclude();
List<String> excludeFields = queryOptions.getFieldsToExclude();
// Only serialize these fields if they have been specified in the include-list.
if (includeFields != null && includeFields.size() > 0)
{
if (includeFields.contains(property.getName()))
{
ctx.genson.serialize(object, property.getType(), writer, ctx);
}
}
// Otherwise, if an exclude-list exists, only serialize properties that are not in this list.
else if (excludeFields != null && excludeFields.size() > 0)
{
if (!excludeFields.contains(property.getName()))
{
ctx.genson.serialize(object, property.getType(), writer, ctx);
}
}
}
else
{
// Default handling if no filtered fields have been specified.
ctx.genson.serialize(object, property.getType(), writer, ctx);
}
}
@Override
public Object deserialize(ObjectReader reader, Context ctx) throws TransformationException, IOException
{
// Default handling here.
return ctx.genson.deserialize(GenericType.of(property.getType()), reader, ctx);
}
}
public static final Genson genson()
{
BeanMutatorAccessorResolver resolver = new BeanMutatorAccessorResolver.CompositeResolver(Arrays.asList(
new BeanMutatorAccessorResolver.GensonAnnotationsResolver(),
new SetterResolver(),
new BeanMutatorAccessorResolver.StandardMutaAccessorResolver()));
Genson genson = new Genson.Builder()
.setSkipNull(true)
.setWithClassMetadata(false)
.setWithDebugInfoPropertyNameResolver(true)
.setWithBeanViewConverter(true)
.withConverters(new IdConverter(), new UpdateConverter())
.withConverterFactory(ContextObjectConverter.factoryInstance)
.withContextualFactory(new IgnoreFieldContextualFactory()) // <--------- REGISERED HERE
.set(resolver)
.create();
return genson;
}
To view this discussion on the web visit https://groups.google.com/d/msgid/genson/6cfc1f52-1e2c-49cd-b38a-ce00d5d06afc%40googlegroups.com.
I am using a derivation of this code but it does not call serialize method of the propertyfilter:@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})@Retention(RetentionPolicy.RUNTIME)@Inherited@Documentedpublic @interface SerializationProperty{/**The name of that property.*/String value() default "";/**Whether this property must be serialized. Default is true, the property will be serialized.*/boolean serialize() default true;/**Whether this property must be deserialized. Default is true, the property will bedeserialized.*/boolean deserialize() default true;}public class Serialization{private static Serialization _instance;private final Genson jsonLib;private Serialization(){jsonLib = new Genson.Builder().setSkipNull(true).withContextualFactory(new SpecialFieldContextualFactory()).create();}public static Serialization Instance(){if(_instance == null){_instance = new Serialization();}return _instance;}public String Serialize(Object object){String json = jsonLib.serialize(object);return json;}public <T> T Deserialize(String jsonString, Class<T> type){T object = jsonLib.deserialize(jsonString, type);return object;}private class SpecialFieldContextualFactory implements ContextualFactory<Object>
{@Overridepublic Converter<Object> create(BeanProperty property, Genson genson){return new RuntimePropertyFilter(property);}}
private class RuntimePropertyFilter implements Converter<Object>
{private final BeanProperty property;public RuntimePropertyFilter(BeanProperty property){this.property = property;}@Override
public void serialize(Object object, ObjectWriter writer, Context ctx) throws Exception{SerializationProperty annotation1 = property.getAnnotation(SerializationProperty.class);if(annotation1 != null && !annotation1.serialize()){return;
}ctx.genson.serialize(object, property.getType(), writer, ctx);}@Override
public Object deserialize(ObjectReader reader, Context ctx) throws Exception{SerializationProperty annotation1 = property.getAnnotation(SerializationProperty.class);if(annotation1 != null && !annotation1.deserialize()){return null;}return ctx.genson.deserialize(GenericType.of(property.getType()), reader, ctx);
To view this discussion on the web visit https://groups.google.com/d/msgid/genson/571f0457-74aa-4ea5-b4bc-31a2dba06d3c%40googlegroups.com.