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