my situation is that of converting a json string into a bean. using JSONOjbect.toBean(jsonString, myBean.class) works well;
however, one when the json object is stringified one of the fields is set as a Json array.
I'd like to convert that array into a string during the conversion from json string into java bean coz the corresponding bean field is a string.
I thought that something like the following might work (this does compile):
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor("myJsonArrayKey", new JsonArrayValueProcessor());
JSONObject jsonObject = JSONObject.fromObject(myJsonString, jsonConfig);
MyBean c = (MyBean)JSONObject.toBean(jsonObject, MyBean.class);
JsonArrayValueProcessor() implements JsonValueProcessor and is supposed to be the class that converts the array key into a string key; the result of the conversion is held in the intermediary object jsonObject.
is this approach correct?
and, how do I implement the two methods of JsonValueProcessor and their parameters?
any assistance that you might be able to provide would be greatly appreciated.
thank you,
Brian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Brian,
Your assumptions are correct, the code will work provided that you implement processObjectValue(String key, Object value, JsonConfig jsonConfig) with your custom logic. That is the method used to transform a value in an object context, the other is used when in an array context. The way you are registering the JsonValueProcessor will work for any bean that has a property named 'myJsonArrayKey', you may however be more restrictive and register it with
registerJsonValueProcessor(MyBean.class, "myJsonArrayKey", new JsonArrayValueProcessor());
Cheers,
Andres
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi there,
my situation is that of converting a json string into a bean. using JSONOjbect.toBean(jsonString, myBean.class) works well;
however, one when the json object is stringified one of the fields is set as a Json array.
I'd like to convert that array into a string during the conversion from json string into java bean coz the corresponding bean field is a string.
I thought that something like the following might work (this does compile):
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor("myJsonArrayKey", new JsonArrayValueProcessor());
JSONObject jsonObject = JSONObject.fromObject(myJsonString, jsonConfig);
MyBean c = (MyBean)JSONObject.toBean(jsonObject, MyBean.class);
JsonArrayValueProcessor() implements JsonValueProcessor and is supposed to be the class that converts the array key into a string key; the result of the conversion is held in the intermediary object jsonObject.
is this approach correct?
and, how do I implement the two methods of JsonValueProcessor and their parameters?
any assistance that you might be able to provide would be greatly appreciated.
thank you,
Brian
Hi Brian,
Your assumptions are correct, the code will work provided that you implement processObjectValue(String key, Object value, JsonConfig jsonConfig) with your custom logic. That is the method used to transform a value in an object context, the other is used when in an array context. The way you are registering the JsonValueProcessor will work for any bean that has a property named 'myJsonArrayKey', you may however be more restrictive and register it with
registerJsonValueProcessor(MyBean.class, "myJsonArrayKey", new JsonArrayValueProcessor());
Cheers,
Andres