Skip to content

Commit

Permalink
274: checking WRITE_ENUMS_USING_INDEX feature
Browse files Browse the repository at this point in the history
  • Loading branch information
etrandafir93 committed Jan 6, 2024
1 parent 6e27079 commit ce4504a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.fasterxml.jackson.datatype.jsr310;

import java.time.*;
import java.util.function.Supplier;

import com.fasterxml.jackson.core.json.PackageVersion;
import com.fasterxml.jackson.core.util.JacksonFeatureSet;
Expand Down Expand Up @@ -144,9 +145,12 @@ public void setupModule(SetupContext context) {

context.addDeserializers(desers);


boolean oneBasedMonth = _features.isEnabled(JavaTimeFeature.ONE_BASED_MONTHS);
Supplier<Boolean> serializeEnumsByIndex = () -> context.isEnabled(SerializationFeature.WRITE_ENUMS_USING_INDEX);

context.addBeanDeserializerModifier(new JavaTimeDeserializerModifier(oneBasedMonth));
context.addBeanSerializerModifier(new JavaTimeSerializerModifier(oneBasedMonth));
context.addBeanSerializerModifier(new JavaTimeSerializerModifier(oneBasedMonth, serializeEnumsByIndex));

// 20-Nov-2023, tatu: [modules-java8#288]: someone may have directly
// added entries, need to add for backwards compatibility
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.fasterxml.jackson.datatype.jsr310.ser;

import java.time.Month;
import java.util.function.Predicate;
import java.util.function.Supplier;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JavaType;
Expand All @@ -13,15 +15,17 @@
*/
public class JavaTimeSerializerModifier extends BeanSerializerModifier {
private final boolean _oneBaseMonths;
private final Supplier<Boolean> _serializeEnumsByIndex;

public JavaTimeSerializerModifier(boolean oneBaseMonths) {
public JavaTimeSerializerModifier(boolean oneBaseMonths, Supplier<Boolean> serializeEnumsByIndex) {
_oneBaseMonths = oneBaseMonths;
_serializeEnumsByIndex = serializeEnumsByIndex;
}

@Override
public JsonSerializer<?> modifyEnumSerializer(SerializationConfig config, JavaType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) {
if (valueType.hasRawClass(Month.class)) {
return new MonthSerializer((JsonSerializer<Enum>) serializer, _oneBaseMonths);
return new MonthSerializer((JsonSerializer<Enum>) serializer, _oneBaseMonths, _serializeEnumsByIndex);
}
return serializer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.time.Month;
import java.util.function.Predicate;
import java.util.function.Supplier;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
Expand All @@ -12,21 +14,19 @@
*/
public class MonthSerializer extends JsonSerializer<Enum<Month>> {
private final boolean _enableOneBaseMonths;
private final Supplier<Boolean> _serializeEnumsByIndex;

private final JsonSerializer<Enum> _defaultSerializer;

public MonthSerializer(JsonSerializer<Enum> defaultSerializer, boolean oneBaseMonths) {
public MonthSerializer(JsonSerializer<Enum> defaultSerializer, boolean oneBaseMonths, Supplier<Boolean> serializeEnumsByIndex) {
_defaultSerializer = defaultSerializer;
_enableOneBaseMonths = oneBaseMonths;
_serializeEnumsByIndex = serializeEnumsByIndex;
}

@Override
public void serialize(Enum<Month> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if(_enableOneBaseMonths
/*
&& we write the enum ordinal (int) value,
via WRITE_ENUMS_USING_INDEX or other similar construct
*/
) {
if (_enableOneBaseMonths && _serializeEnumsByIndex.get()) {
gen.writeNumber(value.ordinal() + 1);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ public Wrapper() { }
@Test
public void testSerializationFromEnum() throws Exception
{
assertEquals( "\"JANUARY\"" , mapperForOneBased().writeValueAsString(Month.JANUARY));
assertEquals( "\"JANUARY\"" , mapperForZeroBased().writeValueAsString(Month.JANUARY));
assertEquals( "\"JANUARY\"" , mapperForOneBased()
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
.writeValueAsString(Month.JANUARY));
assertEquals( "\"JANUARY\"" , mapperForZeroBased()
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
.writeValueAsString(Month.JANUARY));
}

@Test
Expand Down

0 comments on commit ce4504a

Please sign in to comment.