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

Allow for multiple JSON providers #354

Open
bergtwvd opened this issue Jan 25, 2022 · 3 comments
Open

Allow for multiple JSON providers #354

bergtwvd opened this issue Jan 25, 2022 · 3 comments

Comments

@bergtwvd
Copy link

bergtwvd commented Jan 25, 2022

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
Copy link
Member

jbescos commented Jun 8, 2022

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
Copy link
Member

jbescos commented Jun 8, 2022

Could you check if this is what you wanted, please?:
#383

@bergtwvd
Copy link
Author

bergtwvd commented Jun 8, 2022

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

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