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.

Decoding: Configuration.default.withDefaults makes it use the default value when the decoding the field failed

See original GitHub issue

I’m using the Configuration.default.withDefaults feature and lots of my case classes have defaults values for members. However, when decoding a json where there is an error (e.g. type mismatch) it uses the default value and swallows the error:

import io.circe.generic.extras.Configuration
import io.circe.generic.extras.auto._

implicit val customConfig: Configuration = Configuration.default.withDefaults
final case class Foo(number: Int = 999)
io.circe.parser.decode[Foo]("""{"number": "not a number!"}""")

res0: Either[io.circe.Error,Foo] = Right(Foo(999))

Ideally, I’d like the default value to be used only if the field was missing from the json, and get an error when it was there but incorrectly.

Can I change this behavior somewhere central so that I don’t need to have custom decoders for all my case classes with defaults?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
exoegocommented, Dec 7, 2018

I think that “fallback to default value if decoding failed” is a good option to have. But it should not be default, since I think

  • If value of wrong type is given in JSON, decoding should fail and report developers what is wrong.
  • Default values should be used only if key is missing in JSON.

I think popular JSON-based Web-APIs works like this.

0reactions
frothcommented, Apr 9, 2019

Took the time to try the ApplicativeError idea it out and found it was more complicated than helpful. But finished the pull request anyway while doing so. 😉 Please have a look.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use default fields in case class when decoding/encoding json ...
You can do this using the circe-generic-extras package. It's a separate dependency you have to put in your build. For sbt, that's:
Read more >
circe/circe - Gitter
how do I decode case class FooField(value: String). to have value of field named foo if one is present, otherwise have decoding failure....
Read more >
io.circe.generic.extras.Configuration Scala Example
fromFile(file) val raw = try { src.mkString } finally { src.close() } implicit val config = Configuration.default.withDefaults decode[List[ABI]](raw) } }.
Read more >
Encoding and decoding - circe
circe uses Encoder and Decoder type classes for encoding and decoding. An Encoder[A] instance provides a function that will convert any A to...
Read more >
12. OAuth2 - Spring
This section shows how to configure the OAuth 2.0 Login sample using Google ... Therefore, it makes sense to provide default values in...
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