<@U8501EVRD> Yes, sorry, that is correct, it was a...
# android
l
@nikolay Yes, sorry, that is correct, it was a typo in the example
n
have you tried to debug? where it gets stuck?
l
Yes, it keeps thinking that it needs to parse toJson whenever it reaches the layer
So it gets stuck in a recursion
n
btw, why here
(var type: TypeEnum, var layer: Layer)
you have
var
? maybe it should be
val
?
l
I’ve tried that too, it doesn’t matter unfortunately
n
have you tried without
toJson
as was suggested? tbh not sure if it would work, because I’ve though that they are both mandatory
l
it is mandatory, so I can’t try it
n
maybe you could post JSON that you trying to parse ?
I’m really curious about issue, because I’m currently migrating to Moshi
l
I can’t post the exact one, but it has the exact same structure as the example
n
okey
l
Other than this issue, Moshi is great, so don’t be discouraged 🙂
n
Why have I asked about JSON, because I have one idea. Point is that I could not imagine what is coming in JSON under name
layer
. Because from Response class I see that your JSON supposed to have 2 fields. Something like:
Copy code
{
    "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:
Copy code
{
    "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:
Copy code
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())
    }
}
I hope you get what I mean 🙂
l
Thank you very much for the effort, and you’re correct, that works, and that’s an implementation that I can’t use, because we are using this format in almost every request, and some of these “layers”, have up to 30 different cases, so it would be a huge mess when dealing with it, hence why I want it to be explicit types that are being parsed directly 🙂
n
but I think Moshi simply could not parse your JSON to
Layer
, because it doesn’t know which one to pick
l
But isn’t that what I’m trying to tell Moshi? What to parse that is
n
nope
how should it distinguish which
Layer
to take?
l
What is the when(layer) { is YLayer -> etc } for then?
n
you should probably write more low level adapter, that operates with strings
Copy code
class 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)
  }
}
but you have
class Response(var type: TypeEnum, var layer: Layer)
how would it parse Layer here?
l
Response = when(layer) { is YLayer -> Response(Y, layer)} - I’m creating a new Response, with the specified layer?
Response = when(layer) { is YLayer -> Response(Y, YLayer() )
Isnt that parsing the correct layer?
n
no, it is already taking
Response
as param
pass
jsonReader: JsonReader, delegate: JsonAdapter<Stage>
there
or something
l
I’m talking about toJson btw
is it you? 🙂
l
Nope, just used the Layer as an example 🙂
n
Copy code
class 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")
    }
}
that’s more or less what I’m trying to say
l
Yes, and I can’t be using that, because we are re-using the Layer type everywhere, which will then in some cases have 30 optional parameters as they have defined in that example
I’m trying to go one step further, and not have that, instead parse them out directly
n
Let’s say we have two JSONs
Copy code
{
  "type": "X",
  "layer": {
    "type": "LayerX",
    "prop": 42
  }
}
and
Copy code
{
  "type": "X",
  "layer": {
    "type": "LayerY",
    "prop": 100
  }
}
How Moshi could understand how to parse it to your
Response
class?
l
@ToJson fun toJson(layer: Layer) : Response = when(layer) { is YLayer -> Response(Y, LayerY(prop = layer.prop)) is XLayer -> Response(X, LayerX(prop = layer.prop)) } Because the sealed class’s has different fields on them
n
man, it takes as param
Layer
!!
🙂
but
Copy code
"layer": {
        "type": "LayerY",
        "prop": 100
    }
could not be parsed to
Layer
and I’m talking about
fromJson
l
And I’m talking about the toJson!
sealed class Layer() class YLayer(type: String, prop: Int) : Layer And now it can be parsed
n
no, it can’t, I think
l
The parsing works, believe it or not, because I get the object when I’m doing it like this, the only thing that isn’t working as expected, is when it’s done, it starts to nest the layer
n
anyway, I think either I don’t understand the problem or I could not explain what I mean 🙂
l
The actual parsing from toJson and fromJson works when you run it once, then I get the real objects, depending on what type of Layer I send in, or what Response is coming out. It’s when it’s done parsing, it starts to parse it again, and again 🙂
Run it once using the debugger of course
n
then I really don’t get your issue, sorry man!
l
Thanks for trying to help!