![JAR search and dependency download from the Maven repository](/logo.png)
org.mnode.ical4j.json.jot.AbstractJotCardMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ical4j-json Show documentation
Show all versions of ical4j-json Show documentation
Custom marshalling between iCal4j objects and JSON formats
The newest version!
package org.mnode.ical4j.json.jot;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import net.fortuna.ical4j.vcard.Parameter;
import net.fortuna.ical4j.vcard.ParameterFactoryRegistry;
import net.fortuna.ical4j.vcard.Property;
import net.fortuna.ical4j.vcard.PropertyFactoryRegistry;
import org.apache.commons.codec.DecoderException;
import org.mnode.ical4j.json.JsonMapper;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class AbstractJotCardMapper extends StdDeserializer implements JsonMapper {
private final PropertyFactoryRegistry propertyFactoryRegistry;
private final ParameterFactoryRegistry parameterFactoryRegistry;
public AbstractJotCardMapper(Class> vc) {
super(vc);
propertyFactoryRegistry = new PropertyFactoryRegistry();
parameterFactoryRegistry = new ParameterFactoryRegistry();
}
protected List parsePropertyList(String propertyName, JsonParser p) throws IOException, URISyntaxException, ParseException, DecoderException {
List properties = new ArrayList<>();
while (!JsonToken.END_ARRAY.equals(p.nextToken())) {
properties.add(parseProperty(propertyName, p));
}
return properties;
}
protected Property parseProperty(String propertyName, JsonParser p) throws IOException, URISyntaxException, ParseException, DecoderException {
String value = null;
List parameters = new ArrayList<>();
if (JsonToken.START_ARRAY.equals(p.currentToken())) {
StringBuilder b = new StringBuilder();
while (!JsonToken.END_ARRAY.equals(p.nextToken())) {
assertCurrentToken(p, JsonToken.VALUE_STRING);
b.append(p.getText());
b.append(',');
}
value = b.toString();
} else if (JsonToken.START_OBJECT.equals(p.currentToken())) {
// parse parameters and value..
while (!JsonToken.END_OBJECT.equals(p.nextToken())) {
assertCurrentToken(p, JsonToken.FIELD_NAME);
if (isParameter(p.currentName())) {
parameters.add(parseParameter(p));
} else {
assertNextScalarValue(p);
value = p.getText();
}
}
} else {
assertCurrentScalarValue(p);
value = p.getText();
}
return propertyFactoryRegistry.getFactory(propertyName).createProperty(parameters, value);
}
protected Parameter parseParameter(JsonParser p) throws IOException {
assertNextScalarValue(p);
return parameterFactoryRegistry.getFactory(p.currentName()).createParameter(p.currentName(), p.getText());
}
private boolean isParameter(String fieldName) {
return Arrays.asList(Parameter.Id.PREF).contains(Parameter.Id.valueOf(fieldName));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy