Skip to content

Method: registerTypesSerializer(ValueSerializer)

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.Configuration;
21: import cz.cvut.kbss.jsonld.serialization.JsonLdSerializer;
22: import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext;
23:
24: import java.util.*;
25:
26: /**
27: * Manages serializers of non-object property values for a single {@link JsonLdSerializer} instance.
28: * <p>
29: * That is, if an object is not referenced by an object property mapped attribute, it will be serialized by a serializer
30: * managed by this instance.
31: */
32: public class LiteralValueSerializers implements ValueSerializers {
33:
34: private final Map<Class<?>, ValueSerializer<?>> serializers = new HashMap<>();
35:
36: private final ValueSerializer<?> defaultSerializer;
37:
38: private ValueSerializer<String> identifierSerializer;
39:
40: private ValueSerializer<Set<String>> typesSerializer;
41:
42: private ValueSerializer<?> individualSerializer;
43:
44: public LiteralValueSerializers(ValueSerializer<?> defaultSerializer) {
45: this.defaultSerializer = Objects.requireNonNull(defaultSerializer);
46: }
47:
48: @Override
49: public <T> boolean hasCustomSerializer(Class<T> type) {
50: return serializers.containsKey(type);
51: }
52:
53: @Override
54: public <T> Optional<ValueSerializer<T>> getSerializer(SerializationContext<T> ctx) {
55: return Optional.ofNullable((ValueSerializer<T>) serializers.get(ctx.getValue().getClass()));
56: }
57:
58: @Override
59: public <T> ValueSerializer<T> getOrDefault(SerializationContext<T> ctx) {
60: return (ValueSerializer<T>) serializers.getOrDefault(ctx.getValue().getClass(), defaultSerializer);
61: }
62:
63: @Override
64: public <T> void registerSerializer(Class<T> forType, ValueSerializer<? super T> serializer) {
65: Objects.requireNonNull(forType);
66: Objects.requireNonNull(serializer);
67: serializers.put(forType, serializer);
68: }
69:
70: @Override
71: public ValueSerializer<String> getIdentifierSerializer() {
72: return identifierSerializer;
73: }
74:
75: @Override
76: public void registerIdentifierSerializer(ValueSerializer<String> idSerializer) {
77: this.identifierSerializer = Objects.requireNonNull(idSerializer);
78: }
79:
80: @Override
81: public ValueSerializer<Set<String>> getTypesSerializer() {
82: return typesSerializer;
83: }
84:
85: @Override
86: public void registerTypesSerializer(ValueSerializer<Set<String>> typesSerializer) {
87: this.typesSerializer = Objects.requireNonNull(typesSerializer);
88: }
89:
90: @Override
91: public ValueSerializer<?> getIndividualSerializer() {
92: return individualSerializer;
93: }
94:
95: @Override
96: public void registerIndividualSerializer(ValueSerializer<?> individualSerializer) {
97: this.individualSerializer = Objects.requireNonNull(individualSerializer);
98: }
99:
100: @Override
101: public void configure(Configuration configuration) {
102: serializers.values().forEach(vs -> vs.configure(configuration));
103: individualSerializer.configure(configuration);
104: identifierSerializer.configure(configuration);
105: typesSerializer.configure(configuration);
106: defaultSerializer.configure(configuration);
107: }
108: }