001package ezvcard.io.json;
002
003import java.io.IOException;
004
005import com.fasterxml.jackson.core.JsonParser;
006import com.fasterxml.jackson.core.JsonProcessingException;
007import com.fasterxml.jackson.databind.DeserializationContext;
008import com.fasterxml.jackson.databind.JsonDeserializer;
009
010import ezvcard.VCard;
011import ezvcard.io.scribe.ScribeIndex;
012import ezvcard.io.scribe.VCardPropertyScribe;
013import ezvcard.property.VCardProperty;
014
015/*
016 Copyright (c) 2012-2023, Michael Angstadt
017 All rights reserved.
018
019 Redistribution and use in source and binary forms, with or without
020 modification, are permitted provided that the following conditions are met: 
021
022 1. Redistributions of source code must retain the above copyright notice, this
023 list of conditions and the following disclaimer. 
024 2. Redistributions in binary form must reproduce the above copyright notice,
025 this list of conditions and the following disclaimer in the documentation
026 and/or other materials provided with the distribution. 
027
028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
029 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
030 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
032 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
034 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
037 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038
039 The views and conclusions contained in the software and documentation are those
040 of the authors and should not be interpreted as representing official policies, 
041 either expressed or implied, of the FreeBSD Project.
042 */
043
044/**
045 * Deserializes jCards within the jackson-databind framework.
046 * @author Buddy Gorven
047 */
048public class JCardDeserializer extends JsonDeserializer<VCard> {
049        private ScribeIndex index = new ScribeIndex();
050
051        @Override
052        public VCard deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
053                @SuppressWarnings("resource")
054                JCardReader reader = new JCardReader(parser);
055                reader.setScribeIndex(index);
056                return reader.readNext();
057        }
058
059        /**
060         * <p>
061         * Registers a property scribe. This is the same as calling:
062         * </p>
063         * <p>
064         * {@code getScribeIndex().register(scribe)}
065         * </p>
066         * @param scribe the scribe to register
067         */
068        public void registerScribe(VCardPropertyScribe<? extends VCardProperty> scribe) {
069                index.register(scribe);
070        }
071
072        /**
073         * Gets the scribe index.
074         * @return the scribe index
075         */
076        public ScribeIndex getScribeIndex() {
077                return index;
078        }
079
080        /**
081         * Sets the scribe index.
082         * @param index the scribe index
083         */
084        public void setScribeIndex(ScribeIndex index) {
085                this.index = index;
086        }
087}