Skip to content

Commit

Permalink
[Enhancement #54] Minor code cleanup, add some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Mar 31, 2023
1 parent 273985a commit 948be91
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@ public class IndividualSerializer implements ValueSerializer {
public JsonNode serialize(Object value, SerializationContext ctx) {
assert BeanClassProcessor.isIdentifierType(value.getClass()) || value.getClass().isEnum();
if (BeanClassProcessor.isIdentifierType(value.getClass())) {
final ObjectNode node = JsonNodeFactory.createObjectNode(ctx.getTerm());
node.addItem(JsonNodeFactory.createObjectIdNode(idAttribute(ctx), value));
return node;
return serializeValue(value, ctx);
} else {
return serializeEnumConstant((Enum<?>) value, ctx);
assert value instanceof Enum;
final String iri = EnumUtil.resolveMappedIndividual((Enum<?>) value);
return serialize(iri, ctx);
}
}

private String idAttribute(SerializationContext<?> ctx) {
return ctx.getJsonLdContext().getMappedTerm(JsonLd.ID).orElse(JsonLd.ID);
}

protected JsonNode serializeEnumConstant(Enum<?> constant, SerializationContext<?> ctx) {
final String iri = EnumUtil.resolveMappedIndividual(constant);
private JsonNode serializeValue(Object value, SerializationContext<?> ctx) {
final ObjectNode node = JsonNodeFactory.createObjectNode(ctx.getTerm());
node.addItem(JsonNodeFactory.createObjectIdNode(idAttribute(ctx), iri));
node.addItem(JsonNodeFactory.createObjectIdNode(idAttribute(ctx), value));
return node;
}

private String idAttribute(SerializationContext<?> ctx) {
return ctx.getJsonLdContext().getMappedTerm(JsonLd.ID).orElse(JsonLd.ID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
*/
package cz.cvut.kbss.jsonld.serialization.serializer.compact;

import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.common.EnumUtil;
import cz.cvut.kbss.jsonld.serialization.JsonNodeFactory;
import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
import cz.cvut.kbss.jsonld.serialization.model.ObjectNode;
import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializer;
import cz.cvut.kbss.jsonld.serialization.traversal.ObjectGraphTraverser;
import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ContextBuildingIndividualSerializer extends IndividualSerializer {

@Override
public JsonNode serialize(Object value, SerializationContext ctx) {
// Assume term has been already registered in context
if (serializeUsingExtendedDefinition) {
if (BeanClassProcessor.isIdentifierType(value.getClass())) {
return JsonNodeFactory.createStringLiteralNode(ctx.getTerm(), value.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ void traverseSingular(SerializationContext<?> ctx) {
if (ctx.getValue() == null) {
return;
}
final boolean firstEncounter = !knownInstances.containsKey(ctx.getValue());
final boolean shouldTraverse = visitInstance(ctx);
if (!shouldTraverse) {
return;
}
if (isIndividual(ctx)) {
visitor.visitIndividual(ctx);
visitIndividual(ctx);
return;
}
final boolean firstEncounter = !knownInstances.containsKey(ctx.getValue());
openInstance(ctx);
visitIdentifier(ctx);
if (firstEncounter) {
Expand Down Expand Up @@ -161,6 +161,10 @@ public boolean visitInstance(SerializationContext<?> ctx) {
return visitor.visitObject(ctx);
}

public void visitIndividual(SerializationContext<?> ctx) {
visitor.visitIndividual(ctx);
}

public void openInstance(SerializationContext<?> ctx) {
if (!BeanClassProcessor.isIdentifierType(ctx.getValue().getClass())) {
final String identifier = resolveIdentifier(ctx.getValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cz.cvut.kbss.jsonld.serialization.serializer.context;

import cz.cvut.kbss.jopa.vocabulary.OWL;
import cz.cvut.kbss.jsonld.ConfigParam;
import cz.cvut.kbss.jsonld.Configuration;
import cz.cvut.kbss.jsonld.environment.Generator;
import cz.cvut.kbss.jsonld.environment.Vocabulary;
import cz.cvut.kbss.jsonld.environment.model.OwlPropertyType;
import cz.cvut.kbss.jsonld.serialization.context.DummyJsonLdContext;
import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
import cz.cvut.kbss.jsonld.serialization.model.StringLiteralNode;
import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext;
import org.junit.jupiter.api.Test;

import java.net.URI;

import static org.junit.jupiter.api.Assertions.*;

class ContextBuildingIndividualSerializerTest {

private final ContextBuildingIndividualSerializer sut = new ContextBuildingIndividualSerializer();

@Test
void serializeSerializesUriValueAsStringWhenSerializationWithExtendedTermDefinitionInContextIsEnabled() {
final URI individual = Generator.generateUri();
final SerializationContext<URI> ctx =
new SerializationContext<>(Vocabulary.ORIGIN, individual, DummyJsonLdContext.INSTANCE);
final Configuration config = new Configuration();
config.set(ConfigParam.SERIALIZE_INDIVIDUALS_USING_EXPANDED_DEFINITION, Boolean.TRUE.toString());

sut.configure(config);
final JsonNode result = sut.serialize(individual, ctx);
assertInstanceOf(StringLiteralNode.class, result);
final StringLiteralNode node = (StringLiteralNode) result;
assertEquals(individual.toString(), node.getValue());
}

@Test
void serializeSerializesEnumValueMappedToIndividualAsStringWhenSerializationWithExtendedTermDefinitionInContextIsEnabled() {
final OwlPropertyType value = OwlPropertyType.OBJECT_PROPERTY;
final SerializationContext<OwlPropertyType> ctx =
new SerializationContext<>(Vocabulary.HAS_PROPERTY_TYPE, value, DummyJsonLdContext.INSTANCE);
final Configuration config = new Configuration();
config.set(ConfigParam.SERIALIZE_INDIVIDUALS_USING_EXPANDED_DEFINITION, Boolean.TRUE.toString());

sut.configure(config);
final JsonNode result = sut.serialize(value, ctx);
assertInstanceOf(StringLiteralNode.class, result);
final StringLiteralNode node = (StringLiteralNode) result;
assertEquals(OWL.OBJECT_PROPERTY, node.getValue());
}
}

0 comments on commit 948be91

Please sign in to comment.