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 fields with common prefix and dash does not work

See original GitHub issue

Hello, I’ve run into peculiar issue. Imagine the following code:

package com.github.mypackage

case class A(text: String)
case class B(`a-ha`: String, a: A)


import io.circe.parser._
import io.circe.generic.auto._

val msg =
  """
     { "a-ha": "foo", "a": {"text": "foofoo" } }
  """
println(decode[B](msg))

When running this, I got the following exception (the code was run in scala-test so line numbers don’t fit):

class java.lang.String cannot be cast to class com.github.mypackage.A (java.lang.String is in module java.base of loader 'bootstrap'; com.github.mypackage.A is in unnamed module of loader 'app')
java.lang.ClassCastException: class java.lang.String cannot be cast to class com.github.mypackage.A (java.lang.String is in module java.base of loader 'bootstrap'; com.github.mypackage.A is in unnamed module of loader 'app')
	at com.github.mypackage.B.<init>(ParsingSpec.scala:11)
	at com.github.mypackage.ParsingSpec$anon$importedDecoder$macro$15$1$anon$macro$7$1.from(ParsingSpec.scala:25)
	at com.github.mypackage.ParsingSpec$anon$importedDecoder$macro$15$1$anon$macro$7$1.from(ParsingSpec.scala:25)
	at shapeless.LabelledGeneric$$anon$1.from(generic.scala:229)
	at shapeless.LabelledGeneric$$anon$1.from(generic.scala:226)
	at io.circe.generic.decoding.DerivedDecoder$$anon$1.apply(DerivedDecoder.scala:15)
	at io.circe.Decoder.decodeJson(Decoder.scala:86)
	at io.circe.Decoder.decodeJson$(Decoder.scala:86)
	at io.circe.generic.decoding.DerivedDecoder.decodeJson(DerivedDecoder.scala:6)
	at io.circe.Parser.finishDecode(Parser.scala:13)
	at io.circe.Parser.finishDecode$(Parser.scala:9)
	at io.circe.parser.package$.finishDecode(package.scala:5)
	at io.circe.Parser.decode(Parser.scala:29)
	at io.circe.Parser.decode$(Parser.scala:28)
	at io.circe.parser.package$.decode(package.scala:5)
...

The problem is when there are fields with common prefix and the suffix of the longer one starts with dash. This means, for example, that if we had these case classes:

case class A(text: String)
case class B(`aha`: String, a: A)

(note field aha without dash) - then decoding works as expected.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
travisbrowncommented, Jun 1, 2021

Sure! Thanks for reminding me of this ridiculous bug. 😃

0reactions
vbmachercommented, Jun 1, 2021

It works, thanks again. I suggest to close the ticket now…

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Teach Prefixes (Mini Teaching Guide + Download)
Looking for the best way to teach prefixes? Our mini teaching guide shows you how! (And don't miss our free printable prefix list!)...
Read more >
6.4. Building Display Filter Expressions - Wireshark
Wireshark provides a display filter language that enables you to precisely control which packets are displayed. They can be used to check for...
Read more >
DOI Handbook - Numbering
This chapter of the Handbook defines the syntax for a DOI ® name, which is used for the identification of an object of...
Read more >
GS1 DataMatrix Guideline
Reconstitutes the original encoded data by recalculating the data from the complementary codes and add-ins. The recalculation regenerates the ...
Read more >
Everything you need to know about Regular Expressions
After reading this article you will have a solid understanding of what regular expressions are, what they can do, and what they can't...
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