JsonClassNameWriter.java
/*
* Copyright 2018 Global Crop Diversity Trust
*
* 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 org.genesys.blocks.util;
import org.genesys.blocks.model.BasicModel;
import com.fasterxml.jackson.annotation.JsonTypeId;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter;
import com.fasterxml.jackson.databind.util.Annotations;
import lombok.extern.slf4j.Slf4j;
/**
* The ClassNameWriter is applied to {@link BasicModel} and it instructs Jackson
* to to include class simple name as a virtual property.
*
* This is so we don't need to rely on {@link JsonTypeId}, which complicates
* object deserialization.
*
*/
@Slf4j
public class JsonClassNameWriter extends VirtualBeanPropertyWriter {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public JsonClassNameWriter() {
log.trace("JsonClassNameWriter");
}
/**
* Instantiates a new current permissions writer.
*
* @param propDef the prop def
* @param annotations the annotations
* @param type the type
*/
public JsonClassNameWriter(BeanPropertyDefinition propDef, Annotations annotations, JavaType type) {
super(propDef, annotations, type);
log.trace("JsonClassNameWriter {} annotations: {}", propDef.getName(), annotations);
}
/*
* (non-Javadoc)
* @see
* com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter#value(java.lang.
* Object, com.fasterxml.jackson.core.JsonGenerator,
* com.fasterxml.jackson.databind.SerializerProvider)
*/
@Override
protected Object value(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
log.trace("JsonClassNameWriter.value of {}", bean);
return bean == null ? null : bean.getClass().getSimpleName();
}
/*
* (non-Javadoc)
* @see
* com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter#withConfig(com.
* fasterxml.jackson.databind.cfg.MapperConfig,
* com.fasterxml.jackson.databind.introspect.AnnotatedClass,
* com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition,
* com.fasterxml.jackson.databind.JavaType)
*/
@Override
public VirtualBeanPropertyWriter withConfig(MapperConfig<?> config, AnnotatedClass declaringClass, BeanPropertyDefinition propDef, JavaType type) {
log.trace("JsonClassNameWriter.withConfig {}", declaringClass.getName());
return new JsonClassNameWriter(propDef, declaringClass.getAnnotations(), type);
}
}