You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Though admittedly the reason an exception was thrown was my own fault, there was not actually a problem with nesting as the message would have me believe, but rather I had implemented a JsonAdapter that did not actually write any value, and then KotlinJsonAdapter continued to try and write other entries.
Here is some code that causes this situation.
sealedclassMyType {
data classMyInt(valint:Int): MyType()
data classMyString(valstring:String): MyType()
}
data classMyContainer(
valvalue:MyType?,
valotherValue:Int
)
object MyAdapter: JsonAdapter<MyType>() {
@ToJson
overridefuntoJson(writer:JsonWriter, request:MyType?) {
when (request) {
isMyType.MyInt-> writer.value(request.int)
isMyType.MyString-> writer.value(request.string)
// whoops I forgot to handle the null case!
}
}
@FromJson
overridefunfromJson(reader:JsonReader): MyType? {
TODO("Not relevant")
}
}
val converter =Moshi.Builder()
.add(MyAdapter)
.add(KotlinJsonAdapterFactory())
.build()
val container =MyContainer(null, 3)
val json = converter
.adapter(MyContainer::class.java)
.toJson(container) // Uh oh... Nesting problem!
I think this case is one where it would be preferable to fail earlier, and show a more meaningful error message to the user, such as the "Dangling name: __" one you would get if the MyContainer type did not have a second val in it. This could save someone else in this situation lot of unneeded debugging!
Here is KotlinJsonAdapter where the problem is encountered.
overridefuntoJson(writer:JsonWriter, value:T?) {
if (value ==null) throwNullPointerException("value == null")
writer.beginObject()
for (binding in bindings) {
if (binding ==null) continue// Skip constructor parameters that aren't properties.
writer.name(binding.name)
binding.adapter.toJson(writer, binding.get(value))
// Suggestion: detect if a value as not actually been written around here// and throw an exception!// Alternatively, just use a better error message in the Writer implementation// when deferredName is not null
}
writer.endObject()
}
The text was updated successfully, but these errors were encountered:
Though admittedly the reason an exception was thrown was my own fault, there was not actually a problem with nesting as the message would have me believe, but rather I had implemented a JsonAdapter that did not actually write any value, and then
KotlinJsonAdapter
continued to try and write other entries.Here is some code that causes this situation.
I think this case is one where it would be preferable to fail earlier, and show a more meaningful error message to the user, such as the "Dangling name: __" one you would get if the
MyContainer
type did not have a secondval
in it. This could save someone else in this situation lot of unneeded debugging!Here is
KotlinJsonAdapter
where the problem is encountered.The text was updated successfully, but these errors were encountered: