MikO MikO - 1 month ago 6
JSON Question

getting a timestamp out of a gwt-rpc string

i have a response from a server, which looks like:

//OK[0,1,"UpoNlmA",3,5,4,"UpoNlmA",3,2,0,1,"UpoUdDA",3,5,4,"UpoUdDA",3,2,0,1,"UpoZCBA",3,5,4,"UpoZCBA",3,2,0,1,"UpoJAoA",3,5,4,"UpoJNUg",3,2,0,1,"UpoLTHA",3,5,4,"UpoLdHo",3,2,0,1,"UpoSKkA",3,5,4,"UpoSKkA",3,2,0,1,"UpoP4FA",3,5,4,"UpoP4FA",3,2,0,1,"UpoWviA",3,5,4,"UpoWviA",3,2,8,1,["java.util.ArrayList/4159755760","com.initka.onlineinfo.server.datamodel.DepartureInformation/276724449","java.util.Date/3385151746","city","xyz"],0,7]


my reseach led me to the assumption that this is a GWT-RPC response. the question is now, how i can unserialize this strings
"UpoP4FA"
which used to be timestamps?

Answer

This is how Google Web Toolkit encodes longs in RPC. Also timestamps are encoded in this format.

The encoding is pretty simple: Instead of the alphabet 0123456789 (as used for decimals, base 10) the alphabet here is ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$ and the base is 64.

Here is a python snippet to decode a number encoded like this:

def from_gwt_long(inp):
    base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$"
    return sum(base.index(inp[p]) * (len(base) ** (len(inp) - p - 1)) for p in range(len(inp)))

You can also put the alphabet given above into this base converter: http://elenzil.com/esoterica/baseConversion.html

Comments