badba.blogg.se

Kotlin serializable data class
Kotlin serializable data class










dynamically build hierarchical json definitions That seems valuable to me… I’d like to be able to dynamically accumulate json structure into some kind of JsonSpec and then be able to serialize/deserialize from that. It sounds like what wants is to be able to construct JSON (de)serializers at runtime. Actually you can’t work with dynamic types in dynamic typed languages - they just decorate hierarchical structures which consist of primitive types for you. And yes, you can’t work with dynamic types in static typed languages. When you have already known static typed json payload - you just define your dataclass and produce it’s instance from payload using Gson|Jackson. If you have more complex case you can use abstract class|interface and produce it’s children for different node types. I define status array in my target type and use adapter which read integer or array from json field and produces array always. For example i have “status” field in server payload which can be array or integer. They gives you “adapters” for serializing|deserializing specified nodes to minify brain damage from case-sensitive node types. Or you can use one of mapping solutions like Gson|Jackson. For example JsonObject tree which is from-the-box solution in jdk. Standard way to map dynamic (badly engeneered) json|xml api payload is to build hierarchical structure. Even using reflection you manipulate already resolved types. But what will you do with those instances out of this script? Every type in your ‘native’ code must be resolved in compile-time, for casting you also use resolved types. For example, you can generate&run kotlin script|java script|python etc from your ‘native’ code and in it you can define new type and produce its instances.












Kotlin serializable data class