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

Get Data from 2 different types #103

Open
famfir18 opened this issue Sep 23, 2019 · 1 comment
Open

Get Data from 2 different types #103

famfir18 opened this issue Sep 23, 2019 · 1 comment

Comments

@famfir18
Copy link

How to get data from one object, but that object have 2 different types?

I have object named 'metric', but 'metric' have 2 different types

Capture
Capture1

@Ant8
Copy link

Ant8 commented Oct 20, 2020

it appears you need to use abstract class and polymorphic adapter factory, like this:

    @JsonApi(type = TYPE)
    data class SomeType(
    ....
    val metric: HasOne<Metric> = HasOne()
    ...) : Resource()

then

    abstract class Metric : Resource()

and

    @JsonApi(type = "gamePointMetric")
    data class GamePointMetric(...) : Metric()

and finally

    val adapterFactory = ResourceAdapterFactory
        .builder()		        
        .add(GamePointMetric::class.java)
        .add(GameStateMetric::class.java)
        .build()
    ...
    Moshi.Builder()
       .add(adapterFactory)
       .add(PolymorphicJsonAdapterFactory.of(Metric::class.java, "type")
                .withSubtype(GamePointMetric::class.java, "GamePointMetric")
                .withSubtype(GameStateMetric::class.java, "GameStateMetric")
        )
        .build()

solution thanks to @MDikkii

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