Allow for multiple JSON providers
bergtwvd opened this issue · 3 comments
In some cases there may be multiple providers, for instance one that comes with the Maven dependencies, and one that I explicitly want to use. To allow the loading of a specific provider, JsonProvider
should have a getName()
method that returns the name of the provider. The name can be use to select a specific provider.
If you want a different implementation of provider you can do it with System property: -Djakarta.json.provider=your.custom.Implementation
The problem of this is that the property will affect everywhere, and maybe you want to use a different implementation in another webapp running in the same JVM process.
You can also make a new instance of your custom implementation, instead of delegating it to the JsonProvider.
To know the name of the loaded implementation you can do the next: JsonProvider.provider().getClass().getName()
Do you mean that we could add a new method in JsonProvider.provider(String name) and then check that argument when iterating the ServiceLoader?:
ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class);
Iterator<JsonProvider> it = loader.iterator();
while (it.hasNext()) {
JsonProvider provider = it.next();
if (provider.getClass().getNAme().equals(name) || name == null) {
return provider;
}
}
....
The proposed edits are fine. This allows to load different providers. Great!