Tag: gson

如何在Kotlin的Map实例中使用`filter`?

我看到#filter是在Map上定义的,但我无法弄清楚如何使用它。 任何人都在意分享一个例子? 我有一个深层嵌套的TreeMap实例( TreeMap<String, Map<String, Map<*, *>>> ),我想filter/find第一个(这是唯一的域)顶级密钥某些与价值更深层次有关的特征。 以下是数据的样子: { "i1": { "aliases": {} }, "i2": { "aliases": {} }, "i3": { "aliases": {} }, "i4": { "aliases": { "alias-im-looking-for": {} } } } 我有下面这个非功能性的代码,现在解决它: val indexToAliasMappingType = LinkedTreeMap<String, Map<String, Map<*, *>>>() val indexToAliasMappings = Gson().fromJson(response.jsonString, indexToAliasMappingType.javaClass) var currentIndexName = "" for ((index, aliasMappings) in […]

用Kotlin进行Gson反序列化,不调用初始化块

当我创建我的对象时,我的初始化块工作得很好 class ObjectToDeserialize(var someString: String = "") : Serializable { init{ someString += " initialized" } } 这条路: @Test fun createObject_checkIfInitialized() { assertEquals("someString initialized",ObjectToDeserialize("someString").someString) } 但是,当我用Gson反序列化对象时,初始化块不会被执行: @Test fun deserializeObject_checkIfInitialized(){ val someJson: String = "{\"someString\":\"someString\" }" val jsonObject = Gson().fromJson(someJson, ObjectToDeserialize::class.java) assertEquals("someString initialized",jsonObject.someString) // Expected :someString initialized // Actual :someString } 我认为,gson创建对象的方式不同于执行主构造函数。 是否有可能有类似的初始化块?

Realm&Kotlin访问字段的变量

我有以下情况: 我通过Retrofit2&GSON获取一些数据,我不想完全保存在领域数据库中。 但是我需要稍后访问模型。 那么这里是模型: open class Notification() : RealmObject() { @PrimaryKey var pushNotificationId: Long = -1 var date: Date = Date() var apsRaw: String = "" @Ignore var aps: Aps? = null get() = field ?: Gson.getInstance().fromJson(apsRaw, Aps::class.java) private set 同 open class Aps(var message: String = "", var category: String = "") 我想实现的是,只保存领域的apsRaw字符串( 以避免有另一个表“Aps” […]

如何在Kotlin中使用Gson的TypeToken +泛型

我无法从自定义类(Turns)中获取泛型类型的列表: val turnsType = TypeToken<List<Turns>>() {}.type val turns = Gson().fromJson(pref.turns, turnsType) 它说: cannot access '<init>' it is 'public /*package*/' in 'TypeToken'

GSON中的Kotlin代表如何反序列化

我有这个班级: class Project { val nameProperty = SimpleStringProperty("foobar") val name by nameProperty } 我使用Fx-GSON库来序列化JavaFx属性。 当我序列化到JSON我得到这个: { "nameProperty": "foobar", "name$delegate": "foobar" } 但是,当我将其反序列化回到Project类型的对象时, name和nameProperty是2个不同的对象 。 如何使name属性委托给新的nameProperty ?

如何使用Kotlin enum和Retrofit?

我怎样才能解析JSON模型与枚举? 这是我的枚举类: enum class VehicleEnumEntity(val value: String) { CAR("vehicle"), MOTORCYCLE("motorcycle"), VAN("van"), MOTORHOME("motorhome"), OTHER("other") } 我需要解析type为一个枚举 “vehicle”:{“data”:{“type”:“vehicle”,“id”:“F9dubDYLYN”}} 编辑 我试过标准的方式,只是通过我的枚举POJO,它总是空

Kotlin TypeToken“未解决的参考”错误

第5,6和7行中出现错误“未解决的响应引用”。您能帮我理解这个问题吗? public class GenericCall { public fun genecicCall(functionToBeCalled:String, responseType:String, requestType:String, vararg args:String){ var request = Class.forName(requestType).newInstance() var response = Class.forName(responseType).newInstance() var finalType = object : TypeToken<GenericResponse<Class<response>>>(){}.getType() var creditresponse: GenericResponse<response>? = AjaxHelper.ajax(Constants.Ajax.ENDPOINT_CREDIT, Constants.Ajax.REQUEST_POST, request, finalType, null) return ResponseProcessing.processResponse(creditresponse as Any) as response? } } }

使用GSON进行Kotlin对象反序列化

我有一堂课 class ThreadComment( banned: Int, closed: Int, comment: String?, date: String?, email: String?, files: ArrayList<File>?, lasthit: Int, name: String?, num: String?, op: Int, parent: String?, postsCount: Int, sticky: Int, subject: String?, tags: String?, timestamp: Int, trip: String?) : Comment(banned, closed, comment, date, email, files, lasthit, name, num, op, parent, postsCount, sticky, subject, tags, timestamp, trip) […]

Kotlin和@Transient

一堂课: open class MessageDTO : RealmObject, Serializable { @PrimaryKey @SerializedName("message_id") var messageId: String? = null @SerializedName("chat") var chat: String? = null @SerializedName("chat_type") var chatType: String? = null @SerializedName("content") var content: ContentDTO? = null @SerializedName("created") var created: Date? = null @SerializedName("from") var from: String? = null @SerializedName("important") var important: Boolean? = null @SerializedName("is_first") var isFirst: Boolean? […]

如何使用Gson反序列化继承的Kotlin数据类

在Android应用程序中,我需要为具有单一抽象级别的Kotlin数据类反序列化Json数据。 但我没有任何想法,在构造函数中放置正确的属性。 作为一个简单的版本,让我们说我有一个形状: abstract class Shape(open val x: Int, open val y: Int) 有两个派生 data class Circle(val radius: Int, override val x: Int, override val y: Int): Shape(x, y) 和 data class Square(val width: Int, override val x: Int, override val y: Int): Shape(x, y) 所以我的目标是,不要实例化一个形状。 所以,总是反序列化它的派生。 后来我需要处理其他类中的一些集合属性,如: val shapes: List<Shape> 但我也必须知道每个元素的派生类型。 当我试图用Gson反序列化给定的例子 val square […]