使用HashMap的JsonSerialize

时间:2013-01-09 02:00:07

标签: java json jaxb

我有以下java代码

 @XmlRootElement
public class PreferenceEntity  {
    String type, name;
    int id;
    Map<String, String> attributes = new HashMap<String, String>();
    List<PreferenceEntity> children;
    PreferenceEntity parent;

    public PreferenceEntity() {
        parent = null;
    }

    public PreferenceEntity(PreferenceEntity parent) {
        this.parent = parent;
    }

    @XmlAnyElement
    public List<PreferenceEntity> getChildren() {
        return children;
    }
    public void setChildren(List<PreferenceEntity> children) {
        this.children = children;
    }

    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }

    @JsonSerialize(contentUsing=SubjectAutocompleteSerializer.class)
    public Map<String, String> getAttributes() {
        return attributes;
    }

    @XmlTransient
    public PreferenceEntity getParent() {
        return parent;
    }


    public void setName(String name) {
        this.name = name;
    }

    public void setId(int id) { 
        if (id >= 0) {
            this.id = id;
        }
    }

    // Internal class
    public static class SubjectAutocompleteSerializer extends JsonSerializer<Map<String, String>> {

        @Override
        public void serialize(Map<String, String> map, JsonGenerator jgen, SerializerProvider provider)
                throws IOException, JsonProcessingException {
            jgen.writeStartObject();
            for (String key:map.keySet()) {
                jgen.writeStringField(key, map.get(key));
            }
            //jgen.writeFieldName(value.getType());
            //jgen.writeStringField(value.getType(), value.getType());
            // jgen.writeStartObject();
            //jgen.writeObject(value);
            //jgen.writeStartObject("XMY");
            //jgen.writeStringField("value", value.getName());
            // jgen.writeEndObject();
            jgen.writeEndObject();
        }

    }
}

输出是:

{
-preferences : [
-{
-children : [
    -{
    children : null
    type : "A1"
    -attributes : {
    ColumnId : "86"
    }
    }
    -{
    children : null
    type : "A2"
    -attributes : {
    ColumnId : "87"
    }
    }
    -{
    children : null
    type : "A1"
    -attributes : {
    ColumnId : "382"
    Selected : "true"
    }
}

所需的输出如下:

{
-preferences : [
-{
    -children : [
    -{
        children : null
        type : "A1"
        ColumnId : "86"
    }
    -{
        children : null
        type : "A2"
        ColumnId : "87"
    }
    -{
        children : null
        type : "A1"
        ColumnId : "382"
        Selected : "true"
    }
}

更好如果我能得到它:

{
-preferences : [
-{
    -A1 : [
    -{
        children : null
        type : "A1"
        ColumnId : "86"
    }
    -A2{
        children : null
        ColumnId : "87"
    }
    -A3{
        children : null
        ColumnId : "382"
        Selected : "true"
    }
}

我尝试使用JsonSerialize但似乎没有使用HashMap。任何指针都会有很大的帮助。

此致 WhiteLotus

1 个答案:

答案 0 :(得分:0)

由于您想要更改JSON输出的布局,我认为您应该在类级别更改序列化,这应该有效:

@JsonSerialize(using = SubjectAutocompleteSerializer.class)
public class PreferenceEntity {
    ...
        public static class SubjectAutocompleteSerializer extends JsonSerializer<PreferenceEntity> {

            public void serialize(PreferenceEntity value, JsonGenerator jgen,
                          SerializerProvider provider) throws IOException,
                JsonProcessingException {

                jgen.writeStartObject();
                if (value.name != null && !value.name.isEmpty()) {
                    jgen.writeStringField("name", value.name);
                }
                if (value.getType() != null && !value.getType().isEmpty()) {
                    jgen.writeStringField("type", value.getType());
                }
                for (Entry<String, String> entry : value.getAttributes().entrySet()) {
                    jgen.writeStringField(entry.getKey(), entry.getValue());
                }
                jgen.writeObjectField("children", value.getChildren());

                jgen.writeEndObject();
            }
    }
}