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

Year deserialization ignores @JsonFormat pattern #78

Closed
devdevx opened this issue Sep 9, 2018 · 3 comments
Closed

Year deserialization ignores @JsonFormat pattern #78

devdevx opened this issue Sep 9, 2018 · 3 comments
Milestone

Comments

@devdevx
Copy link

devdevx commented Sep 9, 2018

Jackson version: 2.9.3
Jackson datatype version: 2.9.6

Example of the @JsonFormat definition:

public class ObjectTest {
   @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "'Y'yyyy")
    private Year customYear;
   // Getters and setters
}

Test example:

ObjectTest object = new ObjectTest();
object.setCustomYear(Year.of(2018));
ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
String json = objectMapper.writeValueAsString(object); // OK -> "{\"customYear\":\"Y2018\"}";
object = objectMapper.readValue(json, ObjectTest.class); // ERROR -> Throws exception

Possibly related to: #51

Stack trace:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.Year` from String "Y2018": Failed to deserialize java.time.Year: (java.time.format.DateTimeParseException) Text 'Y2018' could not be parsed at index 0
 at [Source: (String)"{"customYear":"Y2018"}"; line: 1, column: 15] (through reference chain: com.devdevx.examples.jackson.domain.javatime.JavaTime["customYear"])

	at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1548)
	at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:910)
	at com.fasterxml.jackson.datatype.jsr310.deser.JSR310DeserializerBase._handleDateTimeException(JSR310DeserializerBase.java:80)
	at com.fasterxml.jackson.datatype.jsr310.deser.YearDeserializer.deserialize(YearDeserializer.java:64)
	at com.fasterxml.jackson.datatype.jsr310.deser.YearDeserializer.deserialize(YearDeserializer.java:34)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:287)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
	at com.devdevx.examples.jackson.domain.JsonTest.toObject(JsonTest.java:38)
	at com.devdevx.examples.jackson.domain.javatime.JavaTimeTest.customYearJsonToObject(JavaTimeTest.java:279)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:515)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:170)
	at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:166)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:113)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:58)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:134)
	at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:128)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:109)
	at java.util.ArrayList.forEach(ArrayList.java:1249)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:138)
	at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:128)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:109)
	at java.util.ArrayList.forEach(ArrayList.java:1249)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:138)
	at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:128)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:109)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:49)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:47)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:62)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.time.format.DateTimeParseException: Text 'Y2018' could not be parsed at index 0
	at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
	at java.time.Year.parse(Year.java:292)
	at java.time.Year.parse(Year.java:277)
	at com.fasterxml.jackson.datatype.jsr310.deser.YearDeserializer.deserialize(YearDeserializer.java:60)
	... 45 more
@cowtowncoder cowtowncoder added 2.9 active Issue being actively investigated and/or worked on labels Sep 11, 2018
cowtowncoder added a commit that referenced this issue Sep 13, 2018
@devdevx
Copy link
Author

devdevx commented Sep 13, 2018

I'm not sure, but it seems to me that com.fasterxml.jackson.datatype.jsr310.deser.YearDeserializer shoud extend JSR310DateTimeDeserializerBase to use the custom pattern. What do you think?

@cowtowncoder cowtowncoder changed the title Year deserialization ignores @JsonFormat pattern and throws exception Year deserialization ignores @JsonFormat pattern Sep 18, 2018
@cowtowncoder cowtowncoder added this to the 2.9.7 milestone Sep 18, 2018
@cowtowncoder cowtowncoder removed the active Issue being actively investigated and/or worked on label Sep 18, 2018
@cowtowncoder
Copy link
Member

Ok, so: added tests in 2.10 for all 3 modules, and fixed 2 to be serializable (datatypes, i.e "optionals", parameter names), but jsr310 (date/time) is trickier case. Problem here is that although everything else is or can be made serializable. Java 8 DateTimeFormatter isn't. And although theoretically it is of course possible to work around this that may not be practical.

For 3.0 (master) I can change things so that Module is serializable, but I am not super confident about 2.x. Leaving open for now, regardless.

@cowtowncoder cowtowncoder reopened this Apr 12, 2019
@cowtowncoder
Copy link
Member

I think I re-opened wrong issue. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants