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

Encoder should remove None: Option[T] values #584

Closed
mikaelstaldal opened this issue Mar 7, 2017 · 8 comments
Closed

Encoder should remove None: Option[T] values #584

mikaelstaldal opened this issue Mar 7, 2017 · 8 comments

Comments

@mikaelstaldal
Copy link

It would be nice if the Encoder would remove the key/value completely when the type is Option[T] and the value is None. Currently, it encodes None to JSON null.

(Json4s works this way.)

This code:

  case class Test(foo: String, bar: Option[String])
  implicit val testEncoder: Encoder[Test] = deriveEncoder
  println(Test("FOO", None).asJson.noSpaces)

currently prints:

{"foo":"FOO","bar":null}

I would like it to be:

{"foo":"FOO"}
@vpavkin
Copy link

vpavkin commented Mar 7, 2017

@mikaelstaldal you should just use a custom Printer with dropNullKeys = true.
By default it's false.

@mikaelstaldal
Copy link
Author

Ah, that works. But it seems like dropNullKeys by default is false?

This is also something that is missing from documentation.

@vpavkin
Copy link

vpavkin commented Mar 7, 2017

yes, it's false :) I made a mistake and fixed the comment afterwards

@ckozak
Copy link

ckozak commented Mar 24, 2017

What was the reason to make dropNullKeys = false by default? It seems that not putting nulls is more common among other libraries.

@bergmark
Copy link

bergmark commented May 3, 2017

I commented on this elsewhere, so a quick repeat:

My take is that the printer should not modify the actual JSON format. This is something each Encoder should decide for itself. Inevitably there will be a case where you want to combine the two options for different types, AFAICT that's not possible at the moment.

@Mahoney
Copy link

Mahoney commented Nov 22, 2017

Could nested options be interpreted differently? For
case class Foo(bar: Option[Option[String]])
Foo(bar = None) = {}
Foo(bar = Some(None)) = {"bar": null}
Foo(bar = Some(Some("actual data"))) = {"bar": "actual data"}

@naderghanbari
Copy link

naderghanbari commented Jan 14, 2018

I agree that Printer should not touch the JSON by default. I also agree that Printer should have the power to make the result nicer if you want to. This could help in cases where the actual value is null (e.g. Java interop). So I think Printer is just fine as is.

I think the encoder, though, should drop None values by default and not encode them as null. It should, however, encode genuine null values as null and not drop them.

Similar to dropNullKeys = false in Printer, I think the encoder configuration can have something like dropNone = true.

@travisbrown
Copy link
Member

Now that we have dropNullValues on both the printer and Json I think we can close this.

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

7 participants