jakartaee / jsonp-api

Jakarta JSON Processing
https://eclipse.org/ee4j/jsonp
Other
139 stars 59 forks source link

Allow for multiple JSON providers #354

Open bergtwvd opened 2 years ago

bergtwvd commented 2 years ago

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.

jbescos commented 2 years ago

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;
            }
        }
....
jbescos commented 2 years ago

Could you check if this is what you wanted, please?: https://github.com/eclipse-ee4j/jsonp/pull/383

bergtwvd commented 2 years ago

The proposed edits are fine. This allows to load different providers. Great!