Skip to content

Commit 1578c73

Browse files
authored
[Fix #840] Allow user to set ObjectMapper before JsonUtils is init (#846)
Signed-off-by: fjtirado <[email protected]>
1 parent 4e02ea4 commit 1578c73

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.Objects;
20+
import java.util.ServiceLoader;
21+
import java.util.function.Supplier;
22+
23+
public class ObjectMapperFactoryProvider implements Supplier<ObjectMapperFactory> {
24+
25+
private static ObjectMapperFactoryProvider instance = new ObjectMapperFactoryProvider();
26+
27+
public static ObjectMapperFactoryProvider instance() {
28+
return instance;
29+
}
30+
31+
private ObjectMapperFactory objectMapperFactory;
32+
33+
private ObjectMapperFactoryProvider() {}
34+
35+
public void setFactory(ObjectMapperFactory objectMapperFactory) {
36+
this.objectMapperFactory = Objects.requireNonNull(objectMapperFactory);
37+
}
38+
39+
@Override
40+
public ObjectMapperFactory get() {
41+
return objectMapperFactory != null
42+
? objectMapperFactory
43+
: ServiceLoader.load(ObjectMapperFactory.class)
44+
.findFirst()
45+
.orElseGet(() -> () -> new ObjectMapper().findAndRegisterModules());
46+
}
47+
}

0 commit comments

Comments
 (0)