Comments (7)
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.
Thanks, this helps much.
from spray-json.
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.
Hi pligor,
Please refer to the section "Providing JsonFormats for other Types" within the README
from spray-json.
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.
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.
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)
- Publish for Scala 2.13.0 HOT 3
- Provide implicits automatically without extra imports HOT 1
- Provide jsonFormatN method overloads HOT 4
- JsonParserSpecJvm is failing HOT 3
- Documentation review and migration guide for 1.5.0 release
- How to debug "Cannot find JsonWriter or JsonFormatter type class" errors
- Not able to parse the Json file with its relation_tree and split into multiple Json file for each child without any hard coding of Node/Tag (As structure of Json is not always same also very lengthy json). Input and Output is pasted below :
- Publish 1.3.6 release (including Scala.js) HOT 1
- Scaladoc
- DefaultJsonProtocol.viaSeq looses ordering for ordered iterables HOT 10
- Is JsonParser thread safe?
- Default JsonWriter for collections
- recursive Json Does not seem to work as documented HOT 3
- What are your plans for spray-json regarding Scala 3? HOT 4
- Generated jsonFormatN has unexpectedly large sizeHint HOT 1
- Cross-compile for scala 3? HOT 1
- Introduce an optional Scala 3 macro module, with full pickling support.
- Is this project still maintained? HOT 2
- No implicit arguments of type: JsonReader[Double] HOT 1
- Cannot parse as Long from String format field
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spray-json.