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

MonthDaySerializer does not serialize objects into array #107

Closed
XhstormR opened this issue Mar 26, 2019 · 8 comments
Closed

MonthDaySerializer does not serialize objects into array #107

XhstormR opened this issue Mar 26, 2019 · 8 comments

Comments

@XhstormR
Copy link

Unlike YearMonthSerializer serialize objects as array " [2018, 5]", MonthDaySerializer serialize to "--03-02"

I notice document say, what should i do :

NOTE: unlike many other date/time type serializers, this serializer will only use Array notation if explicitly instructed to do so with JsonFormat (either directly or through per-type defaults) and NOT with global defaults.

I use org.springframework.boot:spring-boot-starter-web:2.1.3.RELEASE,
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.8,
com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8

application.yml

  jackson:
    serialization:
      INDENT_OUTPUT: false
      WRITE_DATES_AS_TIMESTAMPS: true
@XhstormR
Copy link
Author

I don't know how to make the MonthDaySerializer serialize an object into an array?

@cowtowncoder
Copy link
Member

cowtowncoder commented Mar 27, 2019

This belongs under jackson-modules-java8, since that's where serializer is defined, will move.

@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-databind Mar 27, 2019
@cowtowncoder
Copy link
Member

MonthDaySerializer outputs things as JSON array by default (looking at code), so I would need a reproduction of your issue. It could be due to framework you are using ignoring WRITE_DATES_AS_TIMESTAMPS setting.

@XhstormR
Copy link
Author

XhstormR commented Mar 27, 2019

Thanks, This is the smallest reproduction.

TEST1.zip

C:\Users\Leo>curl http://127.0.0.1:8080/t1
"--03-27"
C:\Users\Leo>curl http://127.0.0.1:8080/t2
[2019,3]
C:\Users\Leo>curl http://127.0.0.1:8080/t3
[2019,3,27,15,0,45,825499000]

@wilkinsona
Copy link

From the referenced Spring Boot issue, here's an even smaller reproduction:

ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).registerModule(new JavaTimeModule());
System.out.println(objectMapper.writeValueAsString(MonthDay.now()));
System.out.println(objectMapper.writeValueAsString(YearMonth.now()));
System.out.println(objectMapper.writeValueAsString(LocalDateTime.now()));

The above code will produce output similar to the following:

"--03-28"
[2019,3]
[2019,3,28,15,35,59,579000000]

@XhstormR
Copy link
Author

XhstormR commented Apr 1, 2019

Has anyone noticed here?

@cowtowncoder cowtowncoder added 2.9 active Issue being actively investigated and/or worked on labels May 13, 2019
@cowtowncoder
Copy link
Member

Ok finally got around to this one.

So: for some reason, original implementation defaulted to textual, not array-based, and since there is no way to distinguish "left as default" setting for use-timestamps from explicit, indication has to be done some other way.
There are 2 main mechanisms: annotation with @JsonFormat and "config overrides".

Former is

    static class Wrapper {
        @JsonFormat(shape = JsonFormat.Shape.ARRAY) // or NUMBER
        public MonthDay value;
    }

and config overrides something like

objectMapper.configOverride(MonthDay.class)
    .setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.ARRAY);

Now: these work, but it seems that deserialization part does NOT actually work at this point.
So I'll leave the issue open for now. I will add tests for serialization for 2.9, but deserialization fix probably needs to go in 2.10.0.

@cowtowncoder cowtowncoder removed 2.9 active Issue being actively investigated and/or worked on labels Jul 17, 2019
@cowtowncoder
Copy link
Member

Closing this, follow-up under #121.

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

3 participants