Skip to contentMethod: resolveTargetClass(JsonObject, Class)
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.deserialization.expanded;
19:
20: import cz.cvut.kbss.jsonld.Configuration;
21: import cz.cvut.kbss.jsonld.JsonLd;
22: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
23: import cz.cvut.kbss.jsonld.deserialization.InstanceBuilder;
24: import cz.cvut.kbss.jsonld.deserialization.util.ValueUtils;
25: import jakarta.json.JsonObject;
26: import jakarta.json.JsonValue;
27:
28: import java.util.Collections;
29: import java.util.List;
30: import java.util.stream.Collectors;
31:
32: abstract class Deserializer<X> {
33:
34: final InstanceBuilder instanceBuilder;
35: final DeserializerConfig config;
36:
37: Deserializer(InstanceBuilder instanceBuilder, DeserializerConfig config) {
38: this.instanceBuilder = instanceBuilder;
39: this.config = config;
40: }
41:
42: Configuration configuration() {
43: return config.getConfiguration();
44: }
45:
46: <T> Class<? extends T> resolveTargetClass(JsonObject jsonRoot, Class<T> resultClass) {
47:• if (BeanClassProcessor.isIdentifierType(resultClass)) {
48: return resultClass;
49: }
50: final List<String> types = getObjectTypes(jsonRoot);
51: return config.getTargetResolver().getTargetClass(resultClass, types);
52: }
53:
54: List<String> getObjectTypes(JsonObject jsonLdObject) {
55: final JsonValue types = jsonLdObject.get(JsonLd.TYPE);
56: if (types == null) {
57: return Collections.emptyList();
58: }
59: assert types.getValueType() == JsonValue.ValueType.ARRAY;
60: return types.asJsonArray().stream().map(ValueUtils::stringValue).collect(Collectors.toList());
61: }
62:
63: abstract void processValue(X value);
64: }