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
Comments
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. |
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 |
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 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. |
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 |
@dualspiral That looks like a messy sollution to something what should be in the library be by default. |
I don’t think it’s particularly messy, but to each their own. We accept PRs if you’d like to do it! |
Ok, will do pr. |
Got no response on the pr. |
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.
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.The text was updated successfully, but these errors were encountered: