Skip to content

Commit 68636eb

Browse files
committed
[Fix #840] Allow user to set ObjectMapper before JsonUtils is init
Signed-off-by: fjtirado <[email protected]>
1 parent 666cdb3 commit 68636eb

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

impl/jackson/src/main/java/io/serverlessworkflow/impl/jackson/JsonUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
public class JsonUtils {
5656

57-
private static ObjectMapper mapper = new ObjectMapper();
57+
private static ObjectMapper mapper = ObjectMapperFactoryProvider.instance().get().get();
5858

5959
public static ObjectMapper mapper() {
6060
return mapper;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.jackson;
17+
18+
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import java.util.function.Supplier;
20+
21+
public interface ObjectMapperFactory extends Supplier<ObjectMapper> {}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.jackson;
17+
18+
import com.fasterxml.jackson.databind.ObjectMapper;
19+
20+
import java.util.Objects;
21+
import java.util.ServiceLoader;
22+
import java.util.function.Supplier;
23+
24+
public class ObjectMapperFactoryProvider implements Supplier<ObjectMapperFactory> {
25+
26+
private static ObjectMapperFactoryProvider instance = new ObjectMapperFactoryProvider();
27+
28+
public static ObjectMapperFactoryProvider instance() {
29+
return instance;
30+
}
31+
32+
private ObjectMapperFactory objectMapperFactory;
33+
34+
private ObjectMapperFactoryProvider() {}
35+
36+
public void setFactory(ObjectMapperFactory objectMapperFactory) {
37+
this.objectMapperFactory = Objects.requireNonNull(objectMapperFactory);
38+
}
39+
40+
@Override
41+
public ObjectMapperFactory get() {
42+
return objectMapperFactory != null
43+
? objectMapperFactory
44+
: ServiceLoader.load(ObjectMapperFactory.class)
45+
.findFirst()
46+
.orElseGet(() -> () -> new ObjectMapper().findAndRegisterModules());
47+
}
48+
}

0 commit comments

Comments
 (0)