Is it a bug ? ``` interface GeoJson{ val type...
# javascript
g
Is it a bug ?
Copy code
interface GeoJson{
    val type:String
    val coordinates:Array<*>
    
    fun asPoint() = Point(coordinates as Array<Double>)
}
data class Point(val coordinates:Array<Double>)   

fun main(args: Array<String>) {        
    val json = """{"type":"Point", "coordinates":[1.0, 2.0]}"""  
    val obj = JSON.parse<GeoJson>(json).asPoint()        
    println("${obj.coordinates}")
}
https://try.kotlinlang.org/#/UserProjects/2fobglofrrpnr9b0pru299uu31/6d1saqhqrbndev90djqurmuiv0
b
Your linked example runs fine for me
t
I ran into the same problem. For this stuff to work properly use https://github.com/Kotlin/kotlinx.serialization
g
@benleggiero Yes, I changed the code and it works when using an extension function on the interface. Not with the member function. I think it’s a bug. @trathschlag at first I planned to use it. But there is also some limitations on the current version.
👍 1
b
@gaetan so, how was it look originally, what is a bug?
actually, deserialization from json to Kotlin class already somehow covered by kotlinx.serialization since 0.3, see https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/runtime_usage.md#dynamic-object-parser-js-only
g
@bashor I put the code back to the version that is not working. https://try.kotlinlang.org/#/UserProjects/2fobglofrrpnr9b0pru299uu31/6d1saqhqrbndev90djqurmuiv0 The main problem is that there is no error during compilation but at runtime it raises this error:
Copy code
Unhandled JavaScript exception: 
TypeError: JSON.parse(...).asPoint is not a function
I tried to use kotlinx.serialization but it does not cover my use for the moment.
I didn’t find yet the solution for my use case: deserialization of GeoJSON files (https://tools.ietf.org/html/rfc7946). I would like to have a performant multiplatform solution (which mean sharing class/interface). When we are calling
JSON.parse
we must use
external interfaces
that can’t be in a common module. I must test the performance of deserialization with external interfaces, followed by a copy inside common classes. The problem is the size of GeoJSON files which can be big.
b
@gaetan got it, thank you. Could you please crate an issue with your example. Ideally we must write some custom checks for JSON.parse.
about GeoJSON — if kotlinx.serialization (with custom serializations) doesn’t work for you, probably simplest and fastest (?) way is to parse the data to Map<String, Any> and write convertor from this map to your DTOs
second step can be lazy
g
I’m going to make some tests with Map.
b
Thank you! Also let me know result of your tests.
👍 1
g
“is to parse the data to Map<String, Any>“. What do you mean? Map is not an external interface. I can’t parse the text to it. 🤔