-
Notifications
You must be signed in to change notification settings - Fork 306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Kotlin model classes #116
Comments
Any information on this? |
Also very interested in this. |
KAPT matured pretty much lately, so I'm almost positive we can make LoganSquare kotlin-ready. I'll try some stuff and PR the outcome. |
I'd love to see this working as well. |
as would I! |
I just did some quick tests and found kapt to be already working with LoganSquare. This is the setup I used: build.gradle: dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.0"
compile "com.bluelinelabs:logansquare:1.3.5"
kapt "com.bluelinelabs:logansquare-compiler:1.3.5"
} Model.kt: @JsonObject
class Model {
@JsonField(name = arrayOf("value"))
var value: String? = null
get
@OnJsonParseComplete
fun onParseComplete(): Unit {
println("onParseComplete")
}
@OnPreJsonSerialize
fun onPreSerialize(): Unit {
println("onPreSerialize")
}
} Main.kt: fun main(args: Array<String>) {
var model: Model = LoganSquare.parse("{\"value\":\"HELLO\"}", Model::class.java)
println("model: ${model.value}")
println("json: ${LoganSquare.serialize(model)}")
} Output: onParseComplete
model: HELLO
onPreSerialize
json: {"value":"HELLO"} Let's collect further improvements that we would like to see made to LoganSquare in this regard! |
That's awesome @aurae! I haven't had time to get into Kotlin anywhere near as much as I'd like to yet, so I'm probably not the one who should be driving these ideas forward. If something either doesn't work correctly or doesn't feel right for Kotlin dev, please post issues! I should be starting my first full Kotlin project in the next few weeks, so I'll be able to contribute soon(ish). Also, PR's are definitely welcome @mradzinski |
@aurae I have had similar success...Have you used the logansquare compiler with retrofit yet? |
I haven't tried retrofit-logansquare in a Kotlin environment yet, but I would assume that it works. I can't say for sure though! |
@aurae The problem with your test is that you are not attaching to Kotlin idiom guidelines. As per Kotlin documentation, DTO's should not be simple Kotlin classes but data classes.
The reason for this is that Kotlin generates the getters and setters for you, plus you get I'll check if there's a way to modify logansquare-compiler to look for constructor fields (like the ones present in data classes) instead of annotated ones and ignore those which are not part of the constructor itself (consider them transient fields). Kotlin first constructors become plain and simple java ones at compile time, so I guess it shouldn't be that hard to check for the constructor method instead of the annotated fields. You can find more information about Kotlin idioms here: https://kotlinlang.org/docs/reference/idioms.html |
Okay, that makes sense. Indeed, I also wasn't able to make LS work with data classes, hence the example with an "ordinary" class. Supporting constructor fields brings us back to #3, which would in turn allow Kotlin data classes to work as well, I think. |
Anyone have ideas on how using constructors would ideally work? Would it try to match member variable names w/ constructor parameters names (very error prone)? Or would the dev be expected to annotate their constructor params (kinda ugly)? Or is there another option I haven't considered? |
I would want to make a data class the normal way and then add the annotation:
And if you need to customize a field you can annotate in the constructor:
|
I don't know why, but my parse is returning @JsonObject
class Line(@PrimaryKey @JsonField var id : Int,
@JsonField var name : String) : Parcelable {
companion object {
@JsonIgnore @JvmField final val CREATOR: Parcelable.Creator<Line> =
object : Parcelable.Creator<Line> {
override fun createFromParcel(source: Parcel): Line = Line(source)
override fun newArray(size: Int): Array<Line?> = arrayOfNulls(size)
}
}
constructor(parcel : Parcel) : this(
parcel.readInt(),
parcel.readString())
override fun writeToParcel(parcel: Parcel, flag: Int) {
parcel.writeInt(id)
parcel.writeString(name)
}
override fun describeContents(): Int = 0
} |
The LoganSquare is not mapping all anotated fields, tested in 2 PCs with differents projects. |
Unfortunately, LoganSquare still not working with Kotlin. I will use another parser. Sorry. |
Any news on this? 😸 |
guys, have any changes to support the data-classes of Kotlin? I see the project does not develop ;( |
I have created a pull request, that implements this feature — #213 |
For me this syntax works fine:
Basically you have to provide a default value for each field, so Kotlin will generate an empty constructor, needed by LS. Didn't fully tested, but in my simple use case does the trick. |
@LuigiPapino Your approach works only for fully mutable data classes (where all fields are Every objection I have heard against use of LoganSquare have been motivated by lack of support for immutable classes. Developers, who express such concerns in the first place, are unlikely to adapt their data classes to make LoganSquare work. |
I guess depends on the use case. |
No description provided.
The text was updated successfully, but these errors were encountered: