Use of Parceler with Kotlin data class with constructor for serialization
Asked Answered
M

4

23

Is there a way to use Parceler with Kotlin data classes and constructor for serialization without using @ParcelProperty annotation for each field?

If I try and use library like this:

@Parcel
data class Valve @ParcelConstructor constructor(val size: Int)

I get Error:Parceler: No corresponding property found for constructor parameter arg0. But if I add @ParcelProperty("size") it works just fine.
Why is that?

Update:
There are other another way to use this library.
I could just remove @ParcelConstructor annotation, but then I will get error
Error:Parceler: No @ParcelConstructor annotated constructor and no default empty bean constructor found.
I think (haven't tested it) I also could make all constructor parameters optional and add @JvmOverloads but that has a side effect that I have to check all properties of the class if they are null or not.

Update 2:
This is what worked for me:

@Parcel
data class Valve(val size: Int? = null)

In short generated Java class must have default empty constructor. One way to achieve that is to do as above - all variables should have default values.

Meredith answered 24/11, 2015 at 10:59 Comment(0)
O
29

According to the docs, Parceler by default works with public fields. But a usual Kotlin data class (as in your example) is rather a "traditional getter/setter bean", since every Kotlin property is represented by a private field and a getter/[setter].

TL; DR: I think this will work:

@Parcel(Serialization.BEAN)
data class Valve(val size: Int = 10)

Note the default value, it allows Kotlin to automatically generate an additional empty constructor, which is required by the Java Been specification.

Another way would be to mark the constructor that we already have:

@Parcel(Serialization.BEAN)
data class Driver @ParcelConstructor constructor(val name: String)

The specific document: https://github.com/johncarl81/parceler#gettersetter-serialization

Overelaborate answered 24/11, 2015 at 11:28 Comment(10)
That wouldn't work since it requires default empty bean constructor. I'll update my question.Meredith
to have an empty constructor you can provide a default value to size. UpdatedOverelaborate
or annotate the primary constructor with @ParcelConstructor just as in your exampleOverelaborate
btw, for having an empty constructor you should not need @JvmOverloads, what I said above should be sufficient. Let me know when you try it outOverelaborate
@Overelaborate how did you compiled parceler to kotlin? im not being able to make Parcel to work in Kotlin.Kandrakandy
@FábioCarballo I never did. For my project (a server) I use Jackson (kotlin module). I answered because the issue with Parcel was of the same nature that the one I solver with Jackson, and the syntax I looked up in the docs.Overelaborate
You may ask @MartynasJurkus, he made it work (obviously). For that you may want to post the compilation error you are getting. Or create a separate question, since chances are you problem is build-related, not kotlin-related.Overelaborate
@FábioCarballo see my updated question on how I got it working based on this answerMeredith
Using this approach I can get everything to compile, but it doesn't work, because the generated Value$$Parcelable class doesn't contain any code to write/read size. Am I missing something, has this been proved to work?Sassan
also worth noting in some scenarios val should be var if the value is expected to changeAmygdaloid
A
10

I know this question already has an answer, but for future viewers who are also struggling to get Parceler to work with kotlin data objects, I wrote a new annotation processor to generate the Parcelable boilerplate for Kotlin data classes. It's designed to massively reduce the boilerplate code in making your data classes Parcelable:

https://github.com/grandstaish/paperparcel

Usage:

Annotate your data class with @PaperParcel, implement PaperParcelable, and add a JVM static instance of the generated CREATOR e.g.:

@PaperParcel
data class Example(
  val test: Int,
  ...
) : PaperParcelable {
  companion object {
    @JvmField val CREATOR = PaperParcelExample.CREATOR
  }
}

Now your data class is Parcelable and can be passed directly to a Bundle or Intent

Edit: Update with latest API

Akanke answered 3/2, 2016 at 6:29 Comment(0)
M
5

Just add the default constructor:

@Parcel
data class Valve(val size: Int) {
    constructor() : this(0)
}
Multiped answered 26/2, 2016 at 0:47 Comment(3)
Why downvote? I had the same issue and fixed it exactly this way.Multiped
This works for me. BTW you need to use kapt: compile 'org.parceler:parceler-api:1.1.6' kapt 'org.parceler:parceler:1.1.6'Hireling
Specifying size with a default value (as mentioned above) should fulfill the need for the parameterless constructor, no?Quillan
D
4

if you use Kotlin 1.1.4 or above it's easier to use @Parcelize annotation

For doing this first add this to build.gradle

android {
    //other codes

    //for using latest experimental build of Android Extensions
    androidExtensions {
        experimental = true
    }
}

Then change your class like this

@Parcelize
data class Valve(val size: Int? = null) : Parcelable
Doralia answered 12/5, 2019 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.