From 100860a8a4b19e34018aaaa00459832fadfc7119 Mon Sep 17 00:00:00 2001 From: silag Date: Thu, 27 Jun 2024 10:52:01 +0300 Subject: [PATCH] added owl annotation value deserializer --- pom.xml | 2 +- .../webprotege/jackson/IriDeserializer.java | 26 ++++++++++++ .../OWLAnnotationValueDeserializer.java | 42 +++++++++++++++++++ .../jackson/WebProtegeJacksonApplication.java | 2 + 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/main/java/edu/stanford/protege/webprotege/jackson/IriDeserializer.java create mode 100644 src/main/java/edu/stanford/protege/webprotege/jackson/OWLAnnotationValueDeserializer.java diff --git a/pom.xml b/pom.xml index 01188db..94dca69 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ edu.stanford.protege webprotege-jackson - 0.9.2 + 0.9.3 webprotege-jackson Jackson Serializers and Deserializers for OWL API Objects diff --git a/src/main/java/edu/stanford/protege/webprotege/jackson/IriDeserializer.java b/src/main/java/edu/stanford/protege/webprotege/jackson/IriDeserializer.java new file mode 100644 index 0000000..64047ca --- /dev/null +++ b/src/main/java/edu/stanford/protege/webprotege/jackson/IriDeserializer.java @@ -0,0 +1,26 @@ +package edu.stanford.protege.webprotege.jackson; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import org.semanticweb.owlapi.model.IRI; + +import java.io.IOException; + +/** + * Matthew Horridge + * Stanford Center for Biomedical Informatics Research + * 18 Jun 2018 + */ +public class IriDeserializer extends StdDeserializer { + + public IriDeserializer() { + super(IRI.class); + } + + @Override + public IRI deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { + return IRI.create(jsonParser.getValueAsString()); + } +} + diff --git a/src/main/java/edu/stanford/protege/webprotege/jackson/OWLAnnotationValueDeserializer.java b/src/main/java/edu/stanford/protege/webprotege/jackson/OWLAnnotationValueDeserializer.java new file mode 100644 index 0000000..64ca7ed --- /dev/null +++ b/src/main/java/edu/stanford/protege/webprotege/jackson/OWLAnnotationValueDeserializer.java @@ -0,0 +1,42 @@ +package edu.stanford.protege.webprotege.jackson; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import edu.stanford.protege.webprotege.jackson.OWLLiteralDeserializer; +import org.semanticweb.owlapi.model.OWLAnnotationValue; + +import javax.annotation.Nonnull; +import javax.inject.Inject; +import java.io.IOException; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class OWLAnnotationValueDeserializer extends StdDeserializer { + + @Nonnull + private final OWLLiteralDeserializer literalDeserializer; + + @Nonnull + private final IriDeserializer iriDeserializer; + + @Inject + public OWLAnnotationValueDeserializer(@Nonnull OWLLiteralDeserializer literalDeserializer, @Nonnull IriDeserializer iriDeserializer) { + super(OWLAnnotationValue.class); + this.literalDeserializer = checkNotNull(literalDeserializer); + this.iriDeserializer = checkNotNull(iriDeserializer); + } + + @Override + public OWLAnnotationValue deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + if(p.hasToken(JsonToken.START_OBJECT)) { + return literalDeserializer.deserialize(p, ctxt); + } + else { + return iriDeserializer.deserialize(p, ctxt); + } + } +} + diff --git a/src/main/java/edu/stanford/protege/webprotege/jackson/WebProtegeJacksonApplication.java b/src/main/java/edu/stanford/protege/webprotege/jackson/WebProtegeJacksonApplication.java index 0853620..4f390d7 100644 --- a/src/main/java/edu/stanford/protege/webprotege/jackson/WebProtegeJacksonApplication.java +++ b/src/main/java/edu/stanford/protege/webprotege/jackson/WebProtegeJacksonApplication.java @@ -202,6 +202,8 @@ public ObjectMapper objectMapper(OWLDataFactory dataFactory) { module.setMixInAnnotation(OWLAnnotationProperty.class, OWLAnnotationPropertyMixin.class); module.addDeserializer(OWLAnnotationPropertyImpl.class, new OWLEntityDeserializer<>(dataFactory, EntityType.ANNOTATION_PROPERTY)); + module.addDeserializer(OWLAnnotationValue.class, new OWLAnnotationValueDeserializer(new OWLLiteralDeserializer(dataFactory), new IriDeserializer())); + module.setMixInAnnotation(OWLIndividual.class, OWLIndividualMixin.class); module.setMixInAnnotation(OWLNamedIndividual.class, OWLNamedIndividualMixin.class);