Skip to content

Package: StringSerializer

StringSerializer

nameinstructionbranchcomplexitylinemethod
StringSerializer(TypeSerializerBuilder)
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%
serializeValue(String, JsonGenerator, SerializationContextImpl)
M: 32 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2021, 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.serializer.types;
14:
15: import java.nio.charset.StandardCharsets;
16:
17: import jakarta.json.bind.JsonbException;
18: import jakarta.json.stream.JsonGenerator;
19:
20: import org.eclipse.yasson.internal.JsonbConfigProperties;
21: import org.eclipse.yasson.internal.SerializationContextImpl;
22: import org.eclipse.yasson.internal.properties.MessageKeys;
23: import org.eclipse.yasson.internal.properties.Messages;
24:
25: /**
26: * Serializer of the {@link String} type.
27: */
28: class StringSerializer extends TypeSerializer<String> {
29:
30: StringSerializer(TypeSerializerBuilder serializerBuilder) {
31: super(serializerBuilder);
32: }
33:
34: @Override
35: void serializeValue(String value, JsonGenerator generator, SerializationContextImpl context) {
36: JsonbConfigProperties configProperties = context.getJsonbContext().getConfigProperties();
37:• if (configProperties.isStrictIJson()) {
38: String newString = new String(value.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
39:• if (!newString.equals(value)) {
40: throw new JsonbException(Messages.getMessage(MessageKeys.UNPAIRED_SURROGATE));
41: }
42: }
43: generator.write(value);
44: }
45:
46: }