GithubHelp home page GithubHelp logo

Comments (7)

hyysguyang avatar hyysguyang commented on July 18, 2024

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$.(SprayJsonTest.scala:19)
at spray.json.androidtest.TestJsonProtocol$.(SprayJsonTest.scala)
... 15 more
Caused by: java.lang.RuntimeException: Cannot determine field order of case class spray.json.androidtest.Color
at scala.sys.package$.error(package.scala:27)
at spray.json.androidtest.TestJsonProtocol$.extractFieldNames(SprayJsonTest.scala:39)
... 19 more

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

  }
}

from spray-json.

jrudolph avatar jrudolph commented on July 18, 2024

Thanks, this helps much.

from spray-json.

pligor avatar pligor commented on July 18, 2024

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!

from spray-json.

hyysguyang avatar hyysguyang commented on July 18, 2024

Hi pligor,

Please refer to the section "Providing JsonFormats for other Types" within the README

from spray-json.

jrudolph avatar jrudolph commented on July 18, 2024

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.

from spray-json.

pligor avatar pligor commented on July 18, 2024

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!

from spray-json.

pligor avatar pligor commented on July 18, 2024

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!

from spray-json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.