it.cnr.iit.jscontact.tools.dto.deserializers.VCardPropsDeserializer Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Consiglio Nazionale delle Ricerche
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package it.cnr.iit.jscontact.tools.dto.deserializers;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import ezvcard.VCardDataType;
import it.cnr.iit.jscontact.tools.dto.VCardProp;
import it.cnr.iit.jscontact.tools.dto.V_Extension;
import it.cnr.iit.jscontact.tools.dto.utils.JsonNodeUtils;
import lombok.NoArgsConstructor;
import java.io.IOException;
import java.util.*;
/**
* Custom JSON deserializer for the VCardProp array.
*
* @author Mario Loffredo
*/
@NoArgsConstructor
public class VCardPropsDeserializer extends JsonDeserializer {
private static Map getParameters(JsonNode node) {
Map parameters = new HashMap<>();
if (node == null || !node.isObject())
return parameters;
Iterator> iter = node.fields();
while (iter.hasNext()) {
Map.Entry entry = iter.next();
parameters.put(entry.getKey(),JsonNodeUtils.toObject(entry.getValue()));
}
return parameters;
}
@Override
public VCardProp[] deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
JsonNode node = jp.getCodec().readTree(jp);
if (node == null || !node.isArray())
return null;
List list = new ArrayList<>();
for (JsonNode subnode : node) {
if (subnode == null || !subnode.isArray())
return null;
Map parameters = new HashMap<>();
list.add(VCardProp.builder()
.name(V_Extension.toV_Extension(subnode.get(0).asText()))
.parameters(getParameters(subnode.get(1)))
.type((subnode.get(2).asText().equals("unknown")) ? null : VCardDataType.get(subnode.get(2).asText()))
.value(JsonNodeUtils.toObject(subnode.get(3)))
.build());
}
return list.toArray(new VCardProp[0]);
}
}