Skip to content

Commit

Permalink
fix #108 shapeOverride ignored when setting up a custom Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
vboulaye authored and cowtowncoder committed Oct 5, 2019
1 parent 11eac26 commit d5c866f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public JsonSerializer<?> createContextual(SerializerProvider prov,
{
JsonFormat.Value ann = findFormatOverrides(prov, property, handledType());
if (ann != null) {
int shapeOverride = 0;
JacksonJodaDateFormat format = _format;

int shapeOverride;
Boolean useTimestamp;

// Simple case first: serialize as numeric timestamp?
Expand All @@ -86,8 +84,9 @@ public JsonSerializer<?> createContextual(SerializerProvider prov,
shapeOverride = FORMAT_ARRAY;
} else {
useTimestamp = null;
shapeOverride = 0;
shapeOverride = _shapeOverride;
}
JacksonJodaDateFormat format = _format;
// must not call if flag defined, to rely on defaults:
if (useTimestamp != null) {
format = format.withUseTimestamp(useTimestamp);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.fasterxml.jackson.datatype.joda.ser;

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonInclude;

import org.joda.time.*;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.joda.JodaTestBase;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
import org.joda.time.*;
import org.joda.time.format.ISODateTimeFormat;

import java.io.IOException;

public class JodaSerializationTest extends JodaTestBase
{
Expand Down Expand Up @@ -84,7 +85,7 @@ public void testLocalDateSerWithTypeInfo() throws IOException
/* Tests for LocalTime type
/**********************************************************
*/

public void testLocalTimeSer() throws IOException
{
LocalTime date = new LocalTime(13,20,54);
Expand All @@ -96,8 +97,28 @@ public void testLocalTimeSer() throws IOException
ObjectMapper mapper = jodaMapper();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
assertEquals(quote("13:20:54.000"), mapper.writeValueAsString(date));

}


public void testLocalTimeSerWithFormatOverride() throws IOException
{
LocalTime date = new LocalTime(13,20,54);

// configure a custom serialzer fot the LocalTime
SimpleModule testModule = new SimpleModule("TestModule");
testModule.addSerializer(LocalTime.class, new LocalTimeSerializer(
new JacksonJodaDateFormat(ISODateTimeFormat.hourMinute()), 1));

ObjectMapper mapper = mapperWithModuleBuilder()
.addModule(testModule)
.build();

assertEquals(quote("13:20"), mapper.writeValueAsString(date));

assertEquals(aposToQuotes("{'contents':'13:20'}"), mapper.writeValueAsString(new Container<>(date)));

}

public void testLocalTimeSerWithTypeInfo() throws IOException
{
LocalTime date = new LocalTime(13,20,54);
Expand All @@ -111,6 +132,7 @@ public void testLocalTimeSerWithTypeInfo() throws IOException
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
assertEquals("[\"org.joda.time.LocalTime\",\"13:20:54.000\"]",
mapper.writeValueAsString(date));

}

/*
Expand Down

0 comments on commit d5c866f

Please sign in to comment.