https://kotlinlang.org logo
#android
Title
# android
j

Joe

03/26/2018, 10:22 AM
I am using Moshi to deserialize json from our server but I have come across an issue I’m sure has a solution, I just can’t see it. Over the socket, we are send json that, at the top level, has three fields:
Copy code
{
    "data_type": "<actual_data_type>",
    "data_id": "<actual_data_id>",
    "data": <data_object>
}
The issue is that the
data
can actually be several different objects based on what
data_type
is can I’m not sure how to pass that information into the adaptor for
Data
. I’ve tried a couple different things, but it just gets closer and closer to me parsing the whole thing myself, which seems to defeat the point. Is there a way to pass information from one adaptor to another?
e

eric

03/26/2018, 3:46 PM
This might help you get started. You'll want to buffer the object and then delegate out the deserialing to the proper adapter with the fromJson(Object) overload. https://github.com/square/moshi/pull/264/files
👍 1
j

Joe

03/26/2018, 6:31 PM
Yes, I found this a few hours ago and have been trying to pull it apart. But thanks, it confirms I’m at least running in the right direction! 🙂
👍 1
3 Views