001package ezvcard.io.json;
002
003import java.io.IOException;
004
005import com.fasterxml.jackson.annotation.JsonFormat;
006import com.fasterxml.jackson.core.JsonGenerator;
007import com.fasterxml.jackson.core.JsonProcessingException;
008import com.fasterxml.jackson.databind.BeanProperty;
009import com.fasterxml.jackson.databind.JsonMappingException;
010import com.fasterxml.jackson.databind.SerializerProvider;
011import com.fasterxml.jackson.databind.ser.ContextualSerializer;
012import com.fasterxml.jackson.databind.ser.std.StdSerializer;
013
014import ezvcard.VCard;
015import ezvcard.io.scribe.ScribeIndex;
016import ezvcard.io.scribe.VCardPropertyScribe;
017import ezvcard.property.ProductId;
018import ezvcard.property.VCardProperty;
019
020/*
021 Copyright (c) 2012-2023, Michael Angstadt
022 All rights reserved.
023
024 Redistribution and use in source and binary forms, with or without
025 modification, are permitted provided that the following conditions are met: 
026
027 1. Redistributions of source code must retain the above copyright notice, this
028 list of conditions and the following disclaimer. 
029 2. Redistributions in binary form must reproduce the above copyright notice,
030 this list of conditions and the following disclaimer in the documentation
031 and/or other materials provided with the distribution. 
032
033 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
034 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
035 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
037 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
039 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
040 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
041 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
042 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
043
044 The views and conclusions contained in the software and documentation are those
045 of the authors and should not be interpreted as representing official policies, 
046 either expressed or implied, of the FreeBSD Project.
047 */
048
049/**
050 * Serializes jCards within the jackson-databind framework.
051 * @author Buddy Gorven
052 */
053@JsonFormat
054public class JCardSerializer extends StdSerializer<VCard> implements ContextualSerializer {
055        private static final long serialVersionUID = -856795690626261178L;
056
057        private ScribeIndex index = new ScribeIndex();
058        private boolean addProdId = true;
059        private boolean versionStrict = true;
060
061        public JCardSerializer() {
062                super(VCard.class);
063        }
064
065        @Override
066        public void serialize(VCard value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
067                @SuppressWarnings("resource")
068                JCardWriter writer = new JCardWriter(gen);
069                writer.setAddProdId(isAddProdId());
070                writer.setVersionStrict(isVersionStrict());
071                writer.setScribeIndex(getScribeIndex());
072
073                writer.write(value);
074        }
075
076        public JCardSerializer createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
077                if (property == null) {
078                        return this;
079                }
080
081                JCardFormat annotation = property.getAnnotation(JCardFormat.class);
082                if (annotation == null) {
083                        return this;
084                }
085
086                JCardSerializer result = new JCardSerializer();
087                result.setAddProdId(annotation.addProdId());
088                result.setVersionStrict(annotation.versionStrict());
089                result.setScribeIndex(getScribeIndex());
090                return result;
091        }
092
093        /**
094         * Gets whether a {@link ProductId} property will be added to the vCard that
095         * marks it as having been generated by this library.
096         * @return true if the property will be added, false if not (defaults to
097         * true)
098         */
099        public boolean isAddProdId() {
100                return addProdId;
101        }
102
103        /**
104         * Sets whether to add a {@link ProductId} property to the vCard that marks
105         * it as having been generated by this library.
106         * @param addProdId true to add the property, false not to (defaults to
107         * true)
108         */
109        public void setAddProdId(boolean addProdId) {
110                this.addProdId = addProdId;
111        }
112
113        /**
114         * Gets whether properties that do not support jCard will be excluded from
115         * the written vCard. jCard only supports properties defined in the vCard
116         * version 4.0 specification.
117         * @return true if the properties will be excluded, false if not (defaults
118         * to true)
119         */
120        public boolean isVersionStrict() {
121                return versionStrict;
122        }
123
124        /**
125         * Sets whether properties that do not support jCard will be excluded from
126         * the written vCard. jCard only supports properties defined in the vCard
127         * version 4.0 specification.
128         * @param versionStrict true to exclude such properties, false not to
129         * (defaults to true)
130         */
131        public void setVersionStrict(boolean versionStrict) {
132                this.versionStrict = versionStrict;
133        }
134
135        /**
136         * <p>
137         * Registers a property scribe. This is the same as calling:
138         * </p>
139         * <p>
140         * {@code getScribeIndex().register(scribe)}
141         * </p>
142         * @param scribe the scribe to register
143         */
144        public void registerScribe(VCardPropertyScribe<? extends VCardProperty> scribe) {
145                index.register(scribe);
146        }
147
148        /**
149         * Gets the scribe index.
150         * @return the scribe index
151         */
152        public ScribeIndex getScribeIndex() {
153                return index;
154        }
155
156        /**
157         * Sets the scribe index.
158         * @param index the scribe index
159         */
160        public void setScribeIndex(ScribeIndex index) {
161                this.index = index;
162        }
163}