001/* 002 * Copyright 2020 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 de.isas.mztab2.model.Uri; 025import java.io.IOException; 026import static java.lang.Math.log; 027import lombok.extern.slf4j.Slf4j; 028import uk.ac.ebi.pride.jmztab2.model.Section; 029 030/** 031 * <p> 032 * UriSerializer implementation for {@link de.isas.mztab2.model.Uri}.</p> 033 * 034 * @author nilshoffmann 035 */ 036@Slf4j 037public class UriSerializer extends StdSerializer<Uri> { 038 039 /** 040 * <p> 041 * Constructor for UriSerializer.</p> 042 */ 043 public UriSerializer() { 044 this(null); 045 } 046 047 /** 048 * <p> 049 * Constructor for UriSerializer.</p> 050 * 051 * @param t a {@link java.lang.Class} object. 052 */ 053 public UriSerializer(Class<Uri> t) { 054 super(t); 055 } 056 057 @Override 058 public void serializeWithType(Uri value, JsonGenerator gen, 059 SerializerProvider serializers, TypeSerializer typeSer) throws IOException { 060 typeSer.writeTypePrefixForObject(value, gen); 061 serialize(value, gen, serializers); 062 typeSer.writeTypeSuffixForObject(value, gen); 063 } 064 065 /** 066 * {@inheritDoc} 067 */ 068 @Override 069 public void serialize(Uri uri, JsonGenerator jg, 070 SerializerProvider sp) throws IOException { 071 if (uri != null) { 072 Serializers.checkIndexedElement(uri); 073 addLineWithProperty(jg, Section.Metadata.getPrefix(), 074 Uri.Properties.value.getPropertyName(), uri, 075 uri. 076 getValue()); 077 078 } else { 079 log.debug(Uri.class.getSimpleName() + " is null!"); 080 } 081 } 082}