Skip to content
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

How to create TypeAdapterFactory for registering this Measure<Length> or Measure<Mass> #6

Open
SujathaKR625 opened this issue Jun 11, 2024 · 3 comments

Comments

@SujathaKR625
Copy link

If we want to register type adapter for the type how to create it?

@pusolito
Copy link
Collaborator

Can you say a bit more about what you'd like to do? I'm not sure if you mean GSon's TypeAdapterFactory for example.

@SujathaKR625
Copy link
Author

SujathaKR625 commented Jun 12, 2024

Yes. I am using Measure with meters unit and Measure with kilograms unit

The json content has weight: 1000.0,height : 8 like that.

I tried to write a typeadapter facory class MeasureTypeAdapterFactory : TypeAdapterFactory {
override fun create(gson: Gson, type: TypeToken): TypeAdapter? {
return when (type.rawType) {
Measure::class.java -> MeasureAdapter() as TypeAdapter
else -> null
}
}
}

But it is not working as expected.

The json should parse and become a object in kotlin data class Dimensions {

val height: Measure = Measure(0.0, Length.meters),
val weight: Measure = Measure(0.0, Mass.kilograms),
}

The one I implemented is not correct. Can you please help with it. I am not able to find good examples as well.

@pusolito
Copy link
Collaborator

Are you working in Kotlin or Java? If Kotlin, are you able to use Kotlin serialization, or is GSon a requirement?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants