PageRenderTime 35ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/alibaba/fastjson/serializer/DateSerializer.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 124 lines | 90 code | 16 blank | 18 comment | 25 complexity | fbd436b6175da08b49faefdd9db17ea2 MD5 | raw file
  1. /*
  2. * Copyright 1999-2101 Alibaba Group.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.fastjson.serializer;
  17. import java.io.IOException;
  18. import java.lang.reflect.Type;
  19. import java.text.DateFormat;
  20. import java.util.Calendar;
  21. import java.util.Date;
  22. import com.alibaba.fastjson.util.IOUtils;
  23. /**
  24. * @author wenshao<szujobs@hotmail.com>
  25. */
  26. public class DateSerializer implements ObjectSerializer {
  27. public final static DateSerializer instance = new DateSerializer();
  28. public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
  29. SerializeWriter out = serializer.getWriter();
  30. if (object == null) {
  31. out.writeNull();
  32. return;
  33. }
  34. if (out.isEnabled(SerializerFeature.WriteClassName)) {
  35. if (object.getClass() != fieldType) {
  36. if (object.getClass() == java.util.Date.class) {
  37. out.write("new Date(");
  38. out.writeLongAndChar(((Date) object).getTime(), ')');
  39. } else {
  40. out.write('{');
  41. out.writeFieldName("@type");
  42. serializer.write(object.getClass().getName());
  43. out.writeFieldValue(',', "val", ((Date) object).getTime());
  44. out.write('}');
  45. }
  46. return;
  47. }
  48. }
  49. Date date = (Date) object;
  50. if (out.isEnabled(SerializerFeature.WriteDateUseDateFormat)) {
  51. DateFormat format = serializer.getDateFormat();
  52. String text = format.format(date);
  53. out.writeString(text);
  54. return;
  55. }
  56. long time = date.getTime();
  57. if (serializer.isEnabled(SerializerFeature.UseISO8601DateFormat)) {
  58. if (serializer.isEnabled(SerializerFeature.UseSingleQuotes)) {
  59. out.append('\'');
  60. } else {
  61. out.append('\"');
  62. }
  63. Calendar calendar = Calendar.getInstance();
  64. calendar.setTimeInMillis(time);
  65. int year = calendar.get(Calendar.YEAR);
  66. int month = calendar.get(Calendar.MONTH) + 1;
  67. int day = calendar.get(Calendar.DAY_OF_MONTH);
  68. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  69. int minute = calendar.get(Calendar.MINUTE);
  70. int second = calendar.get(Calendar.SECOND);
  71. int millis = calendar.get(Calendar.MILLISECOND);
  72. char[] buf;
  73. if (millis != 0) {
  74. buf = "0000-00-00T00:00:00.000".toCharArray();
  75. IOUtils.getChars(millis, 23, buf);
  76. IOUtils.getChars(second, 19, buf);
  77. IOUtils.getChars(minute, 16, buf);
  78. IOUtils.getChars(hour, 13, buf);
  79. IOUtils.getChars(day, 10, buf);
  80. IOUtils.getChars(month, 7, buf);
  81. IOUtils.getChars(year, 4, buf);
  82. } else {
  83. if (second == 0 && minute == 0 && hour == 0) {
  84. buf = "0000-00-00".toCharArray();
  85. IOUtils.getChars(day, 10, buf);
  86. IOUtils.getChars(month, 7, buf);
  87. IOUtils.getChars(year, 4, buf);
  88. } else {
  89. buf = "0000-00-00T00:00:00".toCharArray();
  90. IOUtils.getChars(second, 19, buf);
  91. IOUtils.getChars(minute, 16, buf);
  92. IOUtils.getChars(hour, 13, buf);
  93. IOUtils.getChars(day, 10, buf);
  94. IOUtils.getChars(month, 7, buf);
  95. IOUtils.getChars(year, 4, buf);
  96. }
  97. }
  98. out.write(buf);
  99. if (serializer.isEnabled(SerializerFeature.UseSingleQuotes)) {
  100. out.append('\'');
  101. } else {
  102. out.append('\"');
  103. }
  104. } else {
  105. out.writeLong(time);
  106. }
  107. }
  108. }