I’m trying to interface with a typescript library that deals a lot with Json.
They define a lot of interfaces for declaring what the Json must contain which is nice for type safety but it’s causing some problems in Kotlin. When I try to implement the interface and then serialize that object as Json I get mangled names in the Json due to how Kotlin implements properties.
For example, given these declarations:
external interface Foo {
val foo: String
}
class FooImpl : Foo {
override val foo = "foo"
}
Kotlin will generate something like this:
function FooImpl() {
this.foo_new1zk$_0 = 'foo';
}
Object.defineProperty(FooImpl.prototype, 'foo', {
get: function () {
return this.foo_new1zk$_0;
}
});
FooImpl.$metadata$ = {
kind: Kind_CLASS,
simpleName: 'FooImpl',
interfaces: []
};
which leads to this JSON.stringify(Foo())
outputting {"foo_new1zk$_0":"foo"}
rather than the desired {"foo":"foo"}
.
One way to solve it is to use json()
and asDynamic()
but then I lose the type safety which isn’t what I want.
Are there any other solutions to this problem?