Skip to content

Commit af14a2e

Browse files
mdeinumfmbenhassine
authored andcommitted
Provide constructors for injection
Currently when reading/writing JSON and using the reader/marshaller provided and you want a custom Gson or ObjectMapper instance it still creates the not needed instance. Move the construction to a constructor and provide a constructor to directly pass in the pre-configured Gson or ObjectMapper instance. The same approach is used in Spring itself where Gson or ObjectMapper instances can be passed in.
1 parent f225df8 commit af14a2e

File tree

6 files changed

+39
-11
lines changed

6 files changed

+39
-11
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/GsonJsonObjectMarshaller.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@ public class GsonJsonObjectMarshaller<T> implements JsonObjectMarshaller<T> {
3030

3131
private Gson gson = new Gson();
3232

33+
public GsonJsonObjectMarshaller() {
34+
this.gson=new Gson();
35+
}
36+
37+
public GsonJsonObjectMarshaller(Gson gson) {
38+
this.gson=gson;
39+
}
40+
3341
/**
3442
* Set the {@link Gson} object to use.
3543
* @param gson object to use
44+
* @see #GsonJsonObjectMarshaller(Gson)
3645
*/
3746
public void setGson(Gson gson) {
3847
this.gson = gson;

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/GsonJsonObjectReader.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
*/
4343
public class GsonJsonObjectReader<T> implements JsonObjectReader<T> {
4444

45-
private Class<? extends T> itemType;
45+
private final Class<? extends T> itemType;
4646

4747
private JsonReader jsonReader;
4848

49-
private Gson mapper = new Gson();
49+
private Gson mapper;
5050

5151
private InputStream inputStream;
5252

@@ -55,12 +55,19 @@ public class GsonJsonObjectReader<T> implements JsonObjectReader<T> {
5555
* @param itemType the target item type
5656
*/
5757
public GsonJsonObjectReader(Class<? extends T> itemType) {
58+
this.mapper = new Gson();
59+
this.itemType = itemType;
60+
}
61+
62+
public GsonJsonObjectReader(Gson mapper, Class<? extends T> itemType) {
63+
this.mapper = mapper;
5864
this.itemType = itemType;
5965
}
6066

6167
/**
6268
* Set the object mapper to use to map Json objects to domain objects.
6369
* @param mapper the object mapper to use
70+
* @see #GsonJsonObjectReader(Gson, Class)
6471
*/
6572
public void setMapper(Gson mapper) {
6673
Assert.notNull(mapper, "The mapper must not be null");

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/JacksonJsonObjectMarshaller.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,20 @@
3131
*/
3232
public class JacksonJsonObjectMarshaller<T> implements JsonObjectMarshaller<T> {
3333

34-
private ObjectMapper objectMapper = new ObjectMapper();
34+
private ObjectMapper objectMapper;
35+
36+
public JacksonJsonObjectMarshaller() {
37+
this.objectMapper = new ObjectMapper();
38+
}
39+
40+
public JacksonJsonObjectMarshaller(ObjectMapper objectMapper) {
41+
this.objectMapper=objectMapper;
42+
}
3543

3644
/**
3745
* Set the {@link ObjectMapper} to use.
3846
* @param objectMapper to use
47+
* @see #JacksonJsonObjectMarshaller(ObjectMapper)
3948
*/
4049
public void setObjectMapper(ObjectMapper objectMapper) {
4150
this.objectMapper = objectMapper;

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/JacksonJsonObjectReader.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
*/
4040
public class JacksonJsonObjectReader<T> implements JsonObjectReader<T> {
4141

42-
private Class<? extends T> itemType;
42+
private final Class<? extends T> itemType;
4343

4444
private JsonParser jsonParser;
4545

46-
private ObjectMapper mapper = new ObjectMapper();
46+
private ObjectMapper mapper;
4747

4848
private InputStream inputStream;
4949

@@ -52,12 +52,19 @@ public class JacksonJsonObjectReader<T> implements JsonObjectReader<T> {
5252
* @param itemType the target item type
5353
*/
5454
public JacksonJsonObjectReader(Class<? extends T> itemType) {
55+
this.mapper=new ObjectMapper();
5556
this.itemType = itemType;
5657
}
5758

59+
public JacksonJsonObjectReader(ObjectMapper mapper, Class<? extends T> itemType) {
60+
this.mapper= mapper;
61+
this.itemType=itemType;
62+
}
63+
5864
/**
5965
* Set the object mapper to use to map Json objects to domain objects.
6066
* @param mapper the object mapper to use
67+
* @see #JacksonJsonObjectReader(ObjectMapper, Class)
6168
*/
6269
public void setMapper(ObjectMapper mapper) {
6370
Assert.notNull(mapper, "The mapper must not be null");

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/GsonJsonFileItemWriterFunctionalTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ protected JsonObjectMarshaller<Trade> getJsonObjectMarshaller() {
3434
@Override
3535
protected JsonObjectMarshaller<Trade> getJsonObjectMarshallerWithPrettyPrint() {
3636
Gson gson = new GsonBuilder().setPrettyPrinting().create();
37-
GsonJsonObjectMarshaller<Trade> jsonObjectMarshaller = new GsonJsonObjectMarshaller<>();
38-
jsonObjectMarshaller.setGson(gson);
39-
return jsonObjectMarshaller;
37+
return new GsonJsonObjectMarshaller<>(gson);
4038
}
4139

4240
@Override

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/JacksonJsonFileItemWriterFunctionalTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ protected JsonObjectMarshaller<Trade> getJsonObjectMarshaller() {
3535
protected JsonObjectMarshaller<Trade> getJsonObjectMarshallerWithPrettyPrint() {
3636
ObjectMapper objectMapper = new ObjectMapper();
3737
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
38-
JacksonJsonObjectMarshaller<Trade> jsonObjectMarshaller = new JacksonJsonObjectMarshaller<>();
39-
jsonObjectMarshaller.setObjectMapper(objectMapper);
40-
return jsonObjectMarshaller;
38+
return new JacksonJsonObjectMarshaller<>(objectMapper);
4139
}
4240

4341
@Override

0 commit comments

Comments
 (0)