ludwig
06/15/2018, 10:33 AMnikolay
06/15/2018, 10:40 AMludwig
06/15/2018, 10:48 AMnikolay
06/15/2018, 12:25 PM(var type: TypeEnum, var layer: Layer)
you have var
? maybe it should be val
?ludwig
06/15/2018, 12:44 PMnikolay
06/15/2018, 1:32 PMtoJson
as was suggested? tbh not sure if it would work, because I’ve though that they are both mandatoryludwig
06/15/2018, 1:36 PMnikolay
06/15/2018, 1:40 PMludwig
06/15/2018, 1:43 PMnikolay
06/15/2018, 1:47 PMludwig
06/15/2018, 1:49 PMnikolay
06/15/2018, 2:04 PMlayer
. Because from Response class I see that your JSON supposed to have 2 fields. Something like:
{
"type": "X" (or "Y"),
"layer": ..... (not clear)
}
My suggestion would be to have another class, let’s say ResponseRaw
which would be exactly like your JSON (and it should not include Layer
in the structure).
Something like:
{
"type": "X" (or "Y"),
"something": {
"variable1": 1,
"variable2": 2
}
}
data class ResponseRaw(val type: TypeEnum, val something: Something)
And then your adapter would looks differently:
class LayerAdapter {
@FromJson
fun fromJson(response: ResponseRaw) : Layer = when(response.type) {
Y -> YLayer()
X -> XLayer()
}
@ToJson
fun toJson(layer: Layer) : ResponseRaw = when(layer) {
is YLayer -> ResponseRaw(Y, layer.toSomething())
is XLayer -> ResponseRaw(X, layer.toSomething())
}
}
ludwig
06/15/2018, 2:08 PMnikolay
06/15/2018, 2:09 PMLayer
, because it doesn’t know which one to pickludwig
06/15/2018, 2:10 PMnikolay
06/15/2018, 2:10 PMLayer
to take?ludwig
06/15/2018, 2:11 PMnikolay
06/15/2018, 2:12 PMclass StageAdapter {
@FromJson fun fromJson(jsonReader: JsonReader, delegate: JsonAdapter<Stage>): Stage? {
val value = jsonReader.nextString()
return if (value.startsWith("in-progress")) Stage.IN_PROGRESS else delegate.fromJsonValue(value)
}
}
class Response(var type: TypeEnum, var layer: Layer)
ludwig
06/15/2018, 2:14 PMnikolay
06/15/2018, 2:15 PMResponse
as paramjsonReader: JsonReader, delegate: JsonAdapter<Stage>
thereludwig
06/15/2018, 2:15 PMnikolay
06/15/2018, 2:18 PMludwig
06/15/2018, 2:19 PMnikolay
06/15/2018, 2:19 PMclass LayerAdapter {
@FromJson
fun fromJson(layerJson: LayerJson): Layer = when (layerJson.layerType) {
LayerType.SHAPE -> ShapeLayer(layerJson.layerType, layerJson.shape ?: "")
LayerType.TEXT -> TextLayer(layerJson.layerType, layerJson.text ?: "")
LayerType.IMAGE -> ImageLayer(layerJson.layerType, layerJson.image ?: "")
}
@ToJson
fun toJson(layer: Layer): LayerJson = when (layer) {
is ShapeLayer -> LayerJson(layer.type, shape = layer.shape)
is TextLayer -> LayerJson(layer.type, text = layer.text)
is ImageLayer -> LayerJson(layer.type, image = layer.image)
else -> throw RuntimeException("Not support data type")
}
}
ludwig
06/15/2018, 2:20 PMnikolay
06/15/2018, 2:21 PM{
"type": "X",
"layer": {
"type": "LayerX",
"prop": 42
}
}
and
{
"type": "X",
"layer": {
"type": "LayerY",
"prop": 100
}
}
How Moshi could understand how to parse it to your Response
class?ludwig
06/15/2018, 2:23 PMnikolay
06/15/2018, 2:23 PMLayer
!!"layer": {
"type": "LayerY",
"prop": 100
}
could not be parsed to Layer
fromJson
ludwig
06/15/2018, 2:24 PMnikolay
06/15/2018, 2:26 PMludwig
06/15/2018, 2:26 PMnikolay
06/15/2018, 2:26 PMludwig
06/15/2018, 2:27 PMnikolay
06/15/2018, 2:29 PMludwig
06/15/2018, 2:29 PM