001/* 002 * Copyright 2018 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V.. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.isas.mztab2.io.serialization; 017 018import com.fasterxml.jackson.core.JsonGenerator; 019import com.fasterxml.jackson.databind.SerializerProvider; 020import com.fasterxml.jackson.databind.jsontype.TypeSerializer; 021import com.fasterxml.jackson.databind.ser.std.StdSerializer; 022import static de.isas.mztab2.io.serialization.Serializers.addLineWithProperty; 023import de.isas.mztab2.model.Contact; 024import java.io.IOException; 025import lombok.extern.slf4j.Slf4j; 026import uk.ac.ebi.pride.jmztab2.model.Section; 027 028/** 029 * <p>ContactSerializer implementation for {@link de.isas.mztab2.model.Contact}.</p> 030 * 031 * @author nilshoffmann 032 * 033 */ 034@Slf4j 035public class ContactSerializer extends StdSerializer<Contact> { 036 037 /** 038 * <p> 039 * Constructor for ContactSerializer.</p> 040 */ 041 public ContactSerializer() { 042 this(null); 043 } 044 045 /** 046 * <p> 047 * Constructor for ContactSerializer.</p> 048 * 049 * @param t a {@link java.lang.Class} object. 050 */ 051 public ContactSerializer(Class<Contact> t) { 052 super(t); 053 } 054 055 @Override 056 public void serializeWithType(Contact value, JsonGenerator gen, 057 SerializerProvider serializers, TypeSerializer typeSer) throws IOException { 058 typeSer.writeTypePrefixForObject(value, gen); 059 serialize(value, gen, serializers); 060 typeSer.writeTypeSuffixForObject(value, gen); 061 } 062 063 /** 064 * {@inheritDoc} 065 */ 066 @Override 067 public void serialize(Contact contact, JsonGenerator jg, 068 SerializerProvider sp) throws IOException { 069 if (contact != null) { 070 Serializers.checkIndexedElement(contact); 071 addLineWithProperty(jg, Section.Metadata.getPrefix(), 072 Contact.Properties.name.getPropertyName(), contact, 073 contact. 074 getName()); 075 addLineWithProperty(jg, Section.Metadata.getPrefix(), 076 Contact.Properties.email.getPropertyName(), contact, 077 contact. 078 getEmail()); 079 addLineWithProperty(jg, Section.Metadata.getPrefix(), 080 Contact.Properties.affiliation.getPropertyName(), contact, 081 contact.getAffiliation()); 082 083 } else { 084 log.debug(Contact.class.getSimpleName() + " is null!"); 085 } 086 } 087}