Skip to content

Commit fbc40b5

Browse files
committed
Allow for multiple JSON providers
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
1 parent 7f7ba7e commit fbc40b5

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

api/src/main/java/jakarta/json/spi/JsonProvider.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,47 @@ protected JsonProvider() {
9898
*
9999
* @see ServiceLoader
100100
* @return a JSON provider
101+
*
101102
*/
102103
public static JsonProvider provider() {
104+
return provider(null);
105+
}
106+
107+
/**
108+
* Creates a JSON provider object.
109+
*
110+
* Implementation discovery consists of following steps:
111+
* <ol>
112+
* <li>If the system property {@value #JSONP_PROVIDER_FACTORY} exists,
113+
* then its value is assumed to be the provider factory class.
114+
* This phase of the look up enables per-JVM override of the JsonProvider implementation.</li>
115+
* <li>The provider is loaded using the {@link ServiceLoader#load(Class)} method. When 'providerClassName'
116+
* is not null, it will return the instance having the same class qualified name if exists. This
117+
* is useful when more than one JsonProvider is loaded by the {@link ServiceLoader#load(Class)}.</li>
118+
* <li>If all the steps above fail, then the rest of the look up is unspecified. That said,
119+
* the recommended behavior is to simply look for some hard-coded platform default Jakarta
120+
* JSON Processing implementation. This phase of the look up is so that a platform can have
121+
* its own Jakarta JSON Processing implementation as the last resort.</li>
122+
* </ol>
123+
* Users are recommended to cache the result of this method.
124+
*
125+
* @see ServiceLoader
126+
* @param providerClassName The name of the class to be found from the {@link ServiceLoader#load(Class)}.
127+
* @return a JSON provider
128+
*
129+
* @since 2.1.1
130+
*/
131+
public static JsonProvider provider(String providerClassName) {
103132
if (LazyFactoryLoader.JSON_PROVIDER != null) {
104133
return newInstance(LazyFactoryLoader.JSON_PROVIDER);
105134
}
106135
ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class);
107136
Iterator<JsonProvider> it = loader.iterator();
108-
if (it.hasNext()) {
109-
return it.next();
137+
while (it.hasNext()) {
138+
JsonProvider provider = it.next();
139+
if (providerClassName == null || provider.getClass().getName().equals(providerClassName)) {
140+
return provider;
141+
}
110142
}
111143

112144
// handling OSGi (specific default)

0 commit comments

Comments
 (0)