Skip to content

Package: JsonLdModule

JsonLdModule

nameinstructionbranchcomplexitylinemethod
JsonLdModule()
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
configure(ConfigParam, String)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
configure(String, String)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
init()
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
registerDeserializer(Class, ValueDeserializer)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
registerSerializer(Class, ValueSerializer)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JB4JSON-LD Jackson
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.jackson;
19:
20: import com.fasterxml.jackson.databind.module.SimpleModule;
21: import cz.cvut.kbss.jsonld.ConfigParam;
22: import cz.cvut.kbss.jsonld.Configuration;
23: import cz.cvut.kbss.jsonld.common.PropertyAccessResolver;
24: import cz.cvut.kbss.jsonld.deserialization.ValueDeserializer;
25: import cz.cvut.kbss.jsonld.jackson.common.JsonPropertyAccessResolver;
26: import cz.cvut.kbss.jsonld.jackson.deserialization.JsonLdDeserializerModifier;
27: import cz.cvut.kbss.jsonld.jackson.serialization.JsonLdSerializerModifier;
28: import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializer;
29:
30: import java.util.HashMap;
31: import java.util.Map;
32: import java.util.Objects;
33:
34: /**
35: * Simple module with pre-configured serializer and deserializer modifier.
36: */
37: public class JsonLdModule extends SimpleModule {
38:
39: private final Configuration configuration = new Configuration();
40:
41: private final Map<Class<?>, ValueSerializer<?>> commonSerializers = new HashMap<>();
42: private final Map<Class<?>, ValueDeserializer<?>> commonDeserializers = new HashMap<>();
43:
44: public JsonLdModule() {
45: init();
46: }
47:
48: private void init() {
49: final PropertyAccessResolver accessResolver = new JsonPropertyAccessResolver();
50: setSerializerModifier(new JsonLdSerializerModifier(configuration, accessResolver, commonSerializers));
51: setDeserializerModifier(new JsonLdDeserializerModifier(configuration, accessResolver, commonDeserializers));
52: }
53:
54: /**
55: * Configure this module with additional parameters.
56: *
57: * @param param Parameter to set
58: * @param value New value of the parameter
59: * @return This instance
60: */
61: public JsonLdModule configure(ConfigParam param, String value) {
62: Objects.requireNonNull(param);
63: configuration.set(param, value);
64: return this;
65: }
66:
67: /**
68: * Configure this module with additional parameters.
69: *
70: * @param param Parameter to set
71: * @param value New value of the parameter
72: * @return This instance
73: */
74: public JsonLdModule configure(String param, String value) {
75: Objects.requireNonNull(param);
76: configuration.set(param, value);
77: return this;
78: }
79:
80: /**
81: * Registers the specified serializer for the specified type.
82: *
83: * @param forType Type to register the serializer for
84: * @param serializer Value serializer being registered
85: * @param <T> Type
86: * @return This instance
87: */
88: public <T> JsonLdModule registerSerializer(Class<T> forType, ValueSerializer<T> serializer) {
89: Objects.requireNonNull(forType);
90: Objects.requireNonNull(serializer);
91: commonSerializers.put(forType, serializer);
92: return this;
93: }
94:
95: /**
96: * Registers the specified deserializer for the specified type.
97: *
98: * @param forType Type to register the deserializer for
99: * @param deserializer Value deserializer being registered
100: * @param <T> Type
101: * @return This instance
102: */
103: public <T> JsonLdModule registerDeserializer(Class<T> forType, ValueDeserializer<T> deserializer) {
104: Objects.requireNonNull(forType);
105: Objects.requireNonNull(deserializer);
106: commonDeserializers.put(forType, deserializer);
107: return this;
108: }
109: }