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

[JSON string for maps] ToStringBuilder.reflectionToString doesnt render nested maps correctly.

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.10
    • Fix Version/s: 3.11
    • Component/s: lang.*
    • Labels:
      None

      Description

      Nested Maps are not converted correctly as JSON using ToStringBuilder.reflectToString commons-lang3:3.10 . Please find below the example to reproduce:-

      Class:

      public class Student {    
          private String name;
          private Map<String, String> education;
      
          //getters and setters...
      
          public String toString() {
              return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
          }}
      

       

      Driver test:

              Student p = new Student();
              p.setName("como");
              
              Map<String, String> educationMap = new HashMap<String, String>();
              educationMap.put("primary", "school");
              educationMap.put("graduation", "B.S.");
              
              p.setEducation(educationMap);
              
              System.out.println(p.toString());
      

      The toString() prints

      {"education":{graduation=B.S., primary=school},"name":"como"}
      
      

      but I expect as JSON it should print as below (with proper key,value pairs as field names and values)

      {"education":{"graduation": "B.S.", "primary":"school"},"name":"como"}
      
      

      Suggested fix:

      I can create a Pull request for this issue handling Maps appending logic.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              swarajsaaj Swaraj Pal
            • Votes:
              0 Vote for this issue
              Watchers:
              4 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 - 40m
                40m