ch0ice opened a new pull request #434: Fixed a problem when parsing Chinese 
using the ToStringStyle.JSON_STYLE
URL: https://github.com/apache/commons-lang/pull/434
 
 
   When I write the following code
   ```java
   //org.apache.commons.lang3.builder.ReflectionToStringBuilderTest
   public static class TestJsonStyle{
           private String a;
           public void setA(String a){
               this.a = a;
           }
           @Override
           public String toString() {
               return ReflectionToStringBuilder.toString(this, 
ToStringStyle.JSON_STYLE);
           }
       }
   
       @Test
       public void testJsonStyle() {
           TestJsonStyle jsonStyle = new TestJsonStyle();
           jsonStyle.setA("测试");
           Assertions.assertEquals("{\"a\":\"测试\"}",jsonStyle.toString());
       }
   ```
   You get the following result,I don't think this is correct
   ```java
   {"a":"\u6D4B\u8BD5"}
   ```
   
   When I change the following code, the result works fine
   ```java
   //org.apache.commons.lang3.builder.ToStringStyle
   private void appendValueAsString(final StringBuffer buffer, final String 
value) {
   //            
buffer.append('"').append(StringEscapeUtils.escapeJson(value)).append('"');
               buffer.append('"').append(value).append('"');
   }
   //{"a":"测试"}
   ```
   
   But when I did the unit test, I found out
   An error occurred on this unit test
   When I looked at the test case, I thought there was something wrong with the 
test case itself
   ```java
   //org.apache.commons.lang3.builder.JsonToStringStyleTest
   @Test
       public void testLANG1395() {
   
   //        assertEquals("{\"name\":\"value\"}", new 
ToStringBuilder(base).append("name", "value").toString());
   //        assertEquals("{\"name\":\"\"}", new 
ToStringBuilder(base).append("name", "").toString());
   //        assertEquals("{\"name\":\"\\\"\"}", new 
ToStringBuilder(base).append("name", '"').toString());
   //        assertEquals("{\"name\":\"\\\\\"}", new 
ToStringBuilder(base).append("name", '\\').toString());
   //        assertEquals("{\"name\":\"Let's \\\"quote\\\" this\"}", new 
ToStringBuilder(base).append("name", "Let's \"quote\" this").toString());
   
           assertEquals("{\"name\":\"value\"}", new 
ToStringBuilder(base).append("name", "value").toString());
           assertEquals("{\"name\":\"\"}", new 
ToStringBuilder(base).append("name", "").toString());
           assertEquals("{\"name\":\"\"\"}", new 
ToStringBuilder(base).append("name", '"').toString());
           assertEquals("{\"name\":\"\\\"}", new 
ToStringBuilder(base).append("name", '\\').toString());
           assertEquals("{\"name\":\"Let's \"quote\" this\"}", new 
ToStringBuilder(base).append("name", "Let's \"quote\" this").toString());
   
       }
   ```
   
   Or when I write it this way
   ```java
   System.out.println(new ToStringBuilder(base).append("name", '"').toString());
   //r1(Before fix)      {"name":"\""}
   //r2(After fix)      {"name":"""} 
   ```
   I think the right result is r2 instead of r1

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to