Deserialize server response

2019-06-13 03:32发布

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

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]

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

标签: java http gwt
2条回答
地球回转人心会变
2楼-- · 2019-06-13 03:37

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).

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).

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.

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.

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.

--

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.

查看更多
beautiful°
3楼-- · 2019-06-13 03:45

I think you are looking for something like restyGWT.

But I don't really understand your question so I might be wrong.

查看更多
登录 后发表回答