Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ public class GsonJsonObjectMarshaller<T> implements JsonObjectMarshaller<T> {

private Gson gson = new Gson();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the improvement suggested in this PR, there shouldn't be an inline instantiation of Gson here (similar to what has been done in other classes in this change set). I will update that on merge.


public GsonJsonObjectMarshaller() {
this.gson=new Gson();
}

public GsonJsonObjectMarshaller(Gson gson) {
this.gson=gson;
}

/**
* Set the {@link Gson} object to use.
* @param gson object to use
* @see #GsonJsonObjectMarshaller(Gson)
*/
public void setGson(Gson gson) {
this.gson = gson;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
*/
public class GsonJsonObjectReader<T> implements JsonObjectReader<T> {

private Class<? extends T> itemType;
private final Class<? extends T> itemType;

private JsonReader jsonReader;

private Gson mapper = new Gson();
private Gson mapper;

private InputStream inputStream;

Expand All @@ -55,12 +55,19 @@ public class GsonJsonObjectReader<T> implements JsonObjectReader<T> {
* @param itemType the target item type
*/
public GsonJsonObjectReader(Class<? extends T> itemType) {
this.mapper = new Gson();
this.itemType = itemType;
}

public GsonJsonObjectReader(Gson mapper, Class<? extends T> itemType) {
this.mapper = mapper;
this.itemType = itemType;
}

/**
* Set the object mapper to use to map Json objects to domain objects.
* @param mapper the object mapper to use
* @see #GsonJsonObjectReader(Gson, Class)
*/
public void setMapper(Gson mapper) {
Assert.notNull(mapper, "The mapper must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@
*/
public class JacksonJsonObjectMarshaller<T> implements JsonObjectMarshaller<T> {

private ObjectMapper objectMapper = new ObjectMapper();
private ObjectMapper objectMapper;

public JacksonJsonObjectMarshaller() {
this.objectMapper = new ObjectMapper();
}

public JacksonJsonObjectMarshaller(ObjectMapper objectMapper) {
this.objectMapper=objectMapper;
}

/**
* Set the {@link ObjectMapper} to use.
* @param objectMapper to use
* @see #JacksonJsonObjectMarshaller(ObjectMapper)
*/
public void setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
*/
public class JacksonJsonObjectReader<T> implements JsonObjectReader<T> {

private Class<? extends T> itemType;
private final Class<? extends T> itemType;

private JsonParser jsonParser;

private ObjectMapper mapper = new ObjectMapper();
private ObjectMapper mapper;

private InputStream inputStream;

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

public JacksonJsonObjectReader(ObjectMapper mapper, Class<? extends T> itemType) {
this.mapper= mapper;
this.itemType=itemType;
}

/**
* Set the object mapper to use to map Json objects to domain objects.
* @param mapper the object mapper to use
* @see #JacksonJsonObjectReader(ObjectMapper, Class)
*/
public void setMapper(ObjectMapper mapper) {
Assert.notNull(mapper, "The mapper must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ protected JsonObjectMarshaller<Trade> getJsonObjectMarshaller() {
@Override
protected JsonObjectMarshaller<Trade> getJsonObjectMarshallerWithPrettyPrint() {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
GsonJsonObjectMarshaller<Trade> jsonObjectMarshaller = new GsonJsonObjectMarshaller<>();
jsonObjectMarshaller.setGson(gson);
return jsonObjectMarshaller;
return new GsonJsonObjectMarshaller<>(gson);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ protected JsonObjectMarshaller<Trade> getJsonObjectMarshaller() {
protected JsonObjectMarshaller<Trade> getJsonObjectMarshallerWithPrettyPrint() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
JacksonJsonObjectMarshaller<Trade> jsonObjectMarshaller = new JacksonJsonObjectMarshaller<>();
jsonObjectMarshaller.setObjectMapper(objectMapper);
return jsonObjectMarshaller;
return new JacksonJsonObjectMarshaller<>(objectMapper);
}

@Override
Expand Down