jk2018
11/26/2019, 11:06 PMjk2018
11/26/2019, 11:34 PMcedric
11/27/2019, 3:49 PMjk2018
11/27/2019, 6:20 PMcedric
11/27/2019, 6:32 PMjk2018
11/27/2019, 9:16 PMLeoColman
12/01/2019, 12:30 AMLeoColman
12/01/2019, 12:31 AMLeoColman
12/01/2019, 12:31 AMLeoColman
12/01/2019, 12:31 AMByron Woodfork
12/02/2019, 10:25 PMwhen
clause, I just want to do nothing. As opposed to throwing an exception. Or is there a graceful way to catch the exception once it's thrown? Pretty sure I'm confusing myself because of the way this is laid out.viralshah
02/14/2020, 4:28 AMnotes
array in the following json object?
val msg = json { obj( "a" to "b", "notes" to array() ) }
Andrew Giorgio
02/17/2020, 8:17 PMUnable to instantiate Rectangle with parameters []
as an error. As far as I can tell, it expects the width and height fields to exist on the Shape class itself. Does anyone know why this might be?cedric
02/17/2020, 8:21 PMAndrew Giorgio
02/17/2020, 8:23 PM@TypeFor(field = "type", adapter = ShapeTypeAdapter::class)
open class Shape(val type: String)
data class Rectangle(val width: Int, val height: Int) : Shape("rectangle")
data class Circle(val radius: Int) : Shape("circle")
class ShapeTypeAdapter : TypeAdapter<Shape> {
override fun classFor(type: Any): KClass<out Shape> = when (type as String) {
"rectangle" -> Rectangle::class
"circle" -> Circle::class
else -> throw IllegalArgumentException("Unknown type: $type")
}
}
and then I call the following (in a test), which gives me the error.
val result = Klaxon().parseArray<Shape>(
"""
[
{ "type": "rectangle", "width": 100, "height": 50 },
{ "type": "circle", "radius": 20}
]
"""
)
Andrew Giorgio
02/17/2020, 8:24 PMcedric
02/18/2020, 12:40 AMprintln(result)
at the end and I get:
[Rectangle(width=100, height=50), Circle(radius=20)]
Andrew Giorgio
02/21/2020, 12:06 AMimport com.beust.klaxon.TypeAdapter
import com.beust.klaxon.TypeFor
import kotlin.reflect.KClass
@TypeFor(field = "type", adapter = ShapeTypeAdapter::class)
open class Shape(val type: String) {
val width: Int = -1
val height: Int = -1
val radius: Int = -1
}
class Rectangle(width: Int, height: Int) : Shape("rectangle")
class Circle(radius: Int) : Shape("circle")
class ShapeTypeAdapter : TypeAdapter<Shape> {
override fun classFor(type: Any): KClass<out Shape> = when (type as String) {
"rectangle" -> Rectangle::class
"circle" -> Circle::class
else -> throw IllegalArgumentException("Unknown type: $type")
}
}
This allows the parse to happen without errors. However, the results are unexpected. I wrote the following test, and put the results of the print statements in comments on the same line:
val result = Klaxon().parseArray<Shape>(
"""
[
{ "type": "rectangle", "width": 25, "height": 75},
{ "type": "circle", "radius": 20}
]
"""
)
println(result?.get(0)?.width) //Prints 25
println(result?.get(0)?.height)//Prints 75
However, based on my understanding of how this code should work, the actual width and height parameters that were read from the JSON string should be unused, and the print statements should both output -1. Especially as the width and height properties on Shape are neither mutable nor open.Gunslingor
04/03/2020, 5:04 PMKlaxon().toJsonString(Reports.get(reportID!!), prettyPrint = true)
Gunslingor
04/04/2020, 11:46 PMcedric
04/05/2020, 12:02 AMmathew murphy
05/01/2020, 4:06 PMException in thread "pool-1-thread-1" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:996)
at com.beust.klaxon.World.foundValue$klaxon(World.kt:109)
at com.beust.klaxon.KlaxonParser$1$7.invoke(KlaxonParser.kt:116)
at com.beust.klaxon.KlaxonParser$1$7.invoke(KlaxonParser.kt:9)
at com.beust.klaxon.StateMachine.next(StateMachine.kt:20)
at com.beust.klaxon.KlaxonParser.fullParseLoop(KlaxonParser.kt:63)
at com.beust.klaxon.KlaxonParser.parse(KlaxonParser.kt:25)
cedric
05/01/2020, 4:08 PMmathew murphy
05/01/2020, 4:11 PMcedric
05/01/2020, 4:11 PMmathew murphy
05/01/2020, 4:14 PMmathew murphy
05/01/2020, 4:15 PMcedric
05/01/2020, 4:15 PMmathew murphy
05/01/2020, 7:23 PMmathew murphy
05/01/2020, 7:43 PM