簡體   English   中英

json4s在Android上將案例類Test(“ test”)序列化為“ {}”

[英]json4s serializes a case class Test(“test”) to “{}” on Android

我在Android Scala應用程序中將案例類圖序列化為JSON(使用"com.hanhuy.sbt" % "android-sdk-plugin" % "1.3.5" )。

我正在使用"org.json4s" %% "json4s-native" % "3.2.10" ,即使使用簡單的case類,它也會失敗,如下所示:

package test
case class Test(text: String)

實際序列化的代碼如下所示:

import org.json4s._
import org.json4s.native.Serialization
import org.json4s.native.Serialization.{read, write}

// ...

implicit val formats = Serialization.formats(NoTypeHints)

val test = Test("test")
val serialized = write(test)
info(s"Serialized to '$serialized'")

輸出為:

Serialized to '{}'

我懷疑存在ProGuard問題,並且build.sbt中的ProGuard設置如下:

proguardScala in Android := true

proguardOptions in Android ++= Seq(
  "-dontobfuscate",
  "-dontoptimize",
  "-keepattributes Signature",
  "-dontwarn scala.collection.**", // required from Scala 2.11.3
  "-dontwarn scala.collection.mutable.**", // required from Scala 2.11.0
  "-ignorewarnings",
  "-keep class scala.Dynamic",
  "-keep class test.**"
)

我也嘗試了json4s-jackson但是沒有區別。

ProGuard日志如下所示:

Warning: com.thoughtworks.paranamer.AnnotationParanamer$Jsr330Helper: can't find referenced class javax.inject.Named
Warning: com.thoughtworks.paranamer.AnnotationParanamer$Jsr330Helper: can't find referenced class javax.inject.Named
Warning: org.joda.convert.JDKStringConverter$9: can't find referenced class javax.xml.bind.DatatypeConverter
Warning: org.joda.convert.JDKStringConverter$9: can't find referenced class javax.xml.bind.DatatypeConverter
Warning: org.joda.convert.JDKStringConverter$9: can't find referenced class javax.xml.bind.DatatypeConverter
Note: com.google.android.gms.internal.av calls '(com.google.ads.mediation.MediationAdapter)Class.forName(variable).newInstance()'
Note: com.google.android.gms.maps.internal.q: can't find dynamically referenced class com.google.android.gms.maps.internal.CreatorImpl
Note: org.joda.time.DateTimeZone calls '(org.joda.time.tz.Provider)Class.forName(variable).newInstance()'
Note: org.joda.time.DateTimeZone calls '(org.joda.time.tz.NameProvider)Class.forName(variable).newInstance()'
Note: there were 1 unresolved dynamic references to classes or interfaces.
  You should check if you need to specify additional program jars.
  (http://proguard.sourceforge.net/manual/troubleshooting.html#dynamicalclass)
Note: there were 3 class casts of dynamically created class instances.
  You might consider explicitly keeping the mentioned classes and/or
  their implementations (using '-keep').
  (http://proguard.sourceforge.net/manual/troubleshooting.html#dynamicalclasscast)
Warning: there were 5 unresolved references to classes or interfaces.
     You may need to add missing library jars or update their versions.
     If your code works fine without the missing classes, you can suppress
     the warnings with '-dontwarn' options.
     (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass)
Note: You're ignoring all warnings!

有任何想法嗎?

您可以使用構造函數從String創建JSONObject:

 JSONObject json = new JSONObject(myString);

要將您的JSONObject轉換為String,只需使用toString()方法:

 String myString = json.toString();

另外,如果您嘗試從JSONObject獲取特定的String值,則可以執行以下操作:

 if (json.has("content"))
{
    String content = json.getString("content");
    //do something with content string
}

最后,如果您不太習慣使用JSONObject,建議您使用droidQuery提供的工具來幫助您解析,例如:

Object[] array = $.toArray(myJSONArray);

Map<String, ?> map = $.map(myJSONObject);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM