Please use the google group to ask questions - thanks.

by crift 26 Aug 09:40
Jaxrs Timestamp json problem

When I define a (java.util.Date)property in model(Timestamp in database), I got the model text/json response like this.

{"currentStep":1,"createTime":2010-08-26 16:13:57.0,"name":"x"}

The createTime is not quoted.

Does anyone know this problem?

Thanks.

27 Aug 07:54
by Rob

If you can post your question to the google group that would be great. That is the preferred choice to answer questions now.

Thanks, Rob.

27 Aug 22:59
by Rob

BTW: This is the expected behaviour. There is no standard approach for format and parsing Date/DateTime in JSON. With Ebean you can configure the approach used using JsonValueAdapter.

An example below:

package com.avaje.tests.text.json;

import java.sql.Date;
import java.sql.Timestamp;

import org.junit.Assert;

import junit.framework.TestCase;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.GlobalProperties;
import com.avaje.ebean.text.json.JsonContext;
import com.avaje.ebean.text.json.JsonValueAdapter;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.ebeaninternal.server.text.json.DefaultJsonValueAdapter;
import com.avaje.tests.model.basic.Car;
import com.avaje.tests.model.basic.Vehicle;

public class TestTextJsonUtilDateFormat extends TestCase {

    public void test() {
        
        GlobalProperties.put("ebean.ddl.generate", "false");
        GlobalProperties.put("ebean.ddl.run", "false");
        
        Vehicle v = new Car();
        v.setId(100);
        v.setRegistrationDate(new java.util.Date());
        v.setUpdtime(new Timestamp(System.currentTimeMillis()));
        
        JsonContext context = Ebean.createJsonContext();
        JsonWriteOptions o = new JsonWriteOptions();
        o.setValueAdapter(new CustomDateFormatAdapter());
        
        String jsonString = context.toJsonString(v, true, o);
        //System.out.println(jsonString);
        
        Assert.assertTrue(jsonString.contains("\"registrationDate\":'"));
    }
    
    class CustomDateFormatAdapter implements JsonValueAdapter {

        DefaultJsonValueAdapter defaultImplementation = new DefaultJsonValueAdapter();
        
        public String jsonFromDate(Date date) {
            // TODO
            return null;
        }

        public String jsonFromTimestamp(Timestamp date) {
            // add some single quotes around the timestamp value
            return "'"+defaultImplementation.jsonFromTimestamp(date)+"'";
        }

        public Date jsonToDate(String jsonDate) {
            // TODO 
            return null;
        }

        public Timestamp jsonToTimestamp(String jsonDateTime) {
            // TODO 
            return null;
        }
        
    }
}

Create a New Topic

Title:
Body:
 
Introduction User Guide (pdf) Install/Configure Public JavaDoc Whitepapers
General Database Specific Byte Code Deployment Annotations Features
Top Bugs Top Enhancements
woResponse