Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9135997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:53:43+00:00 2026-06-17T08:53:43+00:00

I have following java code @XmlRootElement public class PreferenceEntity { String type, name; int

  • 0

I have following java code

 @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();
        }

    }
}

Output is :

{
-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"
    }
}

The desired output is following:

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

even better If I can get it:

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

I tried using JsonSerialize but it seems not to be working with HashMap. Any pointers will be a great help.

Regards
WhiteLotus

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:53:43+00:00Added an answer on June 17, 2026 at 8:53 am

    Since you want to change the layout of your JSON output, I think you should change the serialization at class level, this should work:

    @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();
                }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following Java code: import java.io.*; class Global{ public static int a =
I have the following Java code: public class A { private int var_a =
I have the following (Java) code: public class TestBlah { private static final String
I have the following Java code: public static void main(String[] args) { new Thread(new
Say suppose I have the following Java code. public class Example { public static
If I have the following Java code: int[][] readAPuzzle() { Scanner input = new
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
I have following class in Java code: public class CHRTreeCreator extends IndexCreator { ...
I have written following java code: public static void main(String[] args) { Vector vector

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.