I want to modify a java object by passing a json String to my application. The String will not contain all information about the complete, modified object but merely a single member that's meant to be set.
class SomeClass {
Object var1 = "Hello";
Object var2 = "AAA";
// A lot of fields goes here ...
}
public AppTest() throws Exception {
SomeClass myObject = new SomeClass();
myObject.var2 = "BBB";
String modification = "{\"var1\":\"Goodbye\"}";
Gson gson = new Gson();
SomeClass modifed = gson.fromJson(modification, SomeClass.class);
// TODO: Merge a modifed object into myObject somehow
}
Furthermore, some of the fields might be objects with any number of fields. Again, I might want to just modify a single primitive inside the child object. A more complex example:
class SomeOtherClass {
String var4 = "444";
String var5 = "555";
}
class SomeClass {
Object var1 = "111";
Object var2 = "222";
SomeOtherClass var3 = new SomeOtherClass();
}
public AppTest() throws Exception {
SomeClass myObject = new SomeClass();
myObject.var2 = "AAA";
myObject.var3.var5 = "BBB";
String modification = "{\"var3\":{\"var5\":\"XXX\"}}";
Gson gson = new Gson();
SomeClass modifed = gson.fromJson(modification, SomeClass.class);
// TODO: Merge the modifed object into myObject somehow
}
So, my question is how can I partially modify an object with JSON?