问题
I have the following JSON :
{
"bean1": {
"bean12": {
"value1": 4500,
"value2": 1500
},
"bean13": {
"value1": 1550,
"value2": 550
}
}
}
I try to deserialize this json with AutoBean, since i have problem to figure it out. I will like to go the reverse way.
Which interfaces can perfect match this JSON so that deserializing with AutoBean work?
where bean1, bean12, bean13 are interfaces and the values are all BigDecimal.
回答1:
Check this sample. You must have corresponding interface(which has getter and setter for your values)
// Declare any bean-like interface with matching getters and setters, no base type is necessary
interface Person {
Address getAddress();
String getName();
void setName(String name);
void setAddress(Address a);
}
interface Address {
// Other properties, as above
}
// Declare the factory type
interface MyFactory extends AutoBeanFactory {
AutoBean<Address> address();
AutoBean<Person> person();
}
class DoSomething() {
// Instantiate the factory
MyFactory factory = GWT.create(MyFactory.class);
// In non-GWT code, use AutoBeanFactorySource.create(MyFactory.class);
Person makePerson() {
// Construct the AutoBean
AutoBean<Person> person = factory.person();
// Return the Person interface shim
return person.as();
}
String serializeToJson(Person person) {
// Retrieve the AutoBean controller
AutoBean<Person> bean = AutoBeanUtils.getAutoBean(person);
return AutoBeanCodex.encode(bean).getPayload();
}
Person deserializeFromJson(String json) {
AutoBean<Person> bean = AutoBeanCodex.decode(factory, Person.class, json);
return bean.as();
}
}
来源:https://stackoverflow.com/questions/15247259/which-interfaces-representations-to-deserialize-from-json-to-autobean