Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google end point returns JSON for long data type in quotes

I am using Google cloud end point for my rest service. I am consuming this data in a GWT web client using RestyGWT.

I noticed that cloud end point is automatically enclosing a long datatype in double quotes which is causing an exception in RestyGWT when I try to convert JSON to POJO.

Here is my sample code.

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

}

This is what is returned as JSON by cloud end point. You can see that testLong is serialized as "3234345" rather than 3234345.

enter image description here

I have the following questions. (1) How can I remove double quotes in long values ? (2) How can I change the string format to "yyyy-MMM-dd hh:mm:ss" ?

Regards, Sathya

like image 823
Sathya Avatar asked Nov 13 '22 07:11

Sathya


1 Answers

What version of restyGWT are you using ? Did you try 1.4 snapshot ? I think this is the code (1.4) responsible for parsing a long in restygwt, it might help you :

 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();
}
like image 114
Ronan Quillevere Avatar answered Nov 15 '22 06:11

Ronan Quillevere