Menu

JSON and java.util.Date

Help
feanor
2007-03-07
2013-04-29
  • feanor

    feanor - 2007-03-07

    Hi,

    I have been using JSON-lib for some time now to convert Java beans to JSON for using in some client-side JavaScript.  It works great!  Kudos!

    I have a question regarding the transformation of java.util.Date to JSON.  JSON-lib currently transforms the java.util.Date as such:

    "theDate":{"month":4,"day":5,"year":104,"time":1083902400000,"seconds":0,"timezoneOffset":240,"date":7,"hours":0,"minutes":0}

    This is correct, but makes life on the JavaScript side a bit more complicated since "theDate" will not be converted to a JavaScript date upon eval().  Looking at Crockford json library in JavaScript, it turns a JavaScript date into "fullYear-month-dateTHours:minutes:seconds".

    Is there anyway in JSON-lib to convert a java.util.Date to JSON in a format like the one above?  If not, any plans to support this type of behavior in a future revision?

    Thanks.

     
    • aalmiray

      aalmiray - 2007-03-16

      Hi feanor,

      As you have noticed, the java.util.Date serialization is pretty straight forward in the sense that any Date is treated as
      a java bean. In the next major version of Json-lib it will be possible to plug-in custom serialization so this kind of
      issues will be solved.

      Thanks for using the lib, do not hesitate to send a feature request or bug!

      Cheers,
      Andres

       
    • feanor

      feanor - 2007-03-17

      Andres,

      Thanks for your response.
      That is good news.  A plug-in capability would be perfect.

      Keep up the good work.

       
    • Genius Zeng

      Genius Zeng - 2007-08-15

      how to use
      JsDateJsonValueProcessor,JsDateJsonBeanProcessor,
      jsonConfig.registerJsonBeanProcessor,jsonConfig.registerJsonValueProcessor
      please show me a example.
      i am using json-lib-2.0-jdk15.jar

      i use like:

      JsonConfig jsonConfig = JsonConfig.getInstance();
      //jsonConfig.registerJsonBeanProcessor(Date.class, new JsDateJsonBeanProcessor());
      jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsDateJsonValueProcessor());
      String json = JSONObject.fromObject(anObject).toString();

      but not effect.
      and how can i custom the output Date format?

      Thanks.

       
    • aalmiray

      aalmiray - 2007-08-15

      Hi Genius Zeng,
      You may find examples on how to configure processors looking at the tests located at
      src/tests/net/sf/json/TestJSONObjectWithProcessors

      You have three options for customizing the output format of a Date object:
      1) use your own JsonBeanProcessor
      2) use your own JsonValueProcessor
      3) subclass java.util.Date and implement JSONString

      Cheers,
      Andres

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.