使用GWT上的AutoBean解析未知类型的JSON对象 [英] Parsing JSON objects of unknown type with AutoBean on GWT

查看:199
本文介绍了使用GWT上的AutoBean解析未知类型的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器返回JSON中的对象列表。例如,它们可能是 Cat s或 Dog s。



当我知道它们都是 Cat s时,我可以设置 AutoBeanCodex 来轻松工作。当我不知道他们是什么类型的时候,虽然...我该怎么办?



我可以给我所有的实体一个类型字段, d必须在将每个实体传递给> AutoBeanCodex 之前解析每个实体,这样才能抵消该点。我还有什么其他的选择?

解决方案

前些日子和这个一起玩,并且打了几个小时,尝试使用 @Category 方法和其他方法,直到找到这个:你可以创建一个类型为 Splittable 的属性,具有布尔值/字符串/列表/地图编码的基础传输类型。在我的情况下,我知道一些在设计阶段通过网络的包络类型,并且基于其他一些属性,其他一些领域可以是任何其他的自动化。



在编译时你甚至不需要知道另一个bean的类型,你可以使用 Splittable 的方法获取值,但如果使用autobeans,它

 接口信封{
String getStatus();}
String getDataType();
Splittable getData();

$ / code>

(如果您发送数据以及接收 - 编码a bean分解成一个`Splittable在一个信封中发送它比解码更容易)

通过线路发送的JSON被解码(可能使用 AutoBeanCodex )添加到 Envelope 类型中,并且在决定了什么类型必须从 getData() 方法,调用像这样获取嵌套对象

  SpecificNestedBean bean = AutoBeanCodex.decode工厂,
SpecificNestedBean.class,
env.getData())。as();

Envelope 类型和嵌套类型在上面的 factory 中)甚至不需要是相同的 AutoBeanFactory 类型。这可以让你从通用传输实例中抽象出信封的读写,并且为每个 dataType 字符串属性使用一个特定的工厂来解码数据的模型(并且嵌套模型)。


My server returns a list of objects in JSON. They might be Cats or Dogs, for example.

When I know that they'll all be Cats, I can set the AutoBeanCodex to work easily. When I don't know what types they are, though... what should I do?

I could give all of my entities a type field, but then I'd have to parse each entity before passing it to the AutoBeanCodex, which borders on defeating the point. What other options do I have?

解决方案

Just got to play with this the other day, and fought it for a few hours, trying @Category methods and others, until I found this: You can create a property of type Splittable, which represents the underlying transport type that has some encoding for booleans/Strings/Lists/Maps. In my case, I know some enveloping type that goes over the wire at design time, and based on some other property, some other field can be any number of other autobeans.

You don't even need to know the type of the other bean at compile time, you could get values out using Splittable's methods, but if using autobeans anyway, it is nice to define the data that is wrapped.

interface Envelope {
  String getStatus();
  String getDataType();
  Splittable getData();
}

(Setters might be desired if you sending data as well as recieving - encoding a bean into a `Splittable to send it in an envelope is even easier than decoding it)

The JSON sent over the wire is decoded (probably using AutoBeanCodex) into the Envelope type, and after you've decided what type must be coming out of the getData() method, call something like this to get the nested object out

SpecificNestedBean bean = AutoBeanCodex.decode(factory, 
                                               SpecificNestedBean.class, 
                                               env.getData()).as();

The Envelope type and the nested types (in factory above) don't even need to be the same AutoBeanFactory type. This could allow you to abstract out the reading/writing of envelopes from the generic transport instance, and use a specific factory for each dataType string property to decode the data's model (and nested models).

这篇关于使用GWT上的AutoBean解析未知类型的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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