| 1 | package ezvcard.io.json; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import com.fasterxml.jackson.core.JsonParser; |
| 6 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 7 | import com.fasterxml.jackson.databind.DeserializationContext; |
| 8 | import com.fasterxml.jackson.databind.JsonDeserializer; |
| 9 | |
| 10 | import ezvcard.VCard; |
| 11 | import ezvcard.io.scribe.ScribeIndex; |
| 12 | import ezvcard.io.scribe.VCardPropertyScribe; |
| 13 | import ezvcard.property.VCardProperty; |
| 14 | |
| 15 | /* |
| 16 | Copyright (c) 2012-2023, Michael Angstadt |
| 17 | All rights reserved. |
| 18 | |
| 19 | Redistribution and use in source and binary forms, with or without |
| 20 | modification, are permitted provided that the following conditions are met: |
| 21 | |
| 22 | 1. Redistributions of source code must retain the above copyright notice, this |
| 23 | list of conditions and the following disclaimer. |
| 24 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 25 | this list of conditions and the following disclaimer in the documentation |
| 26 | and/or other materials provided with the distribution. |
| 27 | |
| 28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 29 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 30 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 31 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 32 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 33 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 34 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 35 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 37 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | |
| 39 | The views and conclusions contained in the software and documentation are those |
| 40 | of the authors and should not be interpreted as representing official policies, |
| 41 | either expressed or implied, of the FreeBSD Project. |
| 42 | */ |
| 43 | |
| 44 | /** |
| 45 | * Deserializes jCards within the jackson-databind framework. |
| 46 | * @author Buddy Gorven |
| 47 | */ |
| 48 | public class JCardDeserializer extends JsonDeserializer<VCard> { |
| 49 | private ScribeIndex index = new ScribeIndex(); |
| 50 | |
| 51 | @Override |
| 52 | public VCard deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { |
| 53 | @SuppressWarnings("resource") |
| 54 | JCardReader reader = new JCardReader(parser); |
| 55 | reader.setScribeIndex(index); |
| 56 | return reader.readNext(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * <p> |
| 61 | * Registers a property scribe. This is the same as calling: |
| 62 | * </p> |
| 63 | * <p> |
| 64 | * {@code getScribeIndex().register(scribe)} |
| 65 | * </p> |
| 66 | * @param scribe the scribe to register |
| 67 | */ |
| 68 | public void registerScribe(VCardPropertyScribe<? extends VCardProperty> scribe) { |
| 69 | index.register(scribe); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Gets the scribe index. |
| 74 | * @return the scribe index |
| 75 | */ |
| 76 | public ScribeIndex getScribeIndex() { |
| 77 | return index; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Sets the scribe index. |
| 82 | * @param index the scribe index |
| 83 | */ |
| 84 | public void setScribeIndex(ScribeIndex index) { |
| 85 | this.index = index; |
| 86 | } |
| 87 | } |