Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom (de)serialization for a specific field #125

Closed
NeumimTo opened this issue Dec 2, 2018 · 8 comments
Closed

Custom (de)serialization for a specific field #125

NeumimTo opened this issue Dec 2, 2018 · 8 comments

Comments

@NeumimTo
Copy link

NeumimTo commented Dec 2, 2018

Sometimes we would like to (de)serialize field value to another data structure than the type of a field/"type" in the hocon file, this is currently not possible via configurate.

For example assume we have following simplified case.

class Parent {
     Long id;
     List<Child> childs
}
class Child {
     Long id;
     Parent parent
}

Each Parent and Child i want to serialize in its own file, and in the child files i want to serialize parent only as a foreign key/id, during the loading the child file to pojo i want to lookup some eg.: internal cache with parents and assign its reference to the field "parent". Currently this is not possible, If i want to use keep using objectmapper.

If we look at the gson for example theres the annotation @JsonAdapter https://google.github.io/gson/apidocs/com/google/gson/annotations/JsonAdapter.html which can be used to defined custom logic for a (de)serialization of one or more specific fields withing a class. Having an equavalent in Configururate could be a simple sollution for such cases.

@randombyte-developer
Copy link

Maybe HOCON itself can already do that? Check https://github.com/lightbend/config/blob/master/HOCON.md#includes for loading other hocon files. Then you can reference the loaded nodes https://github.com/lightbend/config/blob/master/HOCON.md#substitutions

Don't know if that's what you want.

@NeumimTo
Copy link
Author

NeumimTo commented Dec 2, 2018

No, not what i want.

For example i could have a list of strings in config, but runtime representation would be a byte mask/hashmap/....

No substitution nor include will solve such cases, but something like gson's @JsonAdapter would

@dualspiral
Copy link
Contributor

Currently this is not possible

I think it is. It honestly sounds like TypeSerializers are what you want here, with a bit of creative thinking. From memory, it's possible by setting a new TypeSerializerCollection on a specific loader that handles the child object. Try this:

Create a custom TypeSerializer, but then only register the custom serialiser to the parent file loader by adding the serialiser to the options for the specific loader. Your custom serialiser could then point to your cache.

Get the options from the parent loader and modify those:

        // for the parent config
        ConfigurationLoader<ConfigurationNode> m = ...;
        ConfigurationOptions co = m.getDefaultOptions();
        TypeSerializerCollection tsc = co.getSerializers().newChild();
        tsc.registerType(TypeToken.of(Child.class), new ChildTokenSerializer());
        co.setSerializers(tsc);
        ConfigurationNode node = m.load(co);
        node.getValue(TypeToken.of(Parent.class)); // this will use the options set above when deserialising/serialising.

For the child loader, load without the child serialiser, that should (in theory) use the ConfigSerializable annotation like normal.

Untested, as I say, this is a memory exercise, but I'd think this should work (or some variant of it).

Your alternative is to create your own object mapper that handles other annotations to do this - I do something like that in Neutrino.

@randombyte-developer
Copy link

Wow, neutrino looks cool!

Btw, I've also written a custom object mapper some time ago (which I already don't use anymore): https://github.com/randombyte-developer/kosp/tree/b34da6252006d47458aed9a0c05ea274e8131314/src/main/kotlin/de/randombyte/kosp/config/objectmapping
Just for you to see another example.

@NeumimTo
Copy link
Author

NeumimTo commented Dec 3, 2018

@dualspiral That looks like a messy sollution to something what should be in the library be by default.

@dualspiral
Copy link
Contributor

I don’t think it’s particularly messy, but to each their own.

We accept PRs if you’d like to do it!

@NeumimTo
Copy link
Author

NeumimTo commented Dec 4, 2018

Ok, will do pr.

@NeumimTo
Copy link
Author

Got no response on the pr.
Closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants