which interfaces representations to deserialize from JSON to AutoBean?

社会主义新天地 提交于 2019-12-13 06:25:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!