Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
# Conflicts:
#	pom.xml
  • Loading branch information
ledsoft committed Jan 1, 2021
2 parents 49d5cdf + 5899be2 commit 7dbf5d6
Show file tree
Hide file tree
Showing 21 changed files with 842 additions and 615 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cz.cvut.kbss.jsonld</groupId>
<artifactId>jb4jsonld</artifactId>
<version>0.8.2</version>
<version>0.8.3</version>
<name>JB4JSON-LD</name>
<description>Java Binding for JSON-LD allows serialization and deserialization of Java POJOs to/from JSON-LD.
This is the core implementation, which has to be integrated with Jackson, Jersey etc.
Expand All @@ -18,7 +18,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<cz.cvut.kbss.jopa.version>0.16.0</cz.cvut.kbss.jopa.version>
<cz.cvut.kbss.jopa.version>0.16.2</cz.cvut.kbss.jopa.version>

<org.junit.jupiter.version>5.6.2</org.junit.jupiter.version>
<org.mockito.version>3.4.0</org.mockito.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ public static boolean hasTypesField(Class<?> cls) {
* @return Whether field has a {@link OWLObjectProperty} annotation
*/
public static boolean isObjectProperty(Field field) {
Objects.requireNonNull(field);
return field.getDeclaredAnnotation(OWLObjectProperty.class) != null;
return field != null && field.getDeclaredAnnotation(OWLObjectProperty.class) != null;
}

/**
Expand All @@ -273,8 +272,7 @@ public static boolean isObjectProperty(Field field) {
* @return Whether field has a {@link OWLAnnotationProperty} annotation
*/
public static boolean isAnnotationProperty(Field field) {
Objects.requireNonNull(field);
return field.getDeclaredAnnotation(OWLAnnotationProperty.class) != null;
return field != null && field.getDeclaredAnnotation(OWLAnnotationProperty.class) != null;
}

/**
Expand All @@ -284,8 +282,7 @@ public static boolean isAnnotationProperty(Field field) {
* @return Whether the field has a {@link Id} annotation
*/
public static boolean isInstanceIdentifier(Field field) {
Objects.requireNonNull(field);
return field.getDeclaredAnnotation(Id.class) != null;
return field != null && field.getDeclaredAnnotation(Id.class) != null;
}

/**
Expand All @@ -295,8 +292,7 @@ public static boolean isInstanceIdentifier(Field field) {
* @return Whether the field has a {@link Types} annotation
*/
public static boolean isTypesField(Field field) {
Objects.requireNonNull(field);
return field.getDeclaredAnnotation(Types.class) != null;
return field != null && field.getDeclaredAnnotation(Types.class) != null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,28 @@

import cz.cvut.kbss.jopa.model.MultilingualString;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
import cz.cvut.kbss.jsonld.serialization.model.CollectionNode;
import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
import cz.cvut.kbss.jsonld.serialization.model.ObjectNode;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

class AnnotationFieldSerializer implements FieldSerializer {
class AnnotationValueSerializer implements ValueSerializer {

private final MultilingualStringSerializer multilingualStringSerializer;

AnnotationFieldSerializer(
MultilingualStringSerializer multilingualStringSerializer) {
AnnotationValueSerializer(MultilingualStringSerializer multilingualStringSerializer) {
this.multilingualStringSerializer = multilingualStringSerializer;
}

@Override
public List<JsonNode> serializeField(Field field, Object value) {
final String attName = BeanAnnotationProcessor.getAttributeIdentifier(field);
public List<JsonNode> serialize(String attId, Object value) {
if (value instanceof Collection) {
final Collection<?> col = (Collection<?>) value;
final CollectionNode node = JsonNodeFactory.createCollectionNode(attName, col);
final CollectionNode node = JsonNodeFactory.createCollectionNode(attId, col);
col.forEach(item -> {
if (isReference(item)) {
node.addItem(serializeReference(null, item));
Expand All @@ -52,12 +48,12 @@ public List<JsonNode> serializeField(Field field, Object value) {
return Collections.singletonList(node);
} else {
if (isReference(value)) {
return Collections.singletonList(serializeReference(attName, value));
return Collections.singletonList(serializeReference(attId, value));
} else if (value instanceof MultilingualString) {
return Collections.singletonList(multilingualStringSerializer.serialize(attName,
return Collections.singletonList(multilingualStringSerializer.serialize(attId,
(MultilingualString) value));
}
return Collections.singletonList(JsonNodeFactory.createLiteralNode(attName, value));
return Collections.singletonList(JsonNodeFactory.createLiteralNode(attId, value));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import cz.cvut.kbss.jsonld.serialization.model.CompositeNode;
import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
import cz.cvut.kbss.jsonld.serialization.traversal.InstanceVisitor;
import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;
import java.util.Stack;
Expand All @@ -32,28 +32,22 @@ public class JsonLdTreeBuilder implements InstanceVisitor {

private final Stack<CompositeNode> nodeStack = new Stack<>();
private CompositeNode currentNode;
private Field visitedField;

private final FieldSerializer literalSerializer;
private final FieldSerializer annotationSerializer;
private final FieldSerializer propertiesSerializer = new PropertiesFieldSerializer();
private final ValueSerializer literalSerializer;
private final ValueSerializer annotationSerializer;

public JsonLdTreeBuilder() {
final MultilingualStringSerializer msSerializer = new MultilingualStringSerializer();
this.literalSerializer = new LiteralFieldSerializer(msSerializer);
this.annotationSerializer = new AnnotationFieldSerializer(msSerializer);
this.literalSerializer = new LiteralValueSerializer(msSerializer);
this.annotationSerializer = new AnnotationValueSerializer(msSerializer);
}

@Override
public void openInstance(Object instance) {
final CompositeNode newCurrent = visitedField != null ? JsonNodeFactory.createObjectNode(attId(visitedField)) :
JsonNodeFactory.createObjectNode();
public void openObject(SerializationContext<?> ctx) {
final CompositeNode newCurrent =
ctx.getAttributeId() != null ? JsonNodeFactory.createObjectNode(ctx.getAttributeId()) :
JsonNodeFactory.createObjectNode();
openNewNode(newCurrent);
this.visitedField = null;
}

private String attId(Field field) {
return BeanAnnotationProcessor.getAttributeIdentifier(field);
}

private void openNewNode(CompositeNode newNode) {
Expand All @@ -67,73 +61,53 @@ private void openNewNode(CompositeNode newNode) {
}

@Override
public void closeInstance(Object instance) {
public void closeObject(SerializationContext<?> ctx) {
currentNode.close();
if (!nodeStack.empty()) {
this.currentNode = nodeStack.pop();
}
}

@Override
public void visitIdentifier(String identifier, Object instance) {
public void visitIdentifier(SerializationContext<String> idCtx) {
assert currentNode.isOpen();
currentNode.addItem(JsonNodeFactory.createObjectIdNode(JsonLd.ID, identifier));
currentNode.addItem(JsonNodeFactory.createObjectIdNode(JsonLd.ID, idCtx.getValue()));
}

@Override
public void visitTypes(Collection<String> types, Object instance) {
final CollectionNode typesNode = JsonNodeFactory.createCollectionNode(JsonLd.TYPE, types);
types.forEach(type -> typesNode.addItem(JsonNodeFactory.createLiteralNode(type)));
public void visitTypes(SerializationContext<Collection<String>> typesCtx) {
final CollectionNode typesNode = JsonNodeFactory.createCollectionNode(JsonLd.TYPE, typesCtx.getValue());
typesCtx.getValue().forEach(type -> typesNode.addItem(JsonNodeFactory.createLiteralNode(type)));
currentNode.addItem(typesNode);
}

@Override
public void visitKnownInstance(String id, Object instance) {
if (visitedField != null) {
openNewNode(JsonNodeFactory.createObjectNode(attId(visitedField)));
} else {
openNewNode(JsonNodeFactory.createObjectNode());
}
currentNode.addItem(JsonNodeFactory.createLiteralNode(JsonLd.ID, id));
closeInstance(instance);
this.visitedField = null;
}

@Override
public void visitField(Field field, Object value) {
if (value == null || BeanAnnotationProcessor.isTypesField(field)) {
return;
}
if (BeanAnnotationProcessor.isObjectProperty(field)) {
this.visitedField = field;
} else {
public void visitAttribute(SerializationContext<?> ctx) {
if (ctx.getValue() != null && !BeanAnnotationProcessor.isObjectProperty(ctx.getField())) {
assert currentNode != null;
final List<JsonNode> nodes;
if (BeanAnnotationProcessor.isAnnotationProperty(field)) {
nodes = annotationSerializer.serializeField(field, value);
} else if (BeanAnnotationProcessor.isPropertiesField(field)) {
// A problem could be when the properties contain a property mapped by the model as well
nodes = propertiesSerializer.serializeField(field, value);
if (BeanAnnotationProcessor.isAnnotationProperty(ctx.getField())) {
nodes = annotationSerializer.serialize(ctx.getAttributeId(), ctx.getValue());
} else {
nodes = literalSerializer.serializeField(field, value);
nodes = literalSerializer.serialize(ctx.getAttributeId(), ctx.getValue());
}
nodes.forEach(node -> currentNode.addItem(node));
}
}

@Override
public void openCollection(Collection<?> collection) {
public void openCollection(SerializationContext<? extends Collection<?>> ctx) {
final CollectionNode newCurrent =
visitedField != null ? JsonNodeFactory.createCollectionNode(attId(visitedField), collection) :
JsonNodeFactory.createCollectionNode(collection);
ctx.getAttributeId() != null ? JsonNodeFactory.createCollectionNode(ctx.getAttributeId(),
ctx.getValue()) :
JsonNodeFactory.createCollectionNode(ctx.getValue());
openNewNode(newCurrent);
this.visitedField = null;
}

@Override
public void closeCollection(Collection<?> collection) {
public void closeCollection(SerializationContext<?> ctx) {
assert currentNode instanceof CollectionNode;
closeInstance(collection);
closeObject(ctx);
}

public CompositeNode getTreeRoot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,37 @@
package cz.cvut.kbss.jsonld.serialization;

import cz.cvut.kbss.jopa.model.MultilingualString;
import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
import cz.cvut.kbss.jsonld.serialization.model.CollectionNode;
import cz.cvut.kbss.jsonld.serialization.model.JsonNode;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

class LiteralFieldSerializer implements FieldSerializer {
class LiteralValueSerializer implements ValueSerializer {

private final MultilingualStringSerializer multilingualStringSerializer;

LiteralFieldSerializer(MultilingualStringSerializer multilingualStringSerializer) {
LiteralValueSerializer(MultilingualStringSerializer multilingualStringSerializer) {
this.multilingualStringSerializer = multilingualStringSerializer;
}

@Override
public List<JsonNode> serializeField(Field field, Object value) {
final String attName = BeanAnnotationProcessor.getAttributeIdentifier(field);
public List<JsonNode> serialize(String attId, Object value) {
final JsonNode result;
if (value instanceof Collection) {
final Collection<?> col = (Collection<?>) value;
final CollectionNode node = JsonNodeFactory.createCollectionNode(attName, col);
final CollectionNode node = JsonNodeFactory.createCollectionNode(attId, col);
col.forEach(obj -> {
final JsonNode serObj = obj instanceof MultilingualString ? multilingualStringSerializer.serialize(
(MultilingualString) obj) : JsonNodeFactory.createLiteralNode(obj);
node.addItem(serObj);
});
result = node;
} else if (value instanceof MultilingualString) {
result = multilingualStringSerializer.serialize(attName, (MultilingualString) value);
result = multilingualStringSerializer.serialize(attId, (MultilingualString) value);
} else {
result = JsonNodeFactory.createLiteralNode(attName, value);
result = JsonNodeFactory.createLiteralNode(attId, value);
}
return Collections.singletonList(result);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
import java.util.List;

/**
* Serializes field value.
* Serializes value.
*/
interface FieldSerializer {
interface ValueSerializer {

/**
* Serializes the specified field, returning a list of JSON-LD nodes representing it.
* <p>
* The result is a list because maps (e.g. {@link cz.cvut.kbss.jopa.model.annotations.Properties}) cannot be
* serialized as a single attribute.
*
* @param field The field to serialize
* @param value Value of the field
* @param attId Attribute identifier
* @param value Value to serialize
* @return Serialization result
*/
List<JsonNode> serializeField(Field field, Object value);
List<JsonNode> serialize(String attId, Object value);
}
Loading

0 comments on commit 7dbf5d6

Please sign in to comment.