4

我正在使用Google云终点作为我的休息服务。我在使用RestyGWT的GWT Web客户端中使用这些数据。谷歌端点为长数据类型返回JSON引号

我注意到,云端点自动将长数据类型包含在双引号中,当我尝试将JSON转换为POJO时,这会导致RestyGWT出现异常。

这是我的示例代码。

@Api(name = "test") 
public class EndpointAPI { 

@ApiMethod(httpMethod = HttpMethod.GET, path = "test") 
public Container test() { 
    Container container = new Container(); 

    container.testLong = (long)3234345; 
    container.testDate = new Date(); 
    container.testString = "sathya"; 
    container.testDouble = 123.98; 
    container.testInt = 123;     
    return container; 
} 
public class Container { 
    public long testLong; 
    public Date testDate; 
    public String testString; 
    public double testDouble; 
    public int testInt; 
} 

}

这就是云终点返回JSON。你可以看到,testLong序列化为“3234345”,而不是3234345.

enter image description here

我有以下几个问题。 (1)如何删除长整型值中的双引号? (2)如何将字符串格式更改为“yyyy-MMM-dd hh:mm:ss”?

问候, 沙迪亚

您正在使用什么版本restyGWT的
+1

你不想“删除引号”:不是所有的长值,可以表示为JS Number和RestyGWT可能会将JSON解析为JS对象('JSON.parse()'或'eval()')。不,你真的希望RestyGWT正确使用'Long.parseLong()'(不知道该怎么做,如果可能的话;我不知道RestyGWT)。至于日期,你为什么要*不*使用标准格式? – 2013-04-11 08:47:46

+0

谢谢。我找不到一个方法,但如何使正确的restygwt解析长。与日期格式相同的问题 - restygwt在反序列化时抛出异常。 – Sathya 2013-04-11 09:06:33

回答

1

?你试过1.4快照吗? 我认为这是代码(1.4)负责解析长restygwt,它可以帮助你:

public static final AbstractJsonEncoderDecoder<Long> LONG = new AbstractJsonEncoderDecoder<Long>() { 

    public Long decode(JSONValue value) throws DecodingException { 
     if (value == null || value.isNull() != null) { 
      return null; 
     } 
     return (long) toDouble(value); 
    } 

    public JSONValue encode(Long value) throws EncodingException { 
     return (value == null) ? getNullType() : new JSONNumber(value); 
    } 
}; 

static public double toDouble(JSONValue value) { 
    JSONNumber number = value.isNumber(); 
    if (number == null) { 
     JSONString val = value.isString(); 
     if (val != null){ 
      try { 
       return Double.parseDouble(val.stringValue()); 
      } 
      catch(NumberFormatException e){ 
       // just through exception below 
      } 
     } 
     throw new DecodingException("Expected a json number, but was given: " + value); 
    } 
    return number.doubleValue(); 
}