Giter Site home page Giter Site logo

Comments (14)

jsw avatar jsw commented on June 4, 2024 3

This seems to be broken again in 2.7.4 and 2.7.5.

I setup my ObjectMapper using

val mapper = new ObjectMapper with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.registerModule(new JavaTimeModule)
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false)

and the following test fails

val json = mapper.writeValueAsString(C(None, None))
assert(json === "{}")
[info] - should not serialize null fields *** FAILED ***
[info]   "{["a":null,"b":null]}" did not equal "{[]}" (JacksonUtilTest.scala:27)

from jackson-module-scala.

christophercurrie avatar christophercurrie commented on June 4, 2024

Thanks for the bug report, I'll take a look at why the works for normal classes and see what needs to be added to the case class logic.

from jackson-module-scala.

christophercurrie avatar christophercurrie commented on June 4, 2024

While I haven't yet worked out a way to support this using JsonInclude.Include.NON_NULL (which makes the most logical sense, but doesn't appear to have a hook in JsonSerializer, I have added support in OptionSerializer to recognize and honor JsonInclude.Include.NON_EMPTY to achieve the same goal.

from jackson-module-scala.

christophercurrie avatar christophercurrie commented on June 4, 2024

Reopening, as I realize now that this issue is slightly different than #9.

from jackson-module-scala.

christophercurrie avatar christophercurrie commented on June 4, 2024

I added what I think is a functionally equivalent pair of tests to CaseClassSerializerTest and was not able to reproduce the problem. If you can, please retest your use case with the latest code and reopen if you continue to have issues.

from jackson-module-scala.

christophercurrie avatar christophercurrie commented on June 4, 2024

Well it looks like my tests were bad. Reopening as I've fixed the tests and the problem still exists.

from jackson-module-scala.

christophercurrie avatar christophercurrie commented on June 4, 2024

Ok, filing in some more information so I don't forget it.

  • The non-case class isn't succeeding because it's working properly, it's succeeding because the Scala module doesn't currently recognized the property. If I add @JsonProperty annotation to TestObject3, the test fails.
  • Getting NOT_NULL recognized requires that Jackson recognize that it's possible for a non-null value in the JVM to be serialized as JSON "null". Jackson doesn't currently have a hook for this, but I've got an idea for an approach that might work by replacing BeanPropertyWriter with a new version that recognizes "null-like" values.

I should have a fix for this shortly.

from jackson-module-scala.

leons727 avatar leons727 commented on June 4, 2024

With jackson 2.1.0 and jackson-module-scala 2.1.1-SNAPSHOT:

case class Foo(val a: String, val b: Option[Int] = None)

@RunWith(classOf[BlockJUnit4ClassRunner])
class TryJSON {

@test
def testNulls() {
val m = new ObjectMapper()
m.registerModule(new DefaultScalaModule())
m.setSerializationInclusion(JsonInclude.Include.NON_NULL)
println(m.writeValueAsString(new Foo(null, None)))
}
}

Prints:

{"a":null,"b":null}

Am I missing something?

from jackson-module-scala.

christophercurrie avatar christophercurrie commented on June 4, 2024

Nope, my tests are bad, #45 also reported this. Will fix ASAP and issue a 2.1.1 for it.

from jackson-module-scala.

christophercurrie avatar christophercurrie commented on June 4, 2024

This issue should be resolved in 2.1.1, just released.

from jackson-module-scala.

leons727 avatar leons727 commented on June 4, 2024

Yes, works for me correctly now. Thanks for the quick turnaround!

from jackson-module-scala.

devshorts avatar devshorts commented on June 4, 2024

Seems still broken in 2.8.3 as well

from jackson-module-scala.

ptagr avatar ptagr commented on June 4, 2024

is there a workaround for this ?

from jackson-module-scala.

jsw avatar jsw commented on June 4, 2024

Use JsonInclude.Include.NON_ABSENT. See issue 280

from jackson-module-scala.

Related Issues (20)

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.