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.CV;
024import java.io.IOException;
025import lombok.extern.slf4j.Slf4j;
026import uk.ac.ebi.pride.jmztab2.model.Section;
027
028/**
029 * <p>CvSerializer implementation for {@link de.isas.mztab2.model.CV}.</p>
030 *
031 * @author nilshoffmann
032 *
033 */
034@Slf4j
035public class CvSerializer extends StdSerializer<CV> {
036
037    /**
038     * <p>
039     * Constructor for CvSerializer.</p>
040     */
041    public CvSerializer() {
042        this(null);
043    }
044
045    /**
046     * <p>
047     * Constructor for CvSerializer.</p>
048     *
049     * @param t a {@link java.lang.Class} object.
050     */
051    public CvSerializer(Class<CV> t) {
052        super(t);
053    }
054
055    @Override
056    public void serializeWithType(CV 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(CV cv, JsonGenerator jg,
068        SerializerProvider sp) throws IOException {
069        if (cv != null) {
070            Serializers.checkIndexedElement(cv);
071            addLineWithProperty(jg, Section.Metadata.getPrefix(),
072                CV.Properties.label.getPropertyName(), cv,
073                cv.
074                    getLabel());
075            addLineWithProperty(jg, Section.Metadata.getPrefix(),
076                CV.Properties.uri.getPropertyName(), cv,
077                cv.
078                    getUri());
079            addLineWithProperty(jg, Section.Metadata.getPrefix(),
080                CV.Properties.version.getPropertyName(), cv,
081                cv.getVersion());
082            addLineWithProperty(jg, Section.Metadata.getPrefix(),
083                CV.Properties.fullName.getPropertyName(), cv,
084                cv.getFullName());
085
086        } else {
087           log.debug(CV.class.getSimpleName() + " is null!");
088        }
089    }
090}