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