Skip to content

Package: JsonbContext

JsonbContext

nameinstructionbranchcomplexitylinemethod
JsonbContext(JsonbConfig, JsonProvider)
M: 56 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
createJsonpProperties(JsonbConfig)
M: 38 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
getAnnotationIntrospector()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getChainModelCreator()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getComponentInstanceCreator()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getComponentMatcher()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getConfig()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getConfigProperties()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getJsonParserFactory()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getJsonProvider()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMappingContext()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSerializationModelCreator()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
initComponentInstanceCreator()
M: 45 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
initJsonParserFactory()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$initComponentInstanceCreator$0()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2016, 2022 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0 which is available at
6: * http://www.eclipse.org/legal/epl-2.0,
7: * or the Eclipse Distribution License v. 1.0 which is available at
8: * http://www.eclipse.org/org/documents/edl-v10.php.
9: *
10: * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11: */
12:
13: package org.eclipse.yasson.internal;
14:
15: import java.security.AccessController;
16: import java.security.PrivilegedAction;
17: import java.util.ArrayList;
18: import java.util.Comparator;
19: import java.util.HashMap;
20: import java.util.List;
21: import java.util.Map;
22: import java.util.Objects;
23: import java.util.Optional;
24: import java.util.ServiceLoader;
25: import java.util.logging.Logger;
26:
27: import jakarta.json.bind.JsonbConfig;
28: import jakarta.json.bind.JsonbException;
29: import jakarta.json.spi.JsonProvider;
30: import jakarta.json.stream.JsonGenerator;
31: import jakarta.json.stream.JsonParserFactory;
32:
33: import org.eclipse.yasson.internal.components.JsonbComponentInstanceCreatorFactory;
34: import org.eclipse.yasson.internal.deserializer.DeserializationModelCreator;
35: import org.eclipse.yasson.internal.properties.MessageKeys;
36: import org.eclipse.yasson.internal.properties.Messages;
37: import org.eclipse.yasson.internal.serializer.SerializationModelCreator;
38: import org.eclipse.yasson.spi.JsonbComponentInstanceCreator;
39:
40: /**
41: * Jsonb context holding central components and configuration of jsonb runtime. Scoped to instance of Jsonb runtime.
42: */
43: public class JsonbContext {
44:
45: private static final Logger LOGGER = Logger.getLogger(JsonbContext.class.getName());
46:
47: private final JsonbConfig jsonbConfig;
48:
49: private final MappingContext mappingContext;
50:
51: private final DeserializationModelCreator deserializationModelCreator;
52:
53: private final SerializationModelCreator serializationModelCreator;
54:
55: private final JsonbComponentInstanceCreator componentInstanceCreator;
56:
57: private final JsonProvider jsonProvider;
58:
59: private final JsonParserFactory jsonParserFactory;
60:
61: private final ComponentMatcher componentMatcher;
62:
63: private final AnnotationIntrospector annotationIntrospector;
64:
65: private final JsonbConfigProperties configProperties;
66:
67: /**
68: * Creates and initialize context.
69: *
70: * @param jsonbConfig jsonb jsonbConfig not null
71: * @param jsonProvider provider of JSONP
72: */
73: public JsonbContext(JsonbConfig jsonbConfig, JsonProvider jsonProvider) {
74: Objects.requireNonNull(jsonbConfig);
75: this.jsonbConfig = jsonbConfig;
76: this.mappingContext = new MappingContext(this);
77: this.componentInstanceCreator = initComponentInstanceCreator();
78: this.componentMatcher = new ComponentMatcher(this);
79: this.annotationIntrospector = new AnnotationIntrospector(this);
80: this.jsonProvider = jsonProvider;
81: this.jsonParserFactory = initJsonParserFactory();
82: this.configProperties = new JsonbConfigProperties(jsonbConfig);
83: this.deserializationModelCreator = new DeserializationModelCreator(this);
84: this.serializationModelCreator = new SerializationModelCreator(this);
85: }
86:
87: /**
88: * Gets {@link JsonbConfig}.
89: *
90: * @return Configuration.
91: */
92: public JsonbConfig getConfig() {
93: return jsonbConfig;
94: }
95:
96: /**
97: * Gets mapping context.
98: *
99: * @return Mapping context.
100: */
101: public MappingContext getMappingContext() {
102: return mappingContext;
103: }
104:
105: /**
106: * Get chain model creator.
107: *
108: * @return chain model creator
109: */
110: public DeserializationModelCreator getChainModelCreator() {
111: return deserializationModelCreator;
112: }
113:
114: /**
115: * Get serialization model creator.
116: *
117: * @return serialization model creator
118: */
119: public SerializationModelCreator getSerializationModelCreator() {
120: return serializationModelCreator;
121: }
122:
123: /**
124: * Gets JSONP provider.
125: *
126: * @return JSONP provider.
127: */
128: public JsonProvider getJsonProvider() {
129: return jsonProvider;
130: }
131:
132: /**
133: * Implementation creating instances of user components used by JSONB, such as adapters and strategies.
134: *
135: * @return Instance creator.
136: */
137: public JsonbComponentInstanceCreator getComponentInstanceCreator() {
138: return componentInstanceCreator;
139: }
140:
141: /**
142: * Component matcher for lookup of (de)serializers and adapters.
143: *
144: * @return Component matcher.
145: */
146: public ComponentMatcher getComponentMatcher() {
147: return componentMatcher;
148: }
149:
150: /**
151: * Gets component for annotation parsing.
152: *
153: * @return Annotation introspector.
154: */
155: public AnnotationIntrospector getAnnotationIntrospector() {
156: return annotationIntrospector;
157: }
158:
159: public JsonbConfigProperties getConfigProperties() {
160: return configProperties;
161: }
162:
163: public JsonParserFactory getJsonParserFactory() {
164: return jsonParserFactory;
165: }
166:
167: private JsonParserFactory initJsonParserFactory() {
168: return jsonProvider.createParserFactory(createJsonpProperties(jsonbConfig));
169: }
170:
171: /**
172: * Propagates properties from JsonbConfig to JSONP generator / parser factories.
173: *
174: * @param jsonbConfig jsonb config
175: * @return properties for JSONP generator / parser
176: */
177: protected Map<String, ?> createJsonpProperties(JsonbConfig jsonbConfig) {
178: //JSONP 1.0 actually ignores the value, just checks the key is present. Only set if JsonbConfig.FORMATTING is true.
179: final Optional<Object> property = jsonbConfig.getProperty(JsonbConfig.FORMATTING);
180: final Map<String, Object> factoryProperties = new HashMap<>();
181:• if (property.isPresent()) {
182: final Object value = property.get();
183:• if (!(value instanceof Boolean)) {
184: throw new JsonbException(Messages.getMessage(MessageKeys.JSONB_CONFIG_FORMATTING_ILLEGAL_VALUE));
185: }
186:• if ((Boolean) value) {
187: factoryProperties.put(JsonGenerator.PRETTY_PRINTING, Boolean.TRUE);
188: }
189: return factoryProperties;
190: }
191: return factoryProperties;
192: }
193:
194: private JsonbComponentInstanceCreator initComponentInstanceCreator() {
195: ServiceLoader<JsonbComponentInstanceCreator> loader = AccessController
196: .doPrivileged((PrivilegedAction<ServiceLoader<JsonbComponentInstanceCreator>>) () -> ServiceLoader
197: .load(JsonbComponentInstanceCreator.class));
198: List<JsonbComponentInstanceCreator> creators = new ArrayList<>();
199:• for (JsonbComponentInstanceCreator creator : loader) {
200: creators.add(creator);
201: }
202:• if (creators.isEmpty()) {
203: // No service provider found - use the defaults
204: return JsonbComponentInstanceCreatorFactory.getComponentInstanceCreator();
205: }
206: creators.sort(Comparator.comparingInt(JsonbComponentInstanceCreator::getPriority).reversed());
207: JsonbComponentInstanceCreator creator = creators.get(0);
208: LOGGER.finest("Component instance creator:" + creator.getClass());
209: return creator;
210: }
211:
212: }