Use class name as root key for JSON Jackson serialization

前端 未结 11 2701
长情又很酷
长情又很酷 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:34

    Below is a way to achieve this

    Map<String, MyPojo> singletonMap = Collections.singletonMap("mypojo", mp);
    System.out.println(mapper.writeValueAsString(singletonMap));
    

    Output { "mypojo" : { "id" : 4}}

    Here the advantage is that we can give our on name for the root key of json object. By the above code, mypojo will be the root key. This approach will be most useful when we use java script template like Mustache.js for iteration of json objects

    0 讨论(0)
  • 2020-11-27 18:35

    By adding the jackson annotation @JsonTypeInfo in class level you can have the expected output. i just added no-changes in your class.

    package com.test.jackson;
    
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.SerializationConfig;
    
    import com.fasterxml.jackson.annotation.JsonTypeInfo;
    import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
    import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
    
    @JsonTypeInfo(include=As.WRAPPER_OBJECT, use=Id.NAME)
    public class MyPojo {
        // Remain same as you have
    }
    

    output:

    {
        "MyPojo": {
            "id": 4
        }
    }
    
    0 讨论(0)
  • 2020-11-27 18:36

    I'm not using jackson, but searching I found this configuration that seems to be what you want: WRAP_ROOT_VALUE

    Feature that can be enabled to make root value (usually JSON Object but can be any type) wrapped within a single property JSON object, where key as the "root name", as determined by annotation introspector (esp. for JAXB that uses @XmlRootElement.name) or fallback (non-qualified class name). Feature is mostly intended for JAXB compatibility.

    Default setting is false, meaning root value is not wrapped.

    So that you can configure mapper:

    objectMapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
    

    I hope it helps you...

    0 讨论(0)
  • 2020-11-27 18:43

    To achieve this you need to use the JsonTypeInfo annotation on your class and in particular WRAPPER_OBJECT

    @JsonTypeName("foo")                                                                                         
    @JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT ,use = JsonTypeInfo.Id.NAME)
    
    public class Bar(){
    )
    
    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题