Commit 4f249516 authored by Matija Obreza's avatar Matija Obreza

Include class short name in JSON-serialized entities extending BasicModel

- Don't use @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="_type"), it makes deserialization difficult
parent 56db5281
......@@ -24,13 +24,17 @@ import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import org.genesys.blocks.util.JsonClassNameWriter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
/**
* The Class BasicModel.
*/
@MappedSuperclass
@JsonAppend(props = { @JsonAppend.Prop(name = "_type", value = JsonClassNameWriter.class, type = String.class) })
public class BasicModel implements EntityId, Serializable {
/** The Constant serialVersionUID. */
......@@ -111,7 +115,7 @@ public class BasicModel implements EntityId, Serializable {
}
return true;
}
@Override
public String toString() {
return super.toString() + " id=" + this.id;
......
/*
* 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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
/**
* 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.
*
*/
public class JsonClassNameWriter extends VirtualBeanPropertyWriter {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(JsonClassNameWriter.class);
/**
* 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", 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", 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);
}
}
......@@ -22,10 +22,12 @@ import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.genesys.blocks.model.EntityId;
import org.genesys.blocks.security.serialization.CurrentPermissions;
import org.genesys.blocks.security.serialization.CurrentPermissionsWriter;
import org.genesys.blocks.util.JsonClassNameWriter;
/**
* Interface label for entities that require ACL security.
*/
@JsonAppend(props = { @JsonAppend.Prop(name="_permissions", value = CurrentPermissionsWriter.class, type=CurrentPermissions.class) })
@JsonAppend(props = { @JsonAppend.Prop(name = "_type", value = JsonClassNameWriter.class, type = String.class),
@JsonAppend.Prop(name = "_permissions", value = CurrentPermissionsWriter.class, type = CurrentPermissions.class) })
public interface AclAwareModel extends Serializable, EntityId {
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment