Skip to content

Package: SerializationContextImpl

SerializationContextImpl

nameinstructionbranchcomplexitylinemethod
SerializationContextImpl(JsonbContext)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
SerializationContextImpl(JsonbContext, Type)
M: 21 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
addProcessedObject(Object)
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%
determineSerializationType(Object)
M: 16 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getKey()
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%
getRootSerializer(Type)
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%
isContainerWithNulls()
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%
isRoot()
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%
marshall(Object, JsonGenerator)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
marshall(Object, JsonGenerator, boolean)
M: 36 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
marshallWithoutClose(Object, JsonGenerator)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
removeProcessedObject(Object)
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%
serialize(Object, JsonGenerator)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
serialize(String, Object, JsonGenerator)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
serializeObject(Object, JsonGenerator)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
setContainerWithNulls(boolean)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setKey(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setRoot(boolean)
M: 4 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) 2015, 2022 Oracle and/or its affiliates. All rights reserved.
3: * Copyright (c) 2019, 2020 Payara Foundation and/or its affiliates. All rights reserved.
4: *
5: * This program and the accompanying materials are made available under the
6: * terms of the Eclipse Public License v. 2.0 which is available at
7: * http://www.eclipse.org/legal/epl-2.0,
8: * or the Eclipse Distribution License v. 1.0 which is available at
9: * http://www.eclipse.org/org/documents/edl-v10.php.
10: *
11: * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
12: */
13:
14: package org.eclipse.yasson.internal;
15:
16: import java.lang.reflect.Type;
17: import java.util.HashSet;
18: import java.util.Objects;
19: import java.util.Set;
20: import java.util.logging.Logger;
21:
22: import jakarta.json.bind.JsonbException;
23: import jakarta.json.bind.serializer.SerializationContext;
24: import jakarta.json.stream.JsonGenerationException;
25: import jakarta.json.stream.JsonGenerator;
26:
27: import org.eclipse.yasson.internal.properties.MessageKeys;
28: import org.eclipse.yasson.internal.properties.Messages;
29: import org.eclipse.yasson.internal.serializer.ModelSerializer;
30:
31: /**
32: * JSONB marshaller. Created each time marshalling operation called.
33: */
34: public class SerializationContextImpl extends ProcessingContext implements SerializationContext {
35:
36: private static final Logger LOGGER = Logger.getLogger(SerializationContextImpl.class.getName());
37:
38: /**
39: * Used to avoid StackOverflowError, when adapted / serialized object
40: * contains instance of its type inside it or when object has recursive reference.
41: */
42: private final Set<Object> currentlyProcessedObjects = new HashSet<>();
43:
44: private final Type runtimeType;
45: private String key = null;
46: private boolean containerWithNulls = true;
47: private boolean root = true;
48:
49: /**
50: * Creates Marshaller for generation to String.
51: *
52: * @param jsonbContext Current context.
53: * @param rootRuntimeType Type of root object.
54: */
55: public SerializationContextImpl(JsonbContext jsonbContext, Type rootRuntimeType) {
56: super(jsonbContext);
57: this.runtimeType = rootRuntimeType;
58: }
59:
60: /**
61: * Creates Marshaller for generation to String.
62: *
63: * @param jsonbContext Current context.
64: */
65: public SerializationContextImpl(JsonbContext jsonbContext) {
66: this(jsonbContext, null);
67: }
68:
69: /**
70: * Set new current property key name.
71: *
72: * @param key key name
73: */
74: public void setKey(String key) {
75: this.key = key;
76: }
77:
78: /**
79: * Current property key name.
80: *
81: * @return current property key name
82: */
83: public String getKey() {
84: return key;
85: }
86:
87: /**
88: * Serialized value is a root value.
89: *
90: * @return is root value
91: */
92: public boolean isRoot() {
93: return root;
94: }
95:
96: /**
97: * Set whether serialized value is root value.
98: *
99: * @param root is root value
100: */
101: public void setRoot(boolean root) {
102: this.root = root;
103: }
104:
105: /**
106: * Value from this property is only used in {@link org.eclipse.yasson.internal.serializer.NullSerializer}.
107: * It should not be used anywhere else.
108: *
109: * @return if container supports nulls
110: */
111: public boolean isContainerWithNulls() {
112: return containerWithNulls;
113: }
114:
115: /**
116: * Set if container supports null values.
117: *
118: * @param writeNulls should write nulls in container
119: */
120: public void setContainerWithNulls(boolean writeNulls) {
121: this.containerWithNulls = writeNulls;
122: }
123:
124: /**
125: * Marshals given object to provided Writer or OutputStream.
126: *
127: * @param object object to marshall
128: * @param jsonGenerator generator to use
129: * @param close if generator should be closed
130: */
131: public void marshall(Object object, JsonGenerator jsonGenerator, boolean close) {
132: try {
133: serializeObject(object, jsonGenerator);
134: } catch (JsonbException e) {
135: throw e;
136: } catch (RuntimeException e) {
137: throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR, e.getMessage()), e);
138: } finally {
139: try {
140:• if (close) {
141: jsonGenerator.close();
142: } else {
143: jsonGenerator.flush();
144: }
145: } catch (JsonGenerationException jge) {
146: LOGGER.severe(jge.getMessage());
147: }
148: }
149: }
150:
151: /**
152: * Marshals given object to provided Writer or OutputStream.
153: * Closes the generator on completion.
154: *
155: * @param object object to marshall
156: * @param jsonGenerator generator to use
157: */
158: public void marshall(Object object, JsonGenerator jsonGenerator) {
159: marshall(object, jsonGenerator, true);
160: }
161:
162: /**
163: * Marshals given object to provided Writer or OutputStream.
164: * Leaves generator open for further interaction after completion.
165: *
166: * @param object object to marshall
167: * @param jsonGenerator generator to use
168: */
169: public void marshallWithoutClose(Object object, JsonGenerator jsonGenerator) {
170: marshall(object, jsonGenerator, false);
171: }
172:
173: @Override
174: public <T> void serialize(String key, T object, JsonGenerator generator) {
175: Objects.requireNonNull(key);
176: Objects.requireNonNull(object);
177: setKey(key);
178: serializeObject(object, generator);
179: }
180:
181: @Override
182: public <T> void serialize(T object, JsonGenerator generator) {
183: Objects.requireNonNull(object);
184: serializeObject(object, generator);
185: }
186:
187: /**
188: * Serializes root element.
189: *
190: * @param <T> Root type
191: * @param root Root.
192: * @param generator JSON generator.
193: */
194: public <T> void serializeObject(T root, JsonGenerator generator) {
195: Type type = determineSerializationType(root);
196: final ModelSerializer rootSerializer = getRootSerializer(type);
197: rootSerializer.serialize(root, generator, this);
198: }
199:
200: private <T> Type determineSerializationType(T root) {
201:• if (isRoot() && runtimeType != null) {
202: return runtimeType;
203: }
204:• return root == null ? Object.class : root.getClass();
205: }
206:
207: public ModelSerializer getRootSerializer(Type type) {
208: return getJsonbContext().getSerializationModelCreator().serializerChain(type, true, true);
209: }
210:
211: /**
212: * Adds currently processed object to the {@link Set}.
213: *
214: * @param object processed object
215: * @return if object was added
216: */
217: public boolean addProcessedObject(Object object) {
218: return this.currentlyProcessedObjects.add(object);
219: }
220:
221: /**
222: * Removes processed object from the {@link Set}.
223: *
224: * @param object processed object
225: * @return if object was removed
226: */
227: public boolean removeProcessedObject(Object object) {
228: return currentlyProcessedObjects.remove(object);
229: }
230:
231:
232: }