Skip to content

Commit 9a92def

Browse files
committed
Respect WRITE_ENUMS_USING_TO_STRING in EnumAsIonSymbolSerializer
We don't need to make any changes for the corresponding READ_ENUMS_USING_TO_STRING because the enum deserializer is the Jackson one which already checks for the feature.
1 parent c363acf commit 9a92def

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

ion/src/main/java/com/fasterxml/jackson/dataformat/ion/EnumAsIonSymbolSerializer.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@
2222
import com.fasterxml.jackson.databind.JavaType;
2323
import com.fasterxml.jackson.databind.JsonMappingException;
2424
import com.fasterxml.jackson.databind.JsonNode;
25+
import com.fasterxml.jackson.databind.SerializationFeature;
2526
import com.fasterxml.jackson.databind.SerializerProvider;
2627
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
2728
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
2829

2930
/**
3031
* Serializes enumeration members as IonSymbols.
31-
*
32+
*
3233
* Use annotation
33-
*
34+
*
3435
* <pre>
3536
* &#64;JsonSerialize(using=EnumAsIonSymbolSerializer.class)
3637
* </pre>
37-
*
38+
*
3839
* on enumeration members that should serialize at symbols (which amounts to serializing without being surrounded by
3940
* double-quotes)
4041
*/
@@ -49,7 +50,11 @@ public EnumAsIonSymbolSerializer() {
4950
@Override
5051
public void serialize(Enum<?> value, JsonGenerator g, SerializerProvider provider) throws IOException {
5152
if (IonGenerator.class.isAssignableFrom(g.getClass())) {
52-
((IonGenerator) g).writeSymbol(value.name());
53+
String valueString = provider.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
54+
? value.toString()
55+
: value.name();
56+
57+
((IonGenerator) g).writeSymbol(valueString);
5358
} else {
5459
throw new JsonGenerationException("Can only use EnumAsIonSymbolSerializer with IonGenerator", g);
5560
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at:
7+
*
8+
* http://aws.amazon.com/apache2.0/
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
12+
* language governing permissions and limitations under the License.
13+
*/
14+
15+
package com.fasterxml.jackson.dataformat.ion;
16+
17+
import java.io.IOException;
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
import com.amazon.ion.IonSystem;
21+
import com.amazon.ion.system.IonSystemBuilder;
22+
import com.fasterxml.jackson.databind.SerializationFeature;
23+
24+
public class EnumAsIonSymbolSerializationTest {
25+
private static final IonSystem ION_SYSTEM = IonSystemBuilder.standard().build();
26+
27+
@Test
28+
public void testUsingName() throws IOException {
29+
final IonObjectMapper mapper = newMapper();
30+
31+
Assert.assertEquals(
32+
ION_SYSTEM.singleValue("SOME_VALUE"),
33+
mapper.writeValueAsIonValue(SomeEnum.SOME_VALUE));
34+
}
35+
36+
@Test
37+
public void testUsingToString() throws IOException {
38+
final IonObjectMapper mapper = newMapper();
39+
40+
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
41+
42+
Assert.assertEquals(
43+
ION_SYSTEM.singleValue("some_value"),
44+
mapper.writeValueAsIonValue(SomeEnum.SOME_VALUE));
45+
}
46+
47+
private static IonObjectMapper newMapper() {
48+
final IonObjectMapper mapper = new IonObjectMapper(new IonFactory(null, ION_SYSTEM));
49+
mapper.registerModule(new EnumAsIonSymbolModule());
50+
return mapper;
51+
}
52+
53+
private enum SomeEnum {
54+
SOME_VALUE;
55+
56+
@Override
57+
public String toString() {
58+
return name().toLowerCase();
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)