Skip to content

Package: CollectionDeserializer

CollectionDeserializer

nameinstructionbranchcomplexitylinemethod
CollectionDeserializer(InstanceBuilder, DeserializerConfig, String)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
processValue(List)
M: 4 C: 76
95%
M: 1 C: 15
94%
M: 1 C: 8
89%
M: 0 C: 17
100%
M: 0 C: 1
100%
resolvePropertyValue(Map)
M: 0 C: 47
100%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 6
100%
M: 0 C: 1
100%
resolveValue(Map)
M: 0 C: 75
100%
M: 1 C: 11
92%
M: 1 C: 6
86%
M: 0 C: 11
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: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jsonld.deserialization.expanded;
14:
15: import cz.cvut.kbss.jsonld.JsonLd;
16: import cz.cvut.kbss.jsonld.deserialization.InstanceBuilder;
17: import cz.cvut.kbss.jsonld.exception.MissingIdentifierException;
18:
19: import java.net.URI;
20: import java.util.List;
21: import java.util.Map;
22:
23: class CollectionDeserializer extends Deserializer<List<?>> {
24:
25: private final String property;
26:
27: CollectionDeserializer(InstanceBuilder instanceBuilder, DeserializerConfig config, String property) {
28: super(instanceBuilder, config);
29: this.property = property;
30: }
31:
32: @Override
33: void processValue(List<?> value) {
34:• if (value.size() == 1 && value.get(0) instanceof Map) {
35: final Map<?, ?> map = (Map<?, ?>) value.get(0);
36:• if (!instanceBuilder.isPlural(property)) {
37: resolvePropertyValue(map);
38: return;
39: }
40:• if (map.size() == 1 && map.containsKey(JsonLd.LIST)) {
41:• assert map.get(JsonLd.LIST) instanceof List;
42: processValue((List<?>) map.get(JsonLd.LIST));
43: return;
44: }
45: }
46: instanceBuilder.openCollection(property);
47:• for (Object item : value) {
48:• if (item instanceof Map) {
49: resolveValue((Map<?, ?>) item);
50: } else {
51: instanceBuilder.addValue(item);
52: }
53: }
54: instanceBuilder.closeCollection();
55: }
56:
57: private void resolveValue(Map<?, ?> value) {
58:• if (value.size() == 1 && value.containsKey(JsonLd.VALUE)) {
59: instanceBuilder.addValue(value.get(JsonLd.VALUE));
60:• } else if (value.size() == 1 && value.containsKey(JsonLd.ID)) {
61: instanceBuilder.addNodeReference(value.get(JsonLd.ID).toString());
62:• } else if (instanceBuilder.isCurrentCollectionProperties()) {
63: // If we are deserializing an object into @Properties, just extract the identifier and put it into the map
64:• if (!value.containsKey(JsonLd.ID)) {
65: throw new MissingIdentifierException(
66: "Cannot put an object without an identifier into @Properties. Object: " + value);
67: }
68: instanceBuilder.addValue(URI.create(value.get(JsonLd.ID).toString()));
69: } else {
70: final Class<?> elementType = instanceBuilder.getCurrentCollectionElementType();
71: new ObjectDeserializer(instanceBuilder, config, elementType).processValue(value);
72: }
73: }
74:
75: private void resolvePropertyValue(Map<?, ?> value) {
76:• if (value.size() == 1 && value.containsKey(JsonLd.VALUE)) {
77: instanceBuilder.addValue(property, value.get(JsonLd.VALUE));
78:• } else if (value.size() == 1 && value.containsKey(JsonLd.ID)) {
79: instanceBuilder.addNodeReference(property, value.get(JsonLd.ID).toString());
80: } else {
81: new ObjectDeserializer(instanceBuilder, config, property).processValue(value);
82: }
83: }
84: }