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

Migration to 3.0.0-rc1 issues #715

Closed
plokhotnyuk opened this issue Mar 16, 2025 · 8 comments
Closed

Migration to 3.0.0-rc1 issues #715

plokhotnyuk opened this issue Mar 16, 2025 · 8 comments

Comments

@plokhotnyuk
Copy link

plokhotnyuk commented Mar 16, 2025

I'm trying to migrate on 3.0.0-rc1 here and don't know yet how to resolve remaining issues:

  1. how to enforce serialization of java.time.Year as a JSON string (not a number);
  2. how to use a custom pretty printer with 2 space indention for JSON objects and JSON arrays;
  3. how to avoid serialization of null values for None;
  4. how to properly register classes for sum types that will use a discriminator key/value pair.
@pjfanning
Copy link
Member

@pjfanning
Copy link
Member

Actually, I found the JavaTimeModule in the changed code. Not sure why 'year' is broken. So it now appears as year: 2025 instead of year: "2025"?

@pjfanning
Copy link
Member

I found FasterXML/jackson-modules-java8#355 for the year change.

It looks like year is now deliberately serialized as a number. There appears to be a java annotation that might make it serialize as a string.
@JsonFormat(shape = JsonFormat.Shape.STRING)

FasterXML/jackson-modules-java8@f478801#diff-2dc31f3ce21d209bc49dd1894d08b04aaf011f3a178e72503f6949bff4711cbaR31-R35

fyi @cowtowncoder

@pjfanning
Copy link
Member

For Pretty Printer, I found a test in the master branch of jackson-databind that tests custom pretty printing. It uses

JsonMapper.builder().defaultPrettyPrinter(printer)

https://github.com/FasterXML/jackson-databind/blob/master/src/test/java/tools/jackson/databind/ObjectMapperTest.java#L200

@pjfanning
Copy link
Member

Regarding setSerializationInclusion, I think this is the equivalent

JsonMapper.builder().changeDefaultPropertyInclusion(incl -> incl
                        .withValueInclusion(JsonInclude.Include.NON_EMPTY)
                        .withContentInclusion(JsonInclude.Include.NON_EMPTY))

https://github.com/FasterXML/jackson-databind/blob/master/src/test/java/tools/jackson/databind/ser/AnyGetterTest.java#L307-L309

@plokhotnyuk
Copy link
Author

plokhotnyuk commented Mar 17, 2025

@pjfanning Thanks a lot for your support!

I've solved all issues except serialization of java.time.Year values. The challenge is that I cannot use annotation for a field because I need to serialize a top-level array of java.time.Year. I tried following and nothing worked for me to serialize java.timeYear values as strings:

  1. jsonMapperBuilder.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
  2. jsonMapperBuilder.addModule(new JavaTimeModule().enable(JavaTimeFeature.ALWAYS_ALLOW_STRINGIFIED_DATE_TIMESTAMPS))
  3. jsonMapperBuilder.addModule(new SimpleModule().addSerializer(classOf[Year], { var ser: YearSerializer = null; new YearSerializer { ser = withFormat(null, false, JsonFormat.Shape.STRING) }; ser}))

@cowtowncoder
Copy link
Member

It should be possible to associate @JsonFormat shape via "Config Overrides" for type.
Looks like test for PR has it:

FasterXML/jackson-modules-java8@dc1e77f#diff-2dc31f3ce21d209bc49dd1894d08b04aaf011f3a178e72503f6949bff4711cbaR28-R64

so something like

        final ObjectMapper asStringMapper = JsonMapper.builder()
                .withConfigOverride(Year.class, o -> o.setFormat(
                        JsonFormat.Value.forShape(JsonFormat.Shape.STRING)))
                .addModule(new JavaTimeModule())
                .build();

@plokhotnyuk
Copy link
Author

@cowtowncoder Thank you very much! It works for me!

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