com.github.nmorel.gwtjackson.client.AbstractConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gwt-jackson Show documentation
Show all versions of gwt-jackson Show documentation
gwt-jackson is a GWT JSON serializer/deserializer mechanism based on Jackson annotations
/*
* Copyright 2013 Nicolas Morel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.nmorel.gwtjackson.client;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.github.nmorel.gwtjackson.client.deser.map.key.KeyDeserializer;
import com.github.nmorel.gwtjackson.client.ser.map.key.KeySerializer;
/**
* @author Nicolas Morel
*/
public abstract class AbstractConfiguration {
public class PrimitiveTypeConfiguration {
private final Class type;
private PrimitiveTypeConfiguration( Class type ) {
this.type = type;
}
public PrimitiveTypeConfiguration serializer( Class serializer ) {
mapTypeToSerializer.put( type, serializer );
return this;
}
public PrimitiveTypeConfiguration deserializer( Class deserializer ) {
mapTypeToDeserializer.put( type, deserializer );
return this;
}
}
public class TypeConfiguration {
private final Class type;
private TypeConfiguration( Class type ) {
this.type = type;
}
public TypeConfiguration serializer( Class extends JsonSerializer> serializer ) {
mapTypeToSerializer.put( type, serializer );
return this;
}
public TypeConfiguration deserializer( Class extends JsonDeserializer> deserializer ) {
mapTypeToDeserializer.put( type, deserializer );
return this;
}
}
public class KeyTypeConfiguration {
private final Class type;
private KeyTypeConfiguration( Class type ) {
this.type = type;
}
public KeyTypeConfiguration serializer( Class extends KeySerializer> serializer ) {
mapTypeToKeySerializer.put( type, serializer );
return this;
}
public KeyTypeConfiguration deserializer( Class extends KeyDeserializer> deserializer ) {
mapTypeToKeyDeserializer.put( type, deserializer );
return this;
}
}
private final Map mapTypeToSerializer = new HashMap();
private final Map mapTypeToDeserializer = new HashMap();
private final Map mapTypeToKeySerializer = new HashMap();
private final Map mapTypeToKeyDeserializer = new HashMap();
private final Map mapMixInAnnotations = new HashMap();
private final List whitelist = new ArrayList();
private JsonAutoDetect.Visibility fieldVisibility = JsonAutoDetect.Visibility.DEFAULT;
private JsonAutoDetect.Visibility getterVisibility = JsonAutoDetect.Visibility.DEFAULT;
private JsonAutoDetect.Visibility isGetterVisibility = JsonAutoDetect.Visibility.DEFAULT;
private JsonAutoDetect.Visibility setterVisibility = JsonAutoDetect.Visibility.DEFAULT;
private JsonAutoDetect.Visibility creatorVisibility = JsonAutoDetect.Visibility.DEFAULT;
protected AbstractConfiguration() {
configure();
}
/**
* Return a {@link PrimitiveTypeConfiguration} to configure serializer and/or deserializer for the given primitive type.
*/
protected PrimitiveTypeConfiguration primitiveType( Class type ) {
if ( !type.isPrimitive() ) {
throw new IllegalArgumentException( "Type " + type + " is not a primitive. Call type(Class) instead" );
}
return new PrimitiveTypeConfiguration( type );
}
/**
* Return a {@link TypeConfiguration} to configure serializer and/or deserializer for the given type.
*/
protected TypeConfiguration type( Class type ) {
if ( type.isPrimitive() ) {
throw new IllegalArgumentException( "Type " + type + " is a primitive. Call primitiveType(Class) instead" );
}
return new TypeConfiguration( type );
}
/**
* Return a {@link KeyTypeConfiguration} to configure key serializer and/or deserializer for the given type.
*/
protected KeyTypeConfiguration key( Class type ) {
if ( type.isPrimitive() ) {
throw new IllegalArgumentException( "Primitive types cannot be used as a map's key" );
}
return new KeyTypeConfiguration( type );
}
/**
* Method to use for adding mix-in annotations to use for augmenting
* specified class or interface. All annotations from
* mixinSource
are taken to override annotations
* that target
(or its supertypes) has.
*
* @param target Class (or interface) whose annotations to effectively override
* @param mixinSource Class (or interface) whose annotations are to
* be "added" to target's annotations, overriding as necessary
*/
protected AbstractConfiguration addMixInAnnotations( Class> target, Class> mixinSource ) {
mapMixInAnnotations.put( target, mixinSource );
return this;
}
/**
* Method to add a regex into whitelist.
*
* All the types matching whitelist are added to the subtype list of {@link Object} and
* {@link Serializable} serializer/deserializer.
*
*
* @param regex the regex to add
*/
protected AbstractConfiguration whitelist( String regex ) {
whitelist.add( regex );
return this;
}
/**
* Override the default behaviour of {@link JsonAutoDetect.Visibility#DEFAULT} for fields.
*
* @param visibility the new default behaviour
*/
protected AbstractConfiguration fieldVisibility( JsonAutoDetect.Visibility visibility ) {
this.fieldVisibility = visibility;
return this;
}
/**
* Override the default behaviour of {@link JsonAutoDetect.Visibility#DEFAULT} for getters.
*
* @param visibility the new default behaviour
*/
protected AbstractConfiguration getterVisibility( JsonAutoDetect.Visibility visibility ) {
this.getterVisibility = visibility;
return this;
}
/**
* Override the default behaviour of {@link JsonAutoDetect.Visibility#DEFAULT} for boolean getters.
*
* @param visibility the new default behaviour
*/
protected AbstractConfiguration isGetterVisibility( JsonAutoDetect.Visibility visibility ) {
this.isGetterVisibility = visibility;
return this;
}
/**
* Override the default behaviour of {@link JsonAutoDetect.Visibility#DEFAULT} for setters.
*
* @param visibility the new default behaviour
*/
protected AbstractConfiguration setterVisibility( JsonAutoDetect.Visibility visibility ) {
this.setterVisibility = visibility;
return this;
}
/**
* Override the default behaviour of {@link JsonAutoDetect.Visibility#DEFAULT} for creators.
*
* @param visibility the new default behaviour
*/
protected AbstractConfiguration creatorVisibility( JsonAutoDetect.Visibility visibility ) {
this.creatorVisibility = visibility;
return this;
}
protected abstract void configure();
public Map getMapTypeToSerializer() {
return mapTypeToSerializer;
}
public Map getMapTypeToDeserializer() {
return mapTypeToDeserializer;
}
public Map getMapTypeToKeySerializer() {
return mapTypeToKeySerializer;
}
public Map getMapTypeToKeyDeserializer() {
return mapTypeToKeyDeserializer;
}
public Map getMapMixInAnnotations() {
return mapMixInAnnotations;
}
public List getWhitelist() {
return whitelist;
}
public Visibility getFieldVisibility() {
return fieldVisibility;
}
public Visibility getGetterVisibility() {
return getterVisibility;
}
public Visibility getIsGetterVisibility() {
return isGetterVisibility;
}
public Visibility getSetterVisibility() {
return setterVisibility;
}
public Visibility getCreatorVisibility() {
return creatorVisibility;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy