Search code examples
serializationkotlinxodusxodus-dnq

A proper way to serialize/deserialize Xodus-dnq entity


For example - I've got this entity:

class XdUser(entity: Entity) : XdEntity(entity) {

    var someName by xdStringProp()
    var someNumber by xdIntProp()
}

What is the proper way to serialize/deserialize it from/to json? I have to create data class which just duplicates my entity's fields and then propagate values to XdUser? Or there is other way?


Solution

  • Serializing XdUser to JSON you should be sure that serializer won't process XdUser#entity and other public links which can expose large amount of unnecessary data. Deserialization brings another problems because deserializator should be informed about how to instantiate a class from json using constructor XdUser(entity: Entity).

    From my prospective better choice there is to have another level for rest API. It brings ability to control amount of exposed data, control permissions (if your have them) and the way of how entities will be updated.

    Rest api level be implemented like this:

    open class EntityVO<T: XdEntity>(xdId: String?) {
    }
    
    class UserVO(xdId: String?): EntityVO<XdUser>(xdId: String?) {
    
        var someName by delegateTo(XdUser::someName)
        var someNumber by delegateTo(XdUser::someNumber)
    
    }
    

    and delegateTo should provide delegate which will lookup XdUser by xdId and do get/set value using specified XdUser property. For link/links logic will be more complex but idea can be the same.