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 #383

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow for multiple JSON providers
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Jun 8, 2022
commit fbc40b50a0502ebd95b59371f57bbfbc8f5901d4
36 changes: 34 additions & 2 deletions api/src/main/java/jakarta/json/spi/JsonProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,47 @@ protected JsonProvider() {
*
* @see ServiceLoader
* @return a JSON provider
*
*/
public static JsonProvider provider() {
return provider(null);
}

/**
* Creates a JSON provider object.
*
* Implementation discovery consists of following steps:
* <ol>
* <li>If the system property {@value #JSONP_PROVIDER_FACTORY} exists,
* then its value is assumed to be the provider factory class.
* This phase of the look up enables per-JVM override of the JsonProvider implementation.</li>
* <li>The provider is loaded using the {@link ServiceLoader#load(Class)} method. When 'providerClassName'
* is not null, it will return the instance having the same class qualified name if exists. This
* is useful when more than one JsonProvider is loaded by the {@link ServiceLoader#load(Class)}.</li>
* <li>If all the steps above fail, then the rest of the look up is unspecified. That said,
* the recommended behavior is to simply look for some hard-coded platform default Jakarta
* JSON Processing implementation. This phase of the look up is so that a platform can have
* its own Jakarta JSON Processing implementation as the last resort.</li>
* </ol>
* Users are recommended to cache the result of this method.
*
* @see ServiceLoader
* @param providerClassName The name of the class to be found from the {@link ServiceLoader#load(Class)}.
* @return a JSON provider
*
* @since 2.1.1
*/
public static JsonProvider provider(String providerClassName) {
if (LazyFactoryLoader.JSON_PROVIDER != null) {
return newInstance(LazyFactoryLoader.JSON_PROVIDER);
}
ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class);
Iterator<JsonProvider> it = loader.iterator();
if (it.hasNext()) {
return it.next();
while (it.hasNext()) {
JsonProvider provider = it.next();
if (providerClassName == null || provider.getClass().getName().equals(providerClassName)) {
return provider;
}
}

// handling OSGi (specific default)
Expand Down