Skip to content

Commit 3bbacbd

Browse files
committed
Added toJsonString to be used by toString() methods in model classes (#234).
1 parent b3b0867 commit 3bbacbd

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/main/java/org/gitlab4j/api/utils/JacksonJson.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,29 @@ public List<User> deserialize(JsonParser jsonParser, DeserializationContext cont
218218
return (users);
219219
}
220220
}
221+
222+
/**
223+
* This class is used to create a thread-safe singleton instance of JacksonJson customized
224+
* to be used by
225+
*/
226+
private static class JacksonJsonSingletonHelper {
227+
private static final JacksonJson JACKSON_JSON = new JacksonJson();
228+
static {
229+
JACKSON_JSON.objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
230+
JACKSON_JSON.objectMapper.setSerializationInclusion(Include.ALWAYS);
231+
}
232+
}
233+
234+
/**
235+
* Gets a the supplied object output as a formatted JSON string. Null properties will
236+
* result in the value of the property being null. This is meant to be used for
237+
* toString() implementations of GitLab4J classes.
238+
*
239+
* @param <T> the generics type for the provided object
240+
* @param object the object to output as a JSON string
241+
* @return a String containing the JSON for the specified object
242+
*/
243+
public static <T> String toJsonString(final T object) {
244+
return (JacksonJsonSingletonHelper.JACKSON_JSON.marshal(object));
245+
}
221246
}

0 commit comments

Comments
 (0)