51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

@JsonRawValue 按原样序列化属性

序 {#toc_h2_0}

@JsonRawValue 注解能够按原样序列化属性。属性值不会被转义或者加引号(或者说,会自动去掉转义,多余的引号)。属性值已经是一个 JSON String,或者属性值已经被加了引号时很有用。

例子 {#toc_h2_1}

POJO {#toc_h3_2}

                  public class Report { private long id; private String name; @JsonRawValue private String content; ... }                  

    <svg>
                      <use xlink:href="#vditor-icon-copy" />
                    

    </svg>
                  </span>



  
  
                
                  public class Report {
    private long id;
    private String name;
    @JsonRawValue
    private String content;
    ...
}

                </code>
              </pre>



 
### 序列化 {#toc_h3_3}


 
      
        
                      public class ExampleMain { public static void main(String[] args) throws IOException { Report r = new Report(); r.setId(1); r.setName("Test report"); r.setContent("\"data\""); System.out.println("-- before serialization --"); System.out.println(r); ObjectMapper om = new ObjectMapper(); String jsonString = om.writeValueAsString(r); System.out.println("-- after serialization --"); System.out.println(jsonString); } }                  

        <svg>
                          <use xlink:href="#vditor-icon-copy" />
                        

        </svg>
                      </span>



      
        
                    
                      public class ExampleMain {
        public static void main(String[] args) throws IOException {
            Report r = new Report();
            r.setId(1);
            r.setName("Test report");
            r.setContent("\"data\"");

            System.out.println("-- before serialization --");
            System.out.println(r);

            ObjectMapper om = new ObjectMapper();
            String jsonString = om.writeValueAsString(r);
            System.out.println("-- after serialization --");
            System.out.println(jsonString);
        }




    }


                    </code>
                  </pre>



     
    直接序列化为 ` data ` ,而不是 ` "data" `

     
          
              
                          -- before serialization -- Report{id=1, name='Test report', content='"data"'} -- after serialization -- {"id":1,"name":"Test report","content":"data"}                  

            <svg>
                              <use xlink:href="#vditor-icon-copy" />
                            

            </svg>
                          </span>



          
              
                        
                          -- before serialization --
        Report{id=1, name='Test report', content='"data"'}
        -- after serialization --
        {"id":1,"name":"Test report","content":"data"}

                        </code>
                      </pre>



         
        属性值为 JSON String

         
              
                    
                              public class ExampleMain2 { public static void main(String[] args) throws IOException { Report r = new Report(); r.setId(1); r.setName("Test report"); r.setContent("{\"author\":\"Peter\", \"content\":\"Test content\"}"); System.out.println("-- before serialization --"); System.out.println(r); ObjectMapper om = new ObjectMapper(); String jsonString = om.writeValueAsString(r); System.out.println("-- after serialization --"); System.out.println(jsonString); } }                  

                <svg>
                                  <use xlink:href="#vditor-icon-copy" />
                                

                </svg>
                              </span>



              
                    
                            
                              public class ExampleMain2 {
                public static void main(String[] args) throws IOException {
                    Report r = new Report();
                    r.setId(1);
                    r.setName("Test report");
                    r.setContent("{\"author\":\"Peter\", \"content\":\"Test content\"}");

                    System.out.println("-- before serialization --");
                    System.out.println(r);

                    ObjectMapper om = new ObjectMapper();
                    String jsonString = om.writeValueAsString(r);
                    System.out.println("-- after serialization --");
                    System.out.println(jsonString);
                }




            }


                            </code>
                          </pre>



             
            content 没有以字符串的形式序列化,而是以 JSON 对象的形式序列化

             
                  
                          
                                  -- before serialization -- Report{id=1, name='Test report', content='{"author":"Peter", "content":"Test content"}'} -- after serialization -- {"id":1,"name":"Test report","content":{"author":"Peter", "content":"Test content"}}                  

                    <svg>
                                      <use xlink:href="#vditor-icon-copy" />
                                    

                    </svg>
                                  </span>



                  
                          
                                
                                  -- before serialization --
                Report{id=1, name='Test report', content='{"author":"Peter", "content":"Test content"}'}
                -- after serialization --
                {"id":1,"name":"Test report","content":{"author":"Peter", "content":"Test content"}}

                                </code>
                              </pre>



                 
                不使用 @JsonRawValue {#toc_h2_4}
                -----------------------------


                 
                content 会以字符串的形式序列化

                 
                      
                                
                                      -- before serialization -- Report{id=1, name='Test report', content='{"author":"Peter", "content":"Test content"}'} -- after serialization -- {"id":1,"name":"Test report","content":"{\"author\":\"Peter\", \"content\":\"Test content\"}"}                  

                        <svg>
                                          <use xlink:href="#vditor-icon-copy" />
                                        

                        </svg>
                                      </span>



                      
                                
                                    
                                      -- before serialization --
                    Report{id=1, name='Test report', content='{"author":"Peter", "content":"Test content"}'}
                    -- after serialization --
                    {"id":1,"name":"Test report","content":"{\"author\":\"Peter\", \"content\":\"Test content\"}"}

                                    </code>
                                  </pre>



                     
                    原文链接 
                     https://www.logicbig.com/tutorials/misc/jackson/json-raw-value.html

                     
                                 

                      
                                  
                    *** ** * ** ***

                     标题:@JsonRawValue 按原样序列化属性 
                     作者: https://51tbox.com/ 
                     地址: / 

                      
                                  
                    如未加特殊说明,文章均为原创,转载必须注明出处。均采用 https://creativecommons.org/licenses/by-sa/4.0/deed.zh-Hans !
                      本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。若本站转载文章遗漏了原文链接,请及时告知,我们将做删除处理!文章观点不代表本网站立场,如需处理请联系首页客服。  
                      • 网站转载须在文章起始位置标注作者及原文连接,否则保留追究法律责任的权利。  
                      • 公众号转载请联系网站首页的微信号申请白名单!  

                      
                                  
                    *** ** * ** ***

                      
                     ![]() 

赞(0)
未经允许不得转载:工具盒子 » @JsonRawValue 按原样序列化属性