0

How can i serilize the following class to get value like { "params":[[],[],1,2....]}. Any help would be appreciated.

    public class Search{
public List<Double> minLatLng=new ArrayList<Double>();
    public List<Double> maxLatLng=new ArrayList<Double>(); //These are bounding co-ordinates
        public List<Integer> ids=new ArrayList<Integer>();
        public List<Integer> owner=new ArrayList<Integer>();
        public float minScore;
        public float maxScore;
        public long minLength;
        public long maxLength;
        public String flag;
        public String language;
        public String keywords;
        public List<Integer> groupIds=new ArrayList<Integer>();
        public List<Integer> characteristic=new ArrayList<Integer>();
        public List<Integer> theme=new ArrayList<Integer>();
        public boolean acitivity;
        public List<Integer> activityOwner=new ArrayList<Integer>();
        public boolean event;
        public List<Integer> eventOwner=new ArrayList<Integer>();
        public List<Integer> favouriteUser=new ArrayList<Integer>();
    }

    The out put should be look like { "params":[[],[],1,2....]} how can i use jackson for getting this value

1 Answer 1

1

You can add a method annotated with @JsonValue annotation to your class which would get the field values from the reflection and return them in a form of array map.

Here is an example:

public class JacksonBeanToArray {
    public static class Search {
        public List<Double> minLatLng=new ArrayList<Double>();
        public List<Double> maxLatLng=new ArrayList<Double>(); //These are bounding co-ordinates
        public List<Integer> ids=new ArrayList<Integer>();
        public List<Integer> owner=new ArrayList<Integer>();
        public float minScore;
        public float maxScore;
        public long minLength;
        public long maxLength;
        public String flag;
        public String language;
        public String keywords;
        public List<Integer> groupIds=new ArrayList<Integer>();
        public List<Integer> characteristic=new ArrayList<Integer>();
        public List<Integer> theme=new ArrayList<Integer>();
        public boolean acitivity;
        public List<Integer> activityOwner=new ArrayList<Integer>();
        public boolean event;
        public List<Integer> eventOwner=new ArrayList<Integer>();
        public List<Integer> favouriteUser=new ArrayList<Integer>();

        @JsonValue
        public Map<String, Object[]> getAsParamMap() throws IllegalAccessException {
            Object[] result = new Object[getClass().getFields().length];
            for (int i = 0; i < result.length; i++) {
                result[i] = getClass().getFields()[i].get(this);
            }
            return Collections.singletonMap("params", result);
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        Search search = new Search();
        search.acitivity = true;
        search.maxLatLng = Arrays.asList(0.44, 5.);
        search.maxScore = 4;
        search.language = "EN";
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(search));
    }

}

Output:

{
  "params" : [ [ ], [ 0.44, 5.0 ], [ ], [ ], 0.0, 4.0, 0, 0, null, "EN", null, [ ], [ ], [ ], true, [ ], false, [ ], [ ] ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.