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