diff options
author | Gary Gregory <garydgregory@gmail.com> | 2020-06-24 09:26:11 -0400 |
---|---|---|
committer | Gary Gregory <garydgregory@gmail.com> | 2020-06-24 09:26:11 -0400 |
commit | 1dddec8ba867bc31233ba194f0753ea35818cbfd (patch) | |
tree | 467e0bd94929502fa4af487dcea0b6969a1e2637 /src/main/java/org/apache/commons/lang3/builder | |
parent | deb5f817af5a310512660d0b9f5fcf1d55f088a5 (diff) | |
download | apache-commons-lang-1dddec8ba867bc31233ba194f0753ea35818cbfd.tar.gz |
[LANG-1542] ToStringBuilder.reflectionToString - Wrong JSON format when
object has a List/Array of Enum.
Diffstat (limited to 'src/main/java/org/apache/commons/lang3/builder')
-rw-r--r-- | src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java | 51 |
1 files changed, 33 insertions, 18 deletions
diff --git a/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java b/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java index 9b115739b..4856c32c3 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java +++ b/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java @@ -624,6 +624,16 @@ public abstract class ToStringStyle implements Serializable { * {@code toString}, not {@code null} */ protected void appendDetail(final StringBuffer buffer, final String fieldName, final Collection<?> coll) { + if (coll != null && !coll.isEmpty()) { + buffer.append(arrayStart); + int i = 0; + for (Object item : coll) { + appendDetail(buffer, fieldName, i++, item); + } + buffer.append(arrayEnd); + return; + } + buffer.append(coll); } @@ -919,20 +929,33 @@ public abstract class ToStringStyle implements Serializable { buffer.append(arrayStart); for (int i = 0; i < array.length; i++) { final Object item = array[i]; - if (i > 0) { - buffer.append(arraySeparator); - } - if (item == null) { - appendNullText(buffer, fieldName); - - } else { - appendInternal(buffer, fieldName, item, arrayContentDetail); - } + appendDetail(buffer, fieldName, i, item); } buffer.append(arrayEnd); } /** + * <p>Append to the {@code toString} the detail of an + * {@code Object} array item.</p> + * + * @param buffer the {@code StringBuffer} to populate + * @param fieldName the field name, typically not used as already appended + * @param i the array item index to add + * @param item the array item to add + * @since 3.11 + */ + protected void appendDetail(final StringBuffer buffer, final String fieldName, int i, final Object item) { + if (i > 0) { + buffer.append(arraySeparator); + } + if (item == null) { + appendNullText(buffer, fieldName); + } else { + appendInternal(buffer, fieldName, item, arrayContentDetail); + } + } + + /** * <p>Append to the {@code toString} the detail of an array type.</p> * * @param buffer the {@code StringBuffer} to populate @@ -946,15 +969,7 @@ public abstract class ToStringStyle implements Serializable { final int length = Array.getLength(array); for (int i = 0; i < length; i++) { final Object item = Array.get(array, i); - if (i > 0) { - buffer.append(arraySeparator); - } - if (item == null) { - appendNullText(buffer, fieldName); - - } else { - appendInternal(buffer, fieldName, item, arrayContentDetail); - } + appendDetail(buffer, fieldName, i, item); } buffer.append(arrayEnd); } |