Use class name as root key for JSON Jackson serialization

前端 未结 11 2775
长情又很酷
长情又很酷 2020-11-27 18:05

Suppose I have a pojo:

import org.codehaus.jackson.map.*;

public class MyPojo {
    int id;
    public int getId()
    { return this.id; }

    public void          


        
11条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 18:43

    How about simplest possible solution; just use a wrapper class like:

    class Wrapper {
       public MyPojo MyPojo;
    }
    

    and wrapping/unwrapping in your code?

    Beyond this, it would help to know WHY you would like additional json object entry like this? I know this is done by libs that emulate json via xml api (because of impedance between xml and json, due to conversion from xml to json), but for pure json solutions it is usually not needed.

    Is it to allow you do figure out what actual type is? If so, perhaps you could consider enabled polymorphic type information, to let Jackson handle it automatically? (see 1.5 release notes, entry for PTH, for details).

提交回复
热议问题