Skip to content

Package: PropertiesFieldSerializer

PropertiesFieldSerializer

nameinstructionbranchcomplexitylinemethod
PropertiesFieldSerializer()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$serializePropertyValues$0(CollectionNode, Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
serializeField(Field, Object)
M: 4 C: 61
94%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 14
100%
M: 0 C: 1
100%
serializePropertyValues(String, Collection)
M: 0 C: 32
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2017 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.serialization;
16:
17: import cz.cvut.kbss.jsonld.serialization.model.CollectionNode;
18: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
19:
20: import java.lang.reflect.Field;
21: import java.util.*;
22:
23: /**
24: * Serializes a {@link cz.cvut.kbss.jopa.model.annotations.Properties} field.
25: * <p>
26: * Note that at the moment, when the map also contains a property which is already map by another field, a conflict in
27: * the resulting JSON-LD will arise.
28: */
29: class PropertiesFieldSerializer implements FieldSerializer {
30:
31: @Override
32: public List<JsonNode> serializeField(Field field, Object value) {
33:• assert value instanceof Map;
34: final Map<?, ?> map = (Map<?, ?>) value;
35: final List<JsonNode> result = new ArrayList<>(map.size());
36:• for (Map.Entry<?, ?> e : map.entrySet()) {
37: final String property = e.getKey().toString();
38:• if (e.getValue() == null) {
39: continue;
40: }
41:• if (e.getValue() instanceof Collection) {
42: final Collection<?> propertyValues = (Collection<?>) e.getValue();
43: serializePropertyValues(property, propertyValues).ifPresent(result::add);
44: } else {
45: result.add(JsonNodeFactory.createLiteralNode(property, e.getValue()));
46: }
47: }
48: return result;
49: }
50:
51: private Optional<JsonNode> serializePropertyValues(String property, Collection<?> values) {
52:• if (values.isEmpty()) {
53: return Optional.empty();
54: }
55:• if (values.size() == 1) {
56: final Object val = values.iterator().next();
57: return Optional.of(JsonNodeFactory.createLiteralNode(property, val));
58: } else {
59: final CollectionNode propertyNode = JsonNodeFactory.createCollectionNode(property, values);
60: values.stream().filter(Objects::nonNull)
61: .forEach(v -> propertyNode.addItem(JsonNodeFactory.createLiteralNode(v)));
62: return Optional.of(propertyNode);
63: }
64: }
65: }