2

In an Struts 2 project, we need to serialize and deserialize objects, as our requirement is very simple, we decide to use Struts 2 JSONUtil instead of gson.

import org.apache.struts2.json;

String json = JSONUtil.serialize(myAccountVO);
// return: {"accountNumber":"0105069413007","amount":"1500","balance":"215000"}

For deserialization, we face the class cast exception

    AccountVO vo =(AccountVO) JSONUtil.deserialize(json);
    //Exception

I find that the deserialization returns a map with key value of object properties. So I must do as:

HashMap<String,String> map = (HashMap) JSONUtil.deserialize(string)
accountVo.setAccountNumber(map.get("accountNumber"));
....

Well can I do it better or I am expecting too much from this utility.

1 Answer 1

2

After you have deserialized JSON, you can use JSONPopulator to populate bean properties from a map. E.g.

JSONPopulator populator = new JSONPopulator();
AccountVO vo = new AccountVO();
populator.populateObject(vo, map);
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.