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

Avro4sEncodingException #862

Open
HilbertGodel opened this issue Dec 18, 2024 · 0 comments
Open

Avro4sEncodingException #862

HilbertGodel opened this issue Dec 18, 2024 · 0 comments

Comments

@HilbertGodel
Copy link

HilbertGodel commented Dec 18, 2024

case class Foo(a: String, b: String)

 implicit object FooEncoder extends Encoder[Foo] {

   override val schemaFor = SchemaFor[Foo]

   override def encode(foo: Foo): GenericData.Record = {
     val record = new GenericData.Record(schema)
     println(schema.toString())
     record.put("a", foo.a.toUpperCase)
     record.put("b", foo.b.toUpperCase)
     record
   }
 }
 implicit object FooDecoder extends Decoder[Foo] {

   override val schemaFor = SchemaFor[Foo]

   override def decode(value: Any) = {
     val record = value.asInstanceOf[GenericRecord]
     Foo(record.get("a").toString.toLowerCase, record.get("b").toString.toLowerCase)
   }
 }

 val format = RecordFormat[Foo]
 // record is a type that implements both GenericRecord and Specific Record
 val record = format.to(Foo("alpha", "beta"))
 println(record)

Raised Exception: Exception in thread "main" com.sksamuel.avro4s.Avro4sEncodingException: Cannot marshall an instance of Foo(alpha,beta) to a Record (had class class org.apache.avro.generic.GenericData$Record, output was {"a": "ALPHA", "b": "BETA"})

Scala Version: 2.12.19
avro4s Version: 4.1.2

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

1 participant