哪些接口表示从JSON反序列化为AutoBean? [英] which interfaces representations to deserialize from JSON to AutoBean?

查看:158
本文介绍了哪些接口表示从JSON反序列化为AutoBean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON:

  {
bean1:{
bean12 :{
value1:4500,
value2:1500
},
bean13:{
value1:1550,
value2:550
}
}
}

I尝试使用AutoBean反序列化这个json,因为我有问题<一个>来弄清楚。我会喜欢以相反的方式。

哪些接口可以完美匹配此JSON,以便使用AutoBean进行反序列化工作?



其中bean1,bean12,bean13是接口,值全为BigDecimal。 div>

检查此示例。你必须有相应的接口(你的值有getter和setter)

$ p $ //声明任何类似bean的接口和setter,不需要基类型
interface Person {
Address getAddress();
String getName();
void setName(String name);
void setAddress(Address a);
}

接口地址{
//其他属性,如上
}

//声明工厂类型
接口MyFactory扩展AutoBeanFactory {
AutoBean<地址>地址();
AutoBean< Person>人();
}

class DoSomething(){
//实例化工厂
MyFactory factory = GWT.create(MyFactory.class);
//在非GWT代码中,使用AutoBeanFactorySource.create(MyFactory.class);

Person makePerson(){
//构造AutoBean
AutoBean< Person> person = factory.person();

//返回Person界面填充
return person.as();
}

String serializeToJson(Person person){
//检索AutoBean控制器
AutoBean< Person> bean = AutoBeanUtils.getAutoBean(person);

返回AutoBeanCodex.encode(bean).getPayload();
}

Person deserializeFromJson(String json){
AutoBean< Person> bean = AutoBeanCodex.decode(factory,Person.class,json);
return bean.as();
}
}


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.

解决方案

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();
      }
    }

这篇关于哪些接口表示从JSON反序列化为AutoBean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆