spray-json
spray-json copied to clipboard
jsonFormatN not working with Android
See https://groups.google.com/d/topic/spray-user/V9Gr69EBgHg/discussion
Output Log
05-03 06:48:19.529: ERROR/Issue#53(1413): x f:int m:int cond: falsey f:float m:float cond: false
05-03 06:48:19.539: ERROR/Issue#53(1413): blue f:double m:int cond: truegreen f:float m:float cond: falsered f:int m:double cond: true
05-03 06:48:19.559: ERROR/AndroidRuntime(1413): FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at spray.json.androidtest.SprayJsonTest.onCreate(SprayJsonTest.scala:53)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Cannot automatically determine case class field names and order for 'spray.json.androidtest.Color', please use the 'jsonFormat' overload with explicit field name specification
at spray.json.androidtest.TestJsonProtocol$.extractFieldNames(SprayJsonTest.scala:42)
at spray.json.ProductFormats$class.jsonFormat3(ProductFormats.scala:55)
at spray.json.androidtest.TestJsonProtocol$.jsonFormat3(SprayJsonTest.scala:17)
at spray.json.androidtest.TestJsonProtocol$.
Code:
package spray.json.androidtest
import android.app.Activity
import android.os.Bundle
import android.util.Log
import spray.json._
case class Location(x: Int = 0, y: Float = 0)
case class Color(red: Int = 0, green: Float = 0, blue: Double = 0)
case class Distance(x1: Int = 0, y1: Float = 0, x2: Double = 0, y2: Int = 0)
object TestJsonProtocol extends DefaultJsonProtocol {
implicit val LocationJsonFormat = jsonFormat2(Location)
implicit val ColorJsonFormat = jsonFormat3(Color)
implicit val DistanceJsonFormat = jsonFormat4(Distance)
override protected def extractFieldNames(classManifest: ClassManifest[_]): Array[String] = {
val clazz = classManifest.erasure
try {
// copy methods have the form copy$default$N(), we need to sort them in order, but must account for the fact
// that lexical sorting of ...8(), ...9(), ...10() is not correct, so we extract N and sort by N.toInt
val copyDefaultMethods = clazz.getMethods.filter(_.getName.startsWith("copy$default$")).sortBy(
_.getName.drop("copy$default$".length).takeWhile(_ != '(').toInt)
val fields = clazz.getDeclaredFields.filterNot(_.getName.startsWith("$"))
if (copyDefaultMethods.length != fields.length)
sys.error("Case class " + clazz.getName + " declares additional fields")
val issueOutput= fields.zip(copyDefaultMethods).map {
case (f, m) => f.getName + " f:" + f.getType + " m:" + m.getReturnType + " cond: " + (f.getType != m.getReturnType)
}.mkString
Log.e("Issue#53",issueOutput)
if (fields.zip(copyDefaultMethods).exists { case (f, m) => f.getType != m.getReturnType })
sys.error("Cannot determine field order of case class " + clazz.getName)
fields.map(_.getName)
} catch {
case ex => throw new RuntimeException("Cannot automatically determine case class field names and order " +
"for '" + clazz.getName + "', please use the 'jsonFormat' overload with explicit field name specification", ex)
}
}
}
class SprayJsonTest extends Activity {
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
import TestJsonProtocol._
Color().toJson
}
}
Thanks, this helps much.
I am also experiencing similar issues on Android. The library suggestes me to use the jsonFormat where I explicitly declare each type. I could not find anything like that inside documentation. Does the error message suggests me to use this method: Providing JsonFormats for other Types ? Thank you!
Hi pligor,
Please refer to the section "Providing JsonFormats for other Types" within the README
I don't think you need to get down to the level of the "Providing JsonFormats for other Types". As a workaround, just don't use the jsonFormat1, jsonFormat2, etc. methods and instead use the jsonFormat overloads where you have to pass the json field names explicitly. This adds some pain but doesn't rely on reflection and so should work properly on Android.
Sorry for being like that but what is the sample code for that? I guess it is just one line but still could not find it inside documentation. Thanks again!
Never mind I got it. It is something like this:
implicit val itsJsonFormat = jsonFormat[String, Option[String], SigninState](
SigninState.apply, "SIGNIN_STATE", "TOKEN"
);
first the types of the parameters, then as arguments first the apply method of the case class and also each name, with same order, of the corresponding json keys Either way much more neat! Thanks!