反序列化服务器响应 [英] Deserialize server response

查看:86
本文介绍了反序列化服务器响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否是反序列化服务器响应的方法.因此,在我的情况下,我有一个LinkedHashMap<String,Date>并从服务器重播此内容:

I'm wondering if is the way to deserialize the server response. So in my case I have an LinkedHashMap<String,Date> and returing this from server:

 @Override
    public LinkedHashMap<String, Date> testHMap() {
        LinkedHashMap<String, Date> map = new LinkedHashMap<>();
        map.put("AA", new Date());
        map.put("BB", new Date());

        return map;
    }

我正在尝试获取有关另一个应用程序(gwt)的信息,因此我只能通过HTTP执行调用,从上例中,HTTP响应如下所示://OK['WM577vZ',4,5,2,'WM577vZ',4,3,2,2,0,1,["java.util.LinkedHashMap/3008245022","java.lang.String/2004016611","AA","java.util.Date/3385151746","BB"],0,7]

I'm trying to get info about another application(gwt) so I can perform calls only via HTTP, and from upper example the HTTP response looks like : //OK['WM577vZ',4,5,2,'WM577vZ',4,3,2,2,0,1,["java.util.LinkedHashMap/3008245022","java.lang.String/2004016611","AA","java.util.Date/3385151746","BB"],0,7]

那么,有没有办法从这个HTTP响应中获取LinkedHashMap数据?

So, is there a way to get the LinkedHashMap data from this HTTP respone?

推荐答案

LinkedHashMap位于该响应中-该响应是一个对象流(即,代替JSON,如果相同的值出现两次,则只会序列化一次,这样可以使内容变小,并且还允许循环引用,而不仅仅是平面树.

The LinkedHashMap is in that response - that response is an object stream (i.e. instead of JSON, if the same value appears twice, it will only be serialized once, which lets the content be smaller, and also allows cyclical references instead of only a flat tree).

向后"读取RPC有效负载-从末尾开始向后读取,我们从7(版本),0(已设置的标志)开始,到很大的字符串[](字符串"表格",解码回复所需的字符串,因此每个字符串仅列出一次).

Reading the RPC payload is done "backward" - starting from the end and reading backward, we start with 7 (the version), 0 (the flags that are set), a big [] of strings (the "string table", the strings that are needed to decode the reply, so that each string is only listed once).

然后是1-流中的第一个对象是字符串表中第一个条目的类型,即您要查找的"java.util.LinkedHashMap/3008245022".要解码LinkedHashMap,我们首先需要知道它的排序方式-下一个0值告诉我们它使用默认的插入顺序",然后下一个2表示在其中有两个条目地图.

Then a 1 - the first object in the stream is the type of the first entry in the string table, i.e. "java.util.LinkedHashMap/3008245022" as you are looking for. To decode a LinkedHashMap, we first need to know how it is ordered - the next 0 value tells us that it uses the default of "insertion-order", and then the next 2 says that there are two entries in the map.

现在,我们进行迭代,直到看到两对键和值.下一个值将告诉我们正在查看的键类型:2表示要进入字符串表,然后看到"java.lang.String/2004016611",因此我们知道它将是一个字符串,然后3向我们显示"AA"也来自字符串表.接下来是4,该键的值的类型,可以预料是字符串表中的"java.util.Date/3385151746".为了反序列化日期,我们从有效载荷中读取了long-GWT base64对其进行编码以使其更小-这是下一个标记'WM577vZ'.

Now we iterate until we've seen the two pairs of keys and values. The next value will tell us what kind of key we're looking at: 2 means to go into the string table and we see "java.lang.String/2004016611", so we know it will be a string, then the 3 shows us "AA" also from the string table. Next is 4, the type of the value for that key, predictably this "java.util.Date/3385151746" from the string table. To deserialize a date, we read a long from the payload - GWT base64-encodes these to keep them smaller - this is 'WM577vZ', the next token.

接下来的4个标记(254'WM577vZ')重复此过程,将第二个字符串键添加到地图及其日期值.

The next 4 tokens, (2, 5, 4, and 'WM577vZ') repeat this process, adding the second string key to the map, and the date value for it.

-

这种特殊的有效负载并不是真正显示RPC强大功能的那种,但是手工读取非常简单.目前,在GWT应用程序外部对其进行解码并不是一件容易的事(尽管我正在开发一种通用工具,该工具应该可以在任何地方对其进行解码,但是SO答案实际上并不是谈论它的地方)-如果您想要可以通过普通JS或其他非GWT技术处理的格式,RPC可能目前不是您的最佳选择.

This particular payload isn't the kind that really shows RPC's power, but it is fairly simple to read by hand. Decoding them outside of a GWT app is currently not very easy (though I'm working on a generalized tool which should let it be decoded anywhere, but a SO answer isn't really the place to talk about it) - if you want a format that can be handled by plain JS or some other non-GWT technology, RPC likely isn't your best bet at this time.

这篇关于反序列化服务器响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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