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

ludwig

06/15/2018, 10:33 AM
@nikolay Yes, sorry, that is correct, it was a typo in the example
n

nikolay

06/15/2018, 10:40 AM
have you tried to debug? where it gets stuck?
l

ludwig

06/15/2018, 10:48 AM
Yes, it keeps thinking that it needs to parse toJson whenever it reaches the layer
So it gets stuck in a recursion
n

nikolay

06/15/2018, 12:25 PM
btw, why here
(var type: TypeEnum, var layer: Layer)
you have
var
? maybe it should be
val
?
l

ludwig

06/15/2018, 12:44 PM
I’ve tried that too, it doesn’t matter unfortunately
n

nikolay

06/15/2018, 1:32 PM
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

ludwig

06/15/2018, 1:36 PM
it is mandatory, so I can’t try it
n

nikolay

06/15/2018, 1:40 PM
maybe you could post JSON that you trying to parse ?
I’m really curious about issue, because I’m currently migrating to Moshi
l

ludwig

06/15/2018, 1:43 PM
I can’t post the exact one, but it has the exact same structure as the example
n

nikolay

06/15/2018, 1:47 PM
okey
l

ludwig

06/15/2018, 1:49 PM
Other than this issue, Moshi is great, so don’t be discouraged 🙂
n

nikolay

06/15/2018, 2:04 PM
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

ludwig

06/15/2018, 2:08 PM
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

nikolay

06/15/2018, 2:09 PM
but I think Moshi simply could not parse your JSON to
Layer
, because it doesn’t know which one to pick
l

ludwig

06/15/2018, 2:10 PM
But isn’t that what I’m trying to tell Moshi? What to parse that is
n

nikolay

06/15/2018, 2:10 PM
nope
how should it distinguish which
Layer
to take?
l

ludwig

06/15/2018, 2:11 PM
What is the when(layer) { is YLayer -> etc } for then?
n

nikolay

06/15/2018, 2:12 PM
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

ludwig

06/15/2018, 2:14 PM
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

nikolay

06/15/2018, 2:15 PM
no, it is already taking
Response
as param
pass
jsonReader: JsonReader, delegate: JsonAdapter<Stage>
there
or something
l

ludwig

06/15/2018, 2:15 PM
I’m talking about toJson btw
is it you? 🙂
l

ludwig

06/15/2018, 2:19 PM
Nope, just used the Layer as an example 🙂
n

nikolay

06/15/2018, 2:19 PM
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

ludwig

06/15/2018, 2:20 PM
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

nikolay

06/15/2018, 2:21 PM
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

ludwig

06/15/2018, 2:23 PM
@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

nikolay

06/15/2018, 2:23 PM
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

ludwig

06/15/2018, 2:24 PM
And I’m talking about the toJson!
sealed class Layer() class YLayer(type: String, prop: Int) : Layer And now it can be parsed
n

nikolay

06/15/2018, 2:26 PM
no, it can’t, I think
l

ludwig

06/15/2018, 2:26 PM
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

nikolay

06/15/2018, 2:26 PM
anyway, I think either I don’t understand the problem or I could not explain what I mean 🙂
l

ludwig

06/15/2018, 2:27 PM
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

nikolay

06/15/2018, 2:29 PM
then I really don’t get your issue, sorry man!
l

ludwig

06/15/2018, 2:29 PM
Thanks for trying to help!