Reputation: 154
Are there any custom JSON to java object parsers to map json properties to Java object attributes through some configuration.
suppose I have class
class Person { String id; String name; String loc;}
My json String is
{name:"xy",id:12, address: {location: "abc"}}
I need map location.address
of json to loc
property of Person
. We need to drive this mapping through config XML or prop file.
EDITED....
I am trying to have grammer or rules defined in property file such as:
address.location = loc
id = id
name = name
This is what I am thinking. Not sure how map complex types such as List, Map, etc
So my code would call a method
Person p = ObjectMapper.map(jsonData,Person.class,configuration)
and actual mapping is done is this way
class ObjectMapper {
public static <T> T map(String jsonData,Class<T> rootType, Map<String,String> rules) {
T object = rootType.newInstance();
// TODO: code to parse json using the rules
//and finally return object
return (T) object;
}
This is what I am planning to do. Any help is appreciated. Thanks in Advance.
Upvotes: 2
Views: 2735
Reputation: 4870
Use following code.
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class Test {
public static void main(String... args) throws Exception{
Map<String, String> rules = new HashMap<>();
rules.put("address.location", "loc");
rules.put("name", "name");
rules.put("id", "id");
String jsonData = "{name:'xy',id:'12', address: {location: 'abc'}, phones: ['a', 'b', 'c']}";
Person person = (Person) map(jsonData, Person.class, rules);
System.out.println(new Gson().toJson(person));
}
public static Object map(String jsonData, Class<?> rootType, Map<String, String> rules) throws Exception{
Map<String, Object> genericMap = new HashMap<>();
Type type = new TypeToken<Map<String, Object>>() {}.getType();
genericMap = new Gson().fromJson(jsonData, type);
Object object = rootType.newInstance();
for (Map.Entry<String, String> entry : rules.entrySet()){
String mapKey = entry.getKey();
String mapValue = entry.getValue();
String keys[] = mapKey.split("\\.");
Map<String, Object> obj = null;
if(keys.length > 1){
obj = (Map<String, Object>) genericMap.get(keys[0]);
for(int i=1;i<keys.length-1;i++){
obj = (Map<String, Object>) obj.get(keys[i]);
}
// Method method = object.getClass().getDeclaredMethod("set" + capitalize(mapValue) , String.class);
// method.invoke(object, (String)obj.get(keys[keys.length-1]));
Field field = object.getClass().getDeclaredField(mapValue);
field.set(object, obj.get(keys[keys.length - 1]));
}else{
// Method method = object.getClass().getDeclaredMethod("set" + capitalize(mapValue) , String.class);
// method.invoke(object, (String)genericMap.get(mapValue));
Field field = object.getClass().getDeclaredField(mapValue);
field.set(object, genericMap.get(mapValue));
}
}
return object;
}
public static String capitalize(String line){
return Character.toUpperCase(line.charAt(0)) + line.substring(1);
}
}
class Person {
String id;
String name;
String loc;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
Output
{"id":"12","name":"xy","loc":"abc","phones":["a","b","c"]}
Details
Upvotes: 2
Reputation: 1163
Import this library to your project code.google.com/p/google-gson
Afterwards, it is possible to do this: ("json" is whatever json-String you want to parse in. It could be the one you specified in your question.)
Person personFromJson = new Gson().fromJson(json, Person.class);
Upvotes: -1