Last active
September 12, 2018 11:39
-
-
Save yadavan88/94fd289365549b5a6c37cbabae8c162e to your computer and use it in GitHub Desktop.
json4s FieldSerializer throws ScalaSig exception if used with TypeTag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name := "json4s-test" | |
version := "0.1" | |
scalaVersion := "2.12.6" | |
libraryDependencies ++= Seq( | |
"org.json4s" %% "json4s-jackson" % "3.6.1", | |
"org.scala-lang" % "scala-reflect" % "2.12.6" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Exception in thread "main" org.json4s.package$MappingException: Can't find ScalaSig for class java.lang.ClassLoader | |
at org.json4s.reflect.package$.fail(package.scala:95) | |
at org.json4s.reflect.ScalaSigReader$.$anonfun$findClass$1(ScalaSigReader.scala:59) | |
at scala.Option.getOrElse(Option.scala:121) | |
at org.json4s.reflect.ScalaSigReader$.findClass(ScalaSigReader.scala:59) | |
at org.json4s.reflect.ScalaSigReader$.findField(ScalaSigReader.scala:106) | |
at org.json4s.reflect.ScalaSigReader$.read$1(ScalaSigReader.scala:50) | |
at org.json4s.reflect.ScalaSigReader$.readField(ScalaSigReader.scala:55) | |
at org.json4s.reflect.Reflector$ClassDescriptorBuilder.$anonfun$fields$3(Reflector.scala:69) | |
at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:234) | |
at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:59) | |
at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:52) | |
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48) | |
at scala.collection.TraversableLike.map(TraversableLike.scala:234) | |
at scala.collection.TraversableLike.map$(TraversableLike.scala:227) | |
at scala.collection.AbstractTraversable.map(Traversable.scala:104) | |
at org.json4s.reflect.Reflector$ClassDescriptorBuilder.fields(Reflector.scala:68) | |
at org.json4s.reflect.Reflector$ClassDescriptorBuilder.fields(Reflector.scala:81) | |
at org.json4s.reflect.Reflector$ClassDescriptorBuilder.fields(Reflector.scala:81) | |
at org.json4s.reflect.Reflector$ClassDescriptorBuilder.fields(Reflector.scala:81) | |
at org.json4s.reflect.Reflector$ClassDescriptorBuilder.properties(Reflector.scala:85) | |
at org.json4s.reflect.Reflector$ClassDescriptorBuilder.result(Reflector.scala:191) | |
at org.json4s.reflect.Reflector$.createDescriptor(Reflector.scala:53) | |
at org.json4s.reflect.Reflector$.$anonfun$describe$1(Reflector.scala:48) | |
at org.json4s.reflect.package$Memo.apply(package.scala:36) | |
at org.json4s.reflect.Reflector$.describe(Reflector.scala:48) | |
at org.json4s.Extraction$.decomposeObject$1(Extraction.scala:119) | |
at org.json4s.Extraction$.internalDecomposeWithBuilder(Extraction.scala:230) | |
at org.json4s.Extraction$.addField$1(Extraction.scala:111) | |
at org.json4s.Extraction$.decomposeObject$1(Extraction.scala:142) | |
at org.json4s.Extraction$.internalDecomposeWithBuilder(Extraction.scala:230) | |
at org.json4s.Extraction$.addField$1(Extraction.scala:111) | |
at org.json4s.Extraction$.decomposeObject$1(Extraction.scala:142) | |
at org.json4s.Extraction$.internalDecomposeWithBuilder(Extraction.scala:230) | |
at org.json4s.Extraction$.addField$1(Extraction.scala:111) | |
at org.json4s.Extraction$.decomposeObject$1(Extraction.scala:140) | |
at org.json4s.Extraction$.internalDecomposeWithBuilder(Extraction.scala:230) | |
at org.json4s.Extraction$.decomposeWithBuilder(Extraction.scala:65) | |
at org.json4s.Extraction$.decompose(Extraction.scala:244) | |
at com.reactore.samples.JsonUtilities.toJson(JsonUtilities.scala:23) | |
at com.reactore.samples.JsonUtilities.toJson$(JsonUtilities.scala:22) | |
at com.reactore.samples.JsonUtilitiesImpl$.toJson(JsonUtilities.scala:36) | |
at com.reactore.samples.JsonConverter$.delayedEndpoint$com$reactore$samples$JsonConverter$1(JsonConverter.scala:11) | |
at com.reactore.samples.JsonConverter$delayedInit$body.apply(JsonConverter.scala:8) | |
at scala.Function0.apply$mcV$sp(Function0.scala:34) | |
at scala.Function0.apply$mcV$sp$(Function0.scala:34) | |
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) | |
at scala.App.$anonfun$main$1$adapted(App.scala:76) | |
at scala.collection.immutable.List.foreach(List.scala:389) | |
at scala.App.main(App.scala:76) | |
at scala.App.main$(App.scala:74) | |
at com.reactore.samples.JsonConverter$.main(JsonConverter.scala:8) | |
at com.reactore.samples.JsonConverter.main(JsonConverter.scala) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.reactore.samples | |
case class Employee(id:Long, firstName:String, lastName:String) extends Copyable[Employee] with NameLike { | |
override val name: String = firstName + " " + lastName | |
} | |
object JsonConverter extends App { | |
val emp = Employee(1, "Yadu", "Krishnan") | |
val json = JsonUtilitiesImpl.toJson(emp) | |
println(json) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.reactore.samples | |
import java.text.SimpleDateFormat | |
import org.json4s._ | |
import org.json4s.JsonDSL._ | |
import org.json4s.jackson.JsonMethods._ | |
import scala.reflect.runtime.universe._ | |
abstract class Copyable[T:TypeTag] { | |
def tag:TypeTag[T] = typeTag[T] | |
} | |
trait NameLike { | |
val name: String | |
} | |
trait JsonUtilities { | |
self: TypeSerializers => | |
def toJson(obj: Any): String = { | |
compact(Extraction.decompose(obj)) | |
} | |
} | |
trait TypeSerializers { | |
implicit lazy val serializerFormats: Formats = new DefaultFormats { | |
override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") | |
override val fieldSerializers: List[(Class[_], FieldSerializer[_])] = List((classOf[NameLike], FieldSerializer[NameLike]())) | |
} | |
} | |
object JsonUtilitiesImpl extends JsonUtilities with TypeSerializers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment