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 static de.isas.mztab2.io.serialization.Serializers.addSubElementParameters; 024import de.isas.mztab2.model.Sample; 025import java.io.IOException; 026import lombok.extern.slf4j.Slf4j; 027import uk.ac.ebi.pride.jmztab2.model.Section; 028 029/** 030 * <p>SampleSerializer implementation for {@link de.isas.mztab2.model.Sample}.</p> 031 * 032 * @author nilshoffmann 033 * 034 */ 035@Slf4j 036public class SampleSerializer extends StdSerializer<Sample> { 037 038 /** 039 * <p>Constructor for SampleSerializer.</p> 040 */ 041 public SampleSerializer() { 042 this(null); 043 } 044 045 /** 046 * <p>Constructor for SampleSerializer.</p> 047 * 048 * @param t a {@link java.lang.Class} object. 049 */ 050 public SampleSerializer(Class<Sample> t) { 051 super(t); 052 } 053 054 @Override 055 public void serializeWithType(Sample value, JsonGenerator gen, 056 SerializerProvider serializers, TypeSerializer typeSer) throws IOException { 057 typeSer.writeTypePrefixForObject(value, gen); 058 serialize(value, gen, serializers); 059 typeSer.writeTypeSuffixForObject(value, gen); 060 } 061 062 /** {@inheritDoc} */ 063 @Override 064 public void serialize(Sample sample, JsonGenerator jg, 065 SerializerProvider sp) throws IOException { 066 if (sample != null) { 067 Serializers.checkIndexedElement(sample); 068 addLineWithProperty(jg, Section.Metadata.getPrefix(), null, sample, 069 sample. 070 getName()); 071 addLineWithProperty(jg, Section.Metadata.getPrefix(), Sample.Properties.description.getPropertyName(), 072 sample, 073 sample.getDescription()); 074 addSubElementParameters(jg, Section.Metadata.getPrefix(), sample, 075 Sample.Properties.species.getPropertyName(), 076 sample.getSpecies(), false); 077 addSubElementParameters(jg, Section.Metadata.getPrefix(), sample, 078 Sample.Properties.cellType.getPropertyName(), 079 sample.getCellType(), false); 080 addSubElementParameters(jg, Section.Metadata.getPrefix(), sample, 081 Sample.Properties.disease.getPropertyName(), 082 sample.getDisease(), false); 083 addSubElementParameters(jg, Section.Metadata.getPrefix(), sample, 084 Sample.Properties.tissue.getPropertyName(), 085 sample.getTissue(), false); 086 addSubElementParameters(jg, Section.Metadata.getPrefix(), sample, 087 Sample.Properties.custom.getPropertyName(), 088 sample.getCustom(), false); 089 } else { 090 log.debug(Sample.class.getSimpleName()+" is null!"); 091 } 092 } 093}