Package: PropertiesTraverser
PropertiesTraverser
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PropertiesTraverser(ObjectGraphTraverser) |
|
|
|
|
|
||||||||||||||||||||
isTraversable(Object) |
|
|
|
|
|
||||||||||||||||||||
lambda$serializePropertyValues$0(JsonLdContext, Object) |
|
|
|
|
|
||||||||||||||||||||
serializePropertyValues(String, Collection, JsonLdContext) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
traverseProperties(SerializationContext) |
|
|
|
|
|
||||||||||||||||||||
visitSingleValue(String, Object, JsonLdContext) |
|
|
|
|
|
Coverage
1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jsonld.serialization.traversal;
19:
20: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
21: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
22: import cz.cvut.kbss.jsonld.serialization.context.JsonLdContext;
23:
24: import java.util.Collection;
25: import java.util.Map;
26: import java.util.Objects;
27:
28: /**
29: * Serializes a {@link cz.cvut.kbss.jopa.model.annotations.Properties} field.
30: * <p>
31: * Note that at the moment, when the map also contains a property which is already mapped by another field, a conflict in
32: * the resulting JSON-LD will arise.
33: */
34: class PropertiesTraverser {
35:
36: private final ObjectGraphTraverser parent;
37:
38: PropertiesTraverser(ObjectGraphTraverser parent) {
39: this.parent = parent;
40: }
41:
42: public void traverseProperties(SerializationContext<Map<?, ?>> ctx) {
43:• for (Map.Entry<?, ?> e : ctx.getValue().entrySet()) {
44: final String property = e.getKey().toString();
45:• if (e.getValue() == null) {
46: continue;
47: }
48:• if (e.getValue() instanceof Collection) {
49: final Collection<?> propertyValues = (Collection<?>) e.getValue();
50: serializePropertyValues(property, propertyValues, ctx.getJsonLdContext());
51: } else {
52: visitSingleValue(property, e.getValue(), ctx.getJsonLdContext());
53: }
54: }
55: }
56:
57: private void visitSingleValue(String property, Object value, JsonLdContext jsonLdContext) {
58:• assert value != null;
59:• if (isTraversable(value)) {
60: parent.traverseSingular(new SerializationContext<>(property, value, jsonLdContext));
61: } else {
62: parent.visitAttribute(new SerializationContext<>(property, value, jsonLdContext));
63: }
64: }
65:
66: private static boolean isTraversable(Object value) {
67: final Class<?> cls = value.getClass();
68:• return (BeanClassProcessor.isIdentifierType(value.getClass()) && !String.class.equals(cls)) ||
69:• BeanAnnotationProcessor.isOwlClassEntity(value.getClass()) ||
70:• BeanAnnotationProcessor.hasTypesField(cls);
71: }
72:
73: private void serializePropertyValues(String property, Collection<?> values, JsonLdContext jsonLdContext) {
74:• if (values.isEmpty()) {
75: return;
76: }
77:• if (values.size() == 1) {
78: final Object val = values.iterator().next();
79:• if (val != null) {
80: visitSingleValue(property, val, jsonLdContext);
81: }
82: } else {
83: final SerializationContext<Collection<?>> colContext = new SerializationContext<>(property, values, jsonLdContext);
84: parent.openCollection(colContext);
85: values.stream().filter(Objects::nonNull).forEach(v -> visitSingleValue(null, v, jsonLdContext));
86: parent.closeCollection(colContext);
87: }
88: }
89: }