Stylianos Gakis
06/23/2022, 3:02 PMStylianos Gakis
06/23/2022, 3:02 PMscalar PaymentMethodsResponse
which when going inside the PaymentMethodsApiResponseAdapter I am building, the reader is in a state where peekedToken is BEGIN_OBJECT and peekedData is a LinkedHashMap. As I understand that is because the object in the json starts with a {
.
This is a test with a json of how the backend would respond (from a real response).
And this is the adapter where this line is hit and I do in fact get a LinkedHashMap as I am debugging this.
Thing is, there is no such function on the JsonReader to read this normally, and the only approached I have found that works is calling the @ApolloInternal readAny()
and then checking if its type is LinkedHashMap but I am afraid doing this will just break in future releases since it’s supposed to be internal.
Does this seem like something that our backend is doing wrong instead of something that the library should be handling differently? Or any ideas?mbonnin
06/23/2022, 3:06 PMmbonnin
06/23/2022, 3:07 PMbeginObject()/endObject()
mbonnin
06/23/2022, 3:10 PMwhen
statement in a loop:
class PaymentMethodsResponse(
val success: Boolean,
val code: String
)
val adapter = object: Adapter<PaymentMethodsResponse> {
override fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): PaymentMethodsResponse {
reader.beginObject()
var success: Boolean? = null
var code: String? = null
while (reader.hasNext()) {
val name = reader.nextName()
when (name) {
"success" -> success = reader.nextBoolean()
"code" -> code = reader.nextString()
}
}
reader.endObject()
return PaymentMethodsResponse(
success ?: error("'success' is missing"),
code ?: error("'code' is missing")
)
}
mbonnin
06/23/2022, 3:11 PMmbonnin
06/23/2022, 3:12 PMAnyAdapter
instead of using readAny
val map = AnyAdapter.fromJson(reader, customScalarAdapters)
AnyAdapter
is public API and isn't going anywhere any time soonStylianos Gakis
06/23/2022, 3:15 PMPaymentMethodsApiResponse.SERIALIZER
which comes from the Adyen SDK which should just know how to handle the rest. So the AnyAdapter is what I was looking for all along I think!
That feels so much better, let me try and get it working real quick!Stylianos Gakis
06/23/2022, 3:18 PMoverride fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): PaymentMethodsApiResponse {
val data = AnyAdapter.fromJson(reader, customScalarAdapters)
return if (data is LinkedHashMap<*, *>) {
PaymentMethodsApiResponse.SERIALIZER.deserialize(JSONObject(data.toMap()))
} else {
val jsonString = data.toString().replace("\\", "")
PaymentMethodsApiResponse.SERIALIZER.deserialize(JSONObject(jsonString))
}
}
mbonnin
06/23/2022, 3:19 PMLinkedHashMap
? Could it be Map<*, *>
instead?Stylianos Gakis
06/23/2022, 3:22 PMmbonnin
06/23/2022, 3:23 PMmbonnin
06/23/2022, 3:23 PMStylianos Gakis
06/23/2022, 3:23 PMmbonnin
06/23/2022, 3:23 PMmbonnin
06/23/2022, 3:24 PMStylianos Gakis
06/23/2022, 3:25 PM