Debian Codegraph @

src/main/java/ezvcard/io/json/JCardSerializer.java

raw symbols SCIP
1 package ezvcard.io.json;
2
3 import java.io.IOException;
4
5 import com.fasterxml.jackson.annotation.JsonFormat;
6 import com.fasterxml.jackson.core.JsonGenerator;
7 import com.fasterxml.jackson.core.JsonProcessingException;
8 import com.fasterxml.jackson.databind.BeanProperty;
9 import com.fasterxml.jackson.databind.JsonMappingException;
10 import com.fasterxml.jackson.databind.SerializerProvider;
11 import com.fasterxml.jackson.databind.ser.ContextualSerializer;
12 import com.fasterxml.jackson.databind.ser.std.StdSerializer;
13
14 import ezvcard.VCard;
15 import ezvcard.io.scribe.ScribeIndex;
16 import ezvcard.io.scribe.VCardPropertyScribe;
17 import ezvcard.property.ProductId;
18 import ezvcard.property.VCardProperty;
19
20 /*
21 Copyright (c) 2012-2023, Michael Angstadt
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26
27 1. Redistributions of source code must retain the above copyright notice, this
28 list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright notice,
30 this list of conditions and the following disclaimer in the documentation
31 and/or other materials provided with the distribution.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
44 The views and conclusions contained in the software and documentation are those
45 of the authors and should not be interpreted as representing official policies,
46 either expressed or implied, of the FreeBSD Project.
47 */
48
49 /**
50 * Serializes jCards within the jackson-databind framework.
51 * @author Buddy Gorven
52 */
53 @JsonFormat
54 public class JCardSerializer extends StdSerializer<VCard> implements ContextualSerializer {
55 private static final long serialVersionUID = -856795690626261178L;
56
57 private ScribeIndex index = new ScribeIndex();
58 private boolean addProdId = true;
59 private boolean versionStrict = true;
60
61 public JCardSerializer() {
62 super(VCard.class);
63 }
64
65 @Override
66 public void serialize(VCard value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
67 @SuppressWarnings("resource")
68 JCardWriter writer = new JCardWriter(gen);
69 writer.setAddProdId(isAddProdId());
70 writer.setVersionStrict(isVersionStrict());
71 writer.setScribeIndex(getScribeIndex());
72
73 writer.write(value);
74 }
75
76 public JCardSerializer createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
77 if (property == null) {
78 return this;
79 }
80
81 JCardFormat annotation = property.getAnnotation(JCardFormat.class);
82 if (annotation == null) {
83 return this;
84 }
85
86 JCardSerializer result = new JCardSerializer();
87 result.setAddProdId(annotation.addProdId());
88 result.setVersionStrict(annotation.versionStrict());
89 result.setScribeIndex(getScribeIndex());
90 return result;
91 }
92
93 /**
94 * Gets whether a {@link ProductId} property will be added to the vCard that
95 * marks it as having been generated by this library.
96 * @return true if the property will be added, false if not (defaults to
97 * true)
98 */
99 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 }