Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect WRITE_ENUMS_USING_TO_STRING in EnumAsIonSymbolSerializer #241

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;

/**
* Serializes enumeration members as IonSymbols.
*
*
* Use annotation
*
*
* <pre>
* &#64;JsonSerialize(using=EnumAsIonSymbolSerializer.class)
* </pre>
*
*
* on enumeration members that should serialize at symbols (which amounts to serializing without being surrounded by
* double-quotes)
*/
Expand All @@ -49,7 +50,11 @@ public EnumAsIonSymbolSerializer() {
@Override
public void serialize(Enum<?> value, JsonGenerator g, SerializerProvider provider) throws IOException {
if (IonGenerator.class.isAssignableFrom(g.getClass())) {
((IonGenerator) g).writeSymbol(value.name());
String valueString = provider.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
? value.toString()
: value.name();

((IonGenerator) g).writeSymbol(valueString);
} else {
throw new JsonGenerationException("Can only use EnumAsIonSymbolSerializer with IonGenerator", g);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at:
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/

package com.fasterxml.jackson.dataformat.ion;

import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import com.amazon.ion.IonSystem;
import com.amazon.ion.system.IonSystemBuilder;
import com.fasterxml.jackson.databind.SerializationFeature;

public class EnumAsIonSymbolSerializationTest {
private static final IonSystem ION_SYSTEM = IonSystemBuilder.standard().build();

@Test
public void testUsingName() throws IOException {
final IonObjectMapper mapper = newMapper();

Assert.assertEquals(
ION_SYSTEM.singleValue("SOME_VALUE"),
mapper.writeValueAsIonValue(SomeEnum.SOME_VALUE));
}

@Test
public void testUsingToString() throws IOException {
final IonObjectMapper mapper = newMapper();

mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);

Assert.assertEquals(
ION_SYSTEM.singleValue("some_value"),
mapper.writeValueAsIonValue(SomeEnum.SOME_VALUE));
}

private static IonObjectMapper newMapper() {
final IonObjectMapper mapper = new IonObjectMapper(new IonFactory(null, ION_SYSTEM));
mapper.registerModule(new EnumAsIonSymbolModule());
return mapper;
}

private enum SomeEnum {
SOME_VALUE;

@Override
public String toString() {
return name().toLowerCase();
}
}
}