Skip to content

Method: getSerializer(SerializationContext)

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.serializer;
19:
20: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
21: import cz.cvut.kbss.jsonld.serialization.serializer.compact.ObjectPropertyValueSerializer;
22: import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext;
23:
24: import java.util.Objects;
25: import java.util.Optional;
26: import java.util.Set;
27:
28: /**
29: * Manager of value serializers aware of object graph traversal.
30: * <p>
31: * A new instance of this should be created every time a new object graph is being serialized.
32: */
33: public class ObjectGraphValueSerializers implements ValueSerializers {
34:
35: private final ValueSerializers serializers;
36:
37: private final ObjectPropertyValueSerializer opSerializer;
38:
39: public ObjectGraphValueSerializers(ValueSerializers serializers, ObjectPropertyValueSerializer opSerializer) {
40: this.serializers = Objects.requireNonNull(serializers);
41: this.opSerializer = Objects.requireNonNull(opSerializer);
42: }
43:
44: @Override
45: public <T> boolean hasCustomSerializer(Class<T> type) {
46: return serializers.hasCustomSerializer(type);
47: }
48:
49: @Override
50: public <T> Optional<ValueSerializer<T>> getSerializer(SerializationContext<T> ctx) {
51: final Optional<ValueSerializer<T>> result = serializers.getSerializer(ctx);
52:• return result.isPresent() ? result :
53:• (BeanAnnotationProcessor.isObjectProperty(ctx.getField()) ? Optional.of(opSerializer) :
54: Optional.empty());
55: }
56:
57: @Override
58: public <T> ValueSerializer<T> getOrDefault(SerializationContext<T> ctx) {
59: final Optional<ValueSerializer<T>> result = serializers.getSerializer(ctx);
60: return result.orElseGet(() -> (BeanAnnotationProcessor.isObjectProperty(ctx.getField()) ? opSerializer :
61: serializers.getOrDefault(ctx)));
62: }
63:
64: @Override
65: public <T> void registerSerializer(Class<T> forType, ValueSerializer<? super T> serializer) {
66: serializers.registerSerializer(forType, serializer);
67: }
68:
69: @Override
70: public ValueSerializer<String> getIdentifierSerializer() {
71: return serializers.getIdentifierSerializer();
72: }
73:
74: @Override
75: public void registerIdentifierSerializer(ValueSerializer<String> idSerializer) {
76: serializers.registerIdentifierSerializer(idSerializer);
77: }
78:
79: @Override
80: public ValueSerializer<Set<String>> getTypesSerializer() {
81: return serializers.getTypesSerializer();
82: }
83:
84: @Override
85: public void registerTypesSerializer(ValueSerializer<Set<String>> typesSerializer) {
86: serializers.registerTypesSerializer(typesSerializer);
87: }
88:
89: @Override
90: public ValueSerializer<?> getIndividualSerializer() {
91: return serializers.getIndividualSerializer();
92: }
93:
94: @Override
95: public void registerIndividualSerializer(ValueSerializer<?> individualSerializer) {
96: serializers.registerIndividualSerializer(individualSerializer);
97: }
98: }