Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1542

ToStringBuilder.reflectionToString - Wrong JSON format when object has a List/Array of Enum

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 3.9, 3.10
    • 3.11
    • lang.*
    • None
    • Open JDK 1.8

    • Hide
          /**
           * <p>Uses <code>ReflectionToStringBuilder</code> to generate a
           * <code>toString</code> for the specified object.</p>
           *
           * @param object the Object to be output
           * @param style the style of the <code>toString</code> to create, may be <code>null</code>
           * @return the String result
           * @see ReflectionToStringBuilder#toString(Object,ToStringStyle)
           */
          public static String reflectionToString(final Object object, final ToStringStyle style) {
              return ReflectionToStringBuilder.toString(object, style);
          }
      Show
          /**      * <p>Uses <code>ReflectionToStringBuilder</code> to generate a      * <code>toString</code> for the specified object.</p>      *      * @param object the Object to be output      * @param style the style of the <code>toString</code> to create, may be <code>null</code>      * @return the String result      * @see ReflectionToStringBuilder#toString(Object,ToStringStyle)      */     public static String reflectionToString(final Object object, final ToStringStyle style) {         return ReflectionToStringBuilder.toString(object, style);     }

    Description

      I'm trying to log an object to console with JSON style using ToStringBuilder.reflectionToString from commons-lang3:3.10 but it seems generated a wrong JSON format.

      Problem happening when I have a list of enums in my field list.

       

      This is the class:
           public class Person {
           private long id;
           private String name;
           private List<MyEnum> listEnums;
          //getter and setter
          public String toString(){
           return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); 
          }
      }
      

       

      And MyEnum:

       public enum MyEnum {
           FOOD,
           SPORT,
           BOOK,
           MUSIC
       }

      When I call toString() of Person object. It shows like this

       {
           "id": 1,
           "name": "Karl",
           "listEnums": [FRIDAY, MONDAY, TUESDAY]
       }

       

      What I expected is:

       {
           "id": 1,
           "name": "Karl",
           "listEnums": ["FRIDAY", "MONDAY", "TUESDAY"]
       }

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            khoakhoa Trần Ngọc Khoa
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 3.5h
                3.5h