nacular / measured

Intuitive, type-safe units of measure
https://nacular.github.io/measured
MIT License
156 stars 6 forks source link

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

Open SujathaKR625 opened 3 months ago

SujathaKR625 commented 3 months ago

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

pusolito commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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