繁体   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