question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Encoder should remove None: Option[T] values

See original GitHub issue

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"}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
naderghanbaricommented, 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.

15reactions
ckozakcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove --none-- option from dependant field - ServiceNow
Solved: Hello Experts, How to remove --none-- option from dropdown of dependant field? I have one field named "Stage". Values of this field....
Read more >
Encoding Scala None to JSON value using circe
The best way to do this is probably just to add a post-processing step to a semi-automatically derived encoder for B : import...
Read more >
How to hide "None" option single select custom field?
Hello Everone, I've made a single select custom field required. however, it is still showing the "None" option in the custom field. I've....
Read more >
How to Remove '-None-' option from Select List Field?
Edit the filed and make one of the option as default value in the select list and Make it required field.
Read more >
Python | Remove None values from list - GeeksforGeeks
There can be multiple methods to remove None values from a Python list. Some of them are discussed as follows: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found