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

None is written as null with Include.NON_NULL set since 2.7.4 (also another issue with 2.7.3 and earlier) #325

Closed
dimatkach opened this issue May 19, 2017 · 1 comment

Comments

@dimatkach
Copy link

dimatkach commented May 19, 2017

case class Foo(bar: Options[String] = None)
gets written as { "bar": null }.
This used to work for me before (at least, in 2.6.6). Not sure at which point it got broken, I ran into this issue trying to upgrade to 2.8.8.

Update: Looks like the last version it works right is 2.7.3. I haven't checked everything after that, but 2.7.{4,5,9} and 2.8.0 are all broken.

Unfortunately, I cannot use 2.7.3 or 2.7.2, because they have another issue (seems to be gone in 2.8.8, so I am not reporting it separately): if I have a trait extending a Product, and a case class with a field, that's Option of that trait, then serializing that case class always writes that field as empty:

   trait Prod extends Produc1[String] { def x: String; def canEqual(o: Any) = ???; def _1 = x }
   class Foo(val x: String) extends Prod
   case class Bar(bar: Prod)
   case class Bat(bam: Option[Foo])
   case class Baz(baz: Option[Prod])

   mapper.writeValueAsString(new Foo("bar")) // { "x" = "bar" } - good
   mapper.writeValueAsString(Bar(new Foo("bar"))) // { "bar" = { "x" = "bar" }} - good
   mapper.writeValueAsString(Bat(Some(new Foo("bar")))) // { "bat" = { "x" = "bar" }} - good
   mapper.writeValueAsString(Baz(Some(new Foo("bar")))) // { "baz" = {}} - ????

So, it looks like I am stuck with 2.6.6. for now ... :(

@dimatkach dimatkach changed the title None is written as null with Include.NON_NULL set None is written as null with Include.NON_NULL set since 2.7.4 (also another issue with 2.7.3 and earlier) May 19, 2017
@cowtowncoder
Copy link
Member

@dimatkach What you want to do here is to use value NON_ABSENT instead of NON_NULL, to omit absent value from being serialized. You can read Javadocs for @JsonInclude:

http://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonInclude.html

to see full definitions of intended operation of values.

Now: I think handling is correct: NON_NULL explicitly only refers to Java (or any JVM Language, Scala include) nulls but NOT to possible json serialization as null. While distinction may seem unimportant it is unfortunately quite distinct from serialization side: databinding can not control transformation from JVM value (non-null here) into external json value. Other values (NON_ABSENT, NON_EMPTY) do perform additional checks, so that serializer is called to see if a non-null value falls within category of values to exclude -- such as absent referential values, or empty collections. Also note that values are super-/subsets of each other: NON_EMPTY also excludes everything that NON_ABSENT does, and NON_ABSENT includes NON_NULL.

Exact implementation did indeed change a bit up until version 2.7 (and, regrettably, it seems, for one of patch versions, at least for Scala module?). Behavior should be stable from this point on.

I hope this helps.

alexandre-normand added a commit to alexandre-normand/nomad-spark that referenced this issue May 22, 2018
This fixes issue hashicorp#3.
Updated SubmitRestProtocolMessage because of the change in jackson's
handling of null/absent values described here:
FasterXML/jackson-module-scala#325
andrusha pushed a commit to andrusha/spark that referenced this issue Aug 27, 2018
Jackson is incompatible with upstream versions, therefore bump
the Jackson version to a more recent one.

Behaviour has changed since 2.7.x that None is not considered null
anymore: FasterXML/jackson-module-scala#325

This will only include value that indicates that only properties with
null value, or what is considered empty, are not to be included.
https://fasterxml.github.io/jackson-annotations/javadoc/2.7/com/fasterxml/jackson/annotation/JsonInclude.Include.html

Change the flags to NON_ABSENT
Fokko pushed a commit to Fokko/spark that referenced this issue Oct 4, 2018
Jackson is incompatible with upstream versions, therefore bump
the Jackson version to a more recent one.

Behaviour has changed since 2.7.x that None is not considered null
anymore: FasterXML/jackson-module-scala#325

This will only include value that indicates that only properties with
null value, or what is considered empty, are not to be included.
https://fasterxml.github.io/jackson-annotations/javadoc/2.7/com/fasterxml/jackson/annotation/JsonInclude.Include.html

Change the flags to NON_ABSENT
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