Giter Club home page Giter Club logo

Comments (10)

zapodot avatar zapodot commented on August 30, 2024

I have used the exact same setting in most of my projects, and then it works as expected.. I will look into it though. A code example would be appreciated

from jackson-databind-java-optional.

zapodot avatar zapodot commented on August 30, 2024

As the new test shows, this module does not alter behaviour of the "JsonInclude.Include.NON_NULL serialization setting.
@natnan - Can some other setting in your project have caused this?

from jackson-databind-java-optional.

natnan avatar natnan commented on August 30, 2024

Hi @zapodot . Sorry, I've assumed I'd get email from github for your response, so didn't get back here to check (nor logged in github to check notifications). Then, totally forgot about it.

As I've said I'm using dropwizard, which adds bunch of modules to object mapper. I was able to isolate it to afterburner module. So if you add
objectMapper.registerModule(new AfterburnerModule())
the test will fail.

I'm not sure whether it's as expected. I'll be digging more and will take it to afterburner depending on that.

from jackson-databind-java-optional.

zapodot avatar zapodot commented on August 30, 2024

The AfterBurner module seems to have caused problems for other modules as well. I will have a look at it to see if I can find a solution.

from jackson-databind-java-optional.

zapodot avatar zapodot commented on August 30, 2024

@natnan : just added a new test class that tests serialization/deserialization with AfterBurner enabled as well as the JsonInclude.Include.NON_NULL setting. Does your beans look different from the one I use in the test class? The best approach moving forward would be if you could provide a failing test or at least describe your use case in more detail (how you have defined your POJOs, and which version of Jackson you are using in your project).

from jackson-databind-java-optional.

natnan avatar natnan commented on August 30, 2024

@zapodot

@Test
  public void test_directly_object_mapper() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new AfterburnerModule());
    objectMapper.registerModule(new JavaOptionalModule());
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    String jsonText = objectMapper.writeValueAsString(new CustomJsonObject());

    JsonNode json = objectMapper.readTree(jsonText);

    ...
    assertThat(json.has("optionalEmpty")).isFalse();
  }
...
@Data //lombok
    public class CustomJsonObject {
      private int integer = 5;
      private String nullString = null;
      private Optional<String> optionalEmpty = Optional.empty();
      private Optional<String> optionalString = Optional.of("optional string");
    }

By the way if I've just realized, if I move AfterburnerModule registration below JavaOptionalModule, the test passes. However, in your test, it's already below; so I guess that's not it.

Jackson 2.3.3
Now I realize I should have tested it with the latest version before taking it here :), sorry about that. I'll create a quick project with the latest version and let you know.

from jackson-databind-java-optional.

natnan avatar natnan commented on August 30, 2024

It fails the same way with the following gradle:

    compile 'com.fasterxml.jackson.core:jackson-core:2.4.3'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.4.3'
    compile 'com.fasterxml.jackson.core:jackson-datatype-jdk7:2.4.3'
    compile 'com.fasterxml.jackson.module:jackson-module-afterburner:2.4.3'
    compile 'org.zapodot:jackson-databind-java-optional:2.4.3'

from jackson-databind-java-optional.

zapodot avatar zapodot commented on August 30, 2024

As the test I just added shows this will occur if you have standard JavaBean POJOs. In that case the getters will be used for serialization and if AfterBurner has been initialized before this module our writer will never run. Possible workarounds (apart from changing module load-order) is to annotate the fields with @JsonProperty as that will avoid the AfterBurner optimized property serializers from being invoked.

No other fix is posible at this time because I can not change the behaviour of the AfterBurner module..

Sorry for that :-(

from jackson-databind-java-optional.

natnan avatar natnan commented on August 30, 2024

I understand. Unfortunately I can't change the order but I can annotate the fields even though that's far from pretty. It's better than nothing though :).

I appreciate the time you've spent. Thank you very much.

from jackson-databind-java-optional.

zapodot avatar zapodot commented on August 30, 2024

If you don't like the idea of adding @JsonProperty annotations to the POJOs, have a look at http://wiki.fasterxml.com/JacksonFeatureBuilderPattern

from jackson-databind-java-optional.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.