ambarket / json-simple

Automatically exported from code.google.com/p/json-simple
Apache License 2.0
0 stars 0 forks source link

An json-simple BUG: #74

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
java.util.Date object in the JSONObject JSONObject.toJSONString () should 
return the quoted date string description, in fact, less the quotes, so JSON 
format is the wrong way.
For example:
JSONObject json = new JSONObject();
json.put("age", 18);
json.put("birthday", new java.util.Date());
System.out.println(json.toJSONString());
Output:
{"birthday":Wed Jun 27 14:08:16 GMT+08:00 2012,"age":18}
"birthday" key value less quotes.It should be output: {"birthday": "Wed Jun 27 
14:08:16 GMT+08:00 2012","age":18}
The problem lies in: org.json.simple.toJSONString() Method The last line : 
return value.toString();
Should be changed: return "\""+escape(value.toString())+"\"";

Original issue reported on code.google.com by wjw465...@gmail.com on 27 Jun 2012 at 7:58

GoogleCodeExporter commented 9 years ago
It's by design to let user override toString() to return their customized 
encoding for any object not in the following mapping list:
http://code.google.com/p/json-simple/wiki/MappingBetweenJSONAndJavaEntities

You can use something like below to make it a JSON string:
json.put("birthday", new java.util.Date().toString())

Original comment by fangyid...@gmail.com on 4 Jul 2012 at 5:47