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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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
Andres,
Thanks for your response.
That is good news. A plug-in capability would be perfect.
Keep up the good work.
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.
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