PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 522 lines | 405 code | 97 blank | 20 comment | 106 complexity | f7cb558d39663df20c240dd17b856d41 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.io.Writer;
  19. import java.lang.reflect.Proxy;
  20. import java.lang.reflect.Type;
  21. import java.nio.charset.Charset;
  22. import java.sql.Clob;
  23. import java.text.DateFormat;
  24. import java.text.SimpleDateFormat;
  25. import java.util.ArrayList;
  26. import java.util.Calendar;
  27. import java.util.Collection;
  28. import java.util.Date;
  29. import java.util.Enumeration;
  30. import java.util.IdentityHashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. import java.util.TimeZone;
  34. import com.alibaba.fastjson.JSON;
  35. import com.alibaba.fastjson.JSONAware;
  36. import com.alibaba.fastjson.JSONException;
  37. import com.alibaba.fastjson.JSONStreamAware;
  38. import com.alibaba.fastjson.util.ServiceLoader;
  39. /**
  40. * @author wenshao<szujobs@hotmail.com>
  41. */
  42. public class JSONSerializer {
  43. private final SerializeConfig config;
  44. private final SerializeWriter out;
  45. private List<PropertyFilter> propertyFilters = null;
  46. private List<ValueFilter> valueFilters = null;
  47. private List<NameFilter> nameFilters = null;
  48. private List<PropertyPreFilter> propertyPreFilters = null;
  49. private int indentCount = 0;
  50. private String indent = "\t";
  51. private String dateFormatPatterm = JSON.DEFFAULT_DATE_FORMAT;
  52. private DateFormat dateFormat;
  53. private IdentityHashMap<Object, SerialContext> references = null;
  54. private SerialContext context;
  55. public JSONSerializer(){
  56. this(new SerializeWriter(), SerializeConfig.getGlobalInstance());
  57. }
  58. public JSONSerializer(SerializeWriter out){
  59. this(out, SerializeConfig.getGlobalInstance());
  60. }
  61. public JSONSerializer(SerializeConfig config){
  62. this(new SerializeWriter(), config);
  63. }
  64. @Deprecated
  65. public JSONSerializer(JSONSerializerMap mapping){
  66. this(new SerializeWriter(), mapping);
  67. }
  68. public JSONSerializer(SerializeWriter out, SerializeConfig config){
  69. this.out = out;
  70. this.config = config;
  71. }
  72. public String getDateFormatPattern() {
  73. return dateFormatPatterm;
  74. }
  75. public DateFormat getDateFormat() {
  76. if (dateFormat == null) {
  77. dateFormat = new SimpleDateFormat(dateFormatPatterm);
  78. }
  79. return dateFormat;
  80. }
  81. public void setDateFormat(DateFormat dateFormat) {
  82. this.dateFormat = dateFormat;
  83. }
  84. public void setDateFormat(String dateFormat) {
  85. this.dateFormatPatterm = dateFormat;
  86. if (this.dateFormat != null) {
  87. this.dateFormat = null;
  88. }
  89. }
  90. public SerialContext getContext() {
  91. return context;
  92. }
  93. public void setContext(SerialContext context) {
  94. this.context = context;
  95. }
  96. public void setContext(SerialContext parent, Object object, Object fieldName) {
  97. if (isEnabled(SerializerFeature.DisableCircularReferenceDetect)) {
  98. return;
  99. }
  100. this.context = new SerialContext(parent, object, fieldName);
  101. if (references == null) {
  102. references = new IdentityHashMap<Object, SerialContext>();
  103. }
  104. this.references.put(object, context);
  105. }
  106. public void setContext(Object object, Object fieldName) {
  107. this.setContext(context, object, fieldName);
  108. }
  109. public void popContext() {
  110. if (context != null) {
  111. this.context = this.context.getParent();
  112. }
  113. }
  114. public void setContext(SerialContext parent, Object object) {
  115. if (isEnabled(SerializerFeature.DisableCircularReferenceDetect)) {
  116. return;
  117. }
  118. this.context = new SerialContext(parent, object, null);
  119. if (references == null) {
  120. references = new IdentityHashMap<Object, SerialContext>();
  121. }
  122. this.references.put(object, context);
  123. }
  124. public boolean isWriteClassName() {
  125. return isEnabled(SerializerFeature.WriteClassName);
  126. }
  127. public final boolean isWriteClassName(Type fieldType, Object obj) {
  128. boolean result = out.isEnabled(SerializerFeature.WriteClassName);
  129. if (!result) {
  130. return false;
  131. }
  132. if (fieldType == null) {
  133. if (this.isEnabled(SerializerFeature.NotWriteRootClassName)) {
  134. boolean isRoot = context.getParent() == null;
  135. if (isRoot) {
  136. return false;
  137. }
  138. }
  139. }
  140. return true;
  141. }
  142. public Collection<SerialContext> getReferences() {
  143. if (references == null) {
  144. references = new IdentityHashMap<Object, SerialContext>();
  145. }
  146. return references.values();
  147. }
  148. public SerialContext getSerialContext(Object object) {
  149. if (references == null) {
  150. return null;
  151. }
  152. return references.get(object);
  153. }
  154. public boolean containsReference(Object value) {
  155. if (isEnabled(SerializerFeature.DisableCircularReferenceDetect)) {
  156. return false;
  157. }
  158. if (references == null) {
  159. return false;
  160. }
  161. return references.containsKey(value);
  162. }
  163. public void writeReference(Object object) {
  164. if (isEnabled(SerializerFeature.DisableCircularReferenceDetect)) {
  165. return;
  166. }
  167. SerialContext context = this.getContext();
  168. Object current = context.getObject();
  169. if (object == current) {
  170. out.write("{\"$ref\":\"@\"}");
  171. return;
  172. }
  173. SerialContext parentContext = context.getParent();
  174. if (parentContext != null) {
  175. if (object == parentContext.getObject()) {
  176. out.write("{\"$ref\":\"..\"}");
  177. return;
  178. }
  179. }
  180. SerialContext rootContext = context;
  181. for (;;) {
  182. if (rootContext.getParent() == null) {
  183. break;
  184. }
  185. rootContext = rootContext.getParent();
  186. }
  187. if (object == rootContext.getObject()) {
  188. out.write("{\"$ref\":\"$\"}");
  189. return;
  190. }
  191. SerialContext refContext = this.getSerialContext(object);
  192. String path = refContext.getPath();
  193. out.write("{\"$ref\":\"");
  194. out.write(path);
  195. out.write("\"}");
  196. return;
  197. }
  198. public List<ValueFilter> getValueFilters() {
  199. if (valueFilters == null) {
  200. valueFilters = new ArrayList<ValueFilter>();
  201. }
  202. return valueFilters;
  203. }
  204. public List<ValueFilter> getValueFiltersDirect() {
  205. return valueFilters;
  206. }
  207. public int getIndentCount() {
  208. return indentCount;
  209. }
  210. public void incrementIndent() {
  211. indentCount++;
  212. }
  213. public void decrementIdent() {
  214. indentCount--;
  215. }
  216. public void println() {
  217. out.write('\n');
  218. for (int i = 0; i < indentCount; ++i) {
  219. out.write(indent);
  220. }
  221. }
  222. public List<NameFilter> getNameFilters() {
  223. if (nameFilters == null) {
  224. nameFilters = new ArrayList<NameFilter>();
  225. }
  226. return nameFilters;
  227. }
  228. public List<NameFilter> getNameFiltersDirect() {
  229. return nameFilters;
  230. }
  231. public List<PropertyPreFilter> getPropertyPreFilters() {
  232. if (propertyPreFilters == null) {
  233. propertyPreFilters = new ArrayList<PropertyPreFilter>();
  234. }
  235. return propertyPreFilters;
  236. }
  237. public List<PropertyPreFilter> getPropertyPreFiltersDirect() {
  238. return propertyPreFilters;
  239. }
  240. public List<PropertyFilter> getPropertyFilters() {
  241. if (propertyFilters == null) {
  242. propertyFilters = new ArrayList<PropertyFilter>();
  243. }
  244. return propertyFilters;
  245. }
  246. public List<PropertyFilter> getPropertyFiltersDirect() {
  247. return propertyFilters;
  248. }
  249. public SerializeWriter getWriter() {
  250. return out;
  251. }
  252. public String toString() {
  253. return out.toString();
  254. }
  255. public void config(SerializerFeature feature, boolean state) {
  256. out.config(feature, state);
  257. }
  258. public boolean isEnabled(SerializerFeature feature) {
  259. return out.isEnabled(feature);
  260. }
  261. public void writeNull() {
  262. this.out.writeNull();
  263. }
  264. public SerializeConfig getMapping() {
  265. return config;
  266. }
  267. public static final void write(Writer out, Object object) {
  268. SerializeWriter writer = new SerializeWriter();
  269. try {
  270. JSONSerializer serializer = new JSONSerializer(writer);
  271. serializer.write(object);
  272. writer.writeTo(out);
  273. } catch (IOException ex) {
  274. throw new JSONException(ex.getMessage(), ex);
  275. } finally {
  276. writer.close();
  277. }
  278. }
  279. public static final void write(SerializeWriter out, Object object) {
  280. JSONSerializer serializer = new JSONSerializer(out);
  281. serializer.write(object);
  282. }
  283. public final void write(Object object) {
  284. if (object == null) {
  285. out.writeNull();
  286. return;
  287. }
  288. Class<?> clazz = object.getClass();
  289. ObjectSerializer writer = getObjectWriter(clazz);
  290. try {
  291. writer.write(this, object, null, null);
  292. } catch (IOException e) {
  293. throw new JSONException(e.getMessage(), e);
  294. }
  295. }
  296. public final void writeWithFieldName(Object object, Object fieldName) {
  297. writeWithFieldName(object, fieldName, null);
  298. }
  299. public final void writeWithFieldName(Object object, Object fieldName, Type fieldType) {
  300. try {
  301. if (object == null) {
  302. out.writeNull();
  303. return;
  304. }
  305. Class<?> clazz = object.getClass();
  306. ObjectSerializer writer = getObjectWriter(clazz);
  307. writer.write(this, object, fieldName, fieldType);
  308. } catch (IOException e) {
  309. throw new JSONException(e.getMessage(), e);
  310. }
  311. }
  312. public final void writeWithFormat(Object object, String format) {
  313. if (object instanceof Date) {
  314. String text = new SimpleDateFormat(format).format((Date) object);
  315. out.writeString(text);
  316. return;
  317. }
  318. write(object);
  319. }
  320. public final void write(String text) {
  321. StringSerializer.instance.write(this, text);
  322. }
  323. public ObjectSerializer getObjectWriter(Class<?> clazz) {
  324. ObjectSerializer writer = config.get(clazz);
  325. if (writer == null) {
  326. try {
  327. final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  328. for (Object o : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) {
  329. if (!(o instanceof AutowiredObjectSerializer)) {
  330. continue;
  331. }
  332. AutowiredObjectSerializer autowired = (AutowiredObjectSerializer) o;
  333. for (Type forType : autowired.getAutowiredFor()) {
  334. config.put(forType, autowired);
  335. }
  336. }
  337. } catch (ClassCastException ex) {
  338. // skip
  339. }
  340. writer = config.get(clazz);
  341. }
  342. if (writer == null) {
  343. final ClassLoader classLoader = JSON.class.getClassLoader();
  344. if (classLoader != Thread.currentThread().getContextClassLoader()) {
  345. try {
  346. for (Object o : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) {
  347. if (!(o instanceof AutowiredObjectSerializer)) {
  348. continue;
  349. }
  350. AutowiredObjectSerializer autowired = (AutowiredObjectSerializer) o;
  351. for (Type forType : autowired.getAutowiredFor()) {
  352. config.put(forType, autowired);
  353. }
  354. }
  355. } catch (ClassCastException ex) {
  356. // skip
  357. }
  358. writer = config.get(clazz);
  359. }
  360. }
  361. if (writer == null) {
  362. if (Map.class.isAssignableFrom(clazz)) {
  363. config.put(clazz, MapSerializer.instance);
  364. } else if (List.class.isAssignableFrom(clazz)) {
  365. config.put(clazz, ListSerializer.instance);
  366. } else if (Collection.class.isAssignableFrom(clazz)) {
  367. config.put(clazz, CollectionSerializer.instance);
  368. } else if (Date.class.isAssignableFrom(clazz)) {
  369. config.put(clazz, DateSerializer.instance);
  370. } else if (JSONAware.class.isAssignableFrom(clazz)) {
  371. config.put(clazz, JSONAwareSerializer.instance);
  372. } else if (JSONStreamAware.class.isAssignableFrom(clazz)) {
  373. config.put(clazz, JSONStreamAwareSerializer.instance);
  374. } else if (clazz.isEnum() || (clazz.getSuperclass() != null && clazz.getSuperclass().isEnum())) {
  375. config.put(clazz, EnumSerializer.instance);
  376. } else if (clazz.isArray()) {
  377. Class<?> componentType = clazz.getComponentType();
  378. ObjectSerializer compObjectSerializer = getObjectWriter(componentType);
  379. config.put(clazz, new ArraySerializer(compObjectSerializer));
  380. } else if (Throwable.class.isAssignableFrom(clazz)) {
  381. config.put(clazz, new ExceptionSerializer(clazz));
  382. } else if (TimeZone.class.isAssignableFrom(clazz)) {
  383. config.put(clazz, TimeZoneSerializer.instance);
  384. } else if (Appendable.class.isAssignableFrom(clazz)) {
  385. config.put(clazz, AppendableSerializer.instance);
  386. } else if (Charset.class.isAssignableFrom(clazz)) {
  387. config.put(clazz, CharsetSerializer.instance);
  388. } else if (Enumeration.class.isAssignableFrom(clazz)) {
  389. config.put(clazz, EnumerationSeriliazer.instance);
  390. } else if (Calendar.class.isAssignableFrom(clazz)) {
  391. config.put(clazz, CalendarSerializer.instance);
  392. } else if (Clob.class.isAssignableFrom(clazz)) {
  393. config.put(clazz, ClobSeriliazer.instance);
  394. } else {
  395. boolean isCglibProxy = false;
  396. boolean isJavassistProxy = false;
  397. for (Class<?> item : clazz.getInterfaces()) {
  398. if (item.getName().equals("net.sf.cglib.proxy.Factory")) {
  399. isCglibProxy = true;
  400. break;
  401. } else if (item.getName().equals("javassist.util.proxy.ProxyObject")) {
  402. isJavassistProxy = true;
  403. break;
  404. }
  405. }
  406. if (isCglibProxy || isJavassistProxy) {
  407. Class<?> superClazz = clazz.getSuperclass();
  408. ObjectSerializer superWriter = getObjectWriter(superClazz);
  409. config.put(clazz, superWriter);
  410. return superWriter;
  411. }
  412. if (Proxy.isProxyClass(clazz)) {
  413. config.put(clazz, config.createJavaBeanSerializer(clazz));
  414. } else {
  415. config.put(clazz, config.createJavaBeanSerializer(clazz));
  416. }
  417. }
  418. writer = config.get(clazz);
  419. }
  420. return writer;
  421. }
  422. public void close() {
  423. this.out.close();
  424. }
  425. }