PageRenderTime 70ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/qt-prometheus/qt-prometheus
Java | 595 lines | 537 code | 54 blank | 4 comment | 116 complexity | f79a820ed9961225138f6407c68c792d MD5 | raw file
  1. package com.alibaba.fastjson.serializer;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONAware;
  4. import com.alibaba.fastjson.JSONException;
  5. import com.alibaba.fastjson.JSONStreamAware;
  6. import com.alibaba.fastjson.util.ServiceLoader;
  7. import java.io.IOException;
  8. import java.io.Writer;
  9. import java.lang.reflect.Proxy;
  10. import java.lang.reflect.Type;
  11. import java.nio.charset.Charset;
  12. import java.sql.Clob;
  13. import java.text.DateFormat;
  14. import java.text.SimpleDateFormat;
  15. import java.util.ArrayList;
  16. import java.util.Calendar;
  17. import java.util.Collection;
  18. import java.util.Date;
  19. import java.util.Enumeration;
  20. import java.util.IdentityHashMap;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.TimeZone;
  26. public class JSONSerializer
  27. {
  28. private List<AfterFilter> afterFilters = null;
  29. private List<BeforeFilter> beforeFilters = null;
  30. private final SerializeConfig config;
  31. private SerialContext context;
  32. private DateFormat dateFormat;
  33. private String dateFormatPattern;
  34. private String indent = "\t";
  35. private int indentCount = 0;
  36. private List<NameFilter> nameFilters = null;
  37. private final SerializeWriter out;
  38. private List<PropertyFilter> propertyFilters = null;
  39. private List<PropertyPreFilter> propertyPreFilters = null;
  40. private IdentityHashMap<Object, SerialContext> references = null;
  41. private List<ValueFilter> valueFilters = null;
  42. public JSONSerializer()
  43. {
  44. this(new SerializeWriter(), SerializeConfig.getGlobalInstance());
  45. }
  46. @Deprecated
  47. public JSONSerializer(JSONSerializerMap paramJSONSerializerMap)
  48. {
  49. this(new SerializeWriter(), paramJSONSerializerMap);
  50. }
  51. public JSONSerializer(SerializeConfig paramSerializeConfig)
  52. {
  53. this(new SerializeWriter(), paramSerializeConfig);
  54. }
  55. public JSONSerializer(SerializeWriter paramSerializeWriter)
  56. {
  57. this(paramSerializeWriter, SerializeConfig.getGlobalInstance());
  58. }
  59. public JSONSerializer(SerializeWriter paramSerializeWriter, SerializeConfig paramSerializeConfig)
  60. {
  61. this.out = paramSerializeWriter;
  62. this.config = paramSerializeConfig;
  63. }
  64. public static final void write(SerializeWriter paramSerializeWriter, Object paramObject)
  65. {
  66. new JSONSerializer(paramSerializeWriter).write(paramObject);
  67. }
  68. public static final void write(Writer paramWriter, Object paramObject)
  69. {
  70. SerializeWriter localSerializeWriter = new SerializeWriter();
  71. try
  72. {
  73. new JSONSerializer(localSerializeWriter).write(paramObject);
  74. localSerializeWriter.writeTo(paramWriter);
  75. return;
  76. }
  77. catch (IOException paramWriter)
  78. {
  79. throw new JSONException(paramWriter.getMessage(), paramWriter);
  80. }
  81. finally
  82. {
  83. localSerializeWriter.close();
  84. }
  85. throw paramWriter;
  86. }
  87. public void close()
  88. {
  89. this.out.close();
  90. }
  91. public void config(SerializerFeature paramSerializerFeature, boolean paramBoolean)
  92. {
  93. this.out.config(paramSerializerFeature, paramBoolean);
  94. }
  95. public boolean containsReference(Object paramObject)
  96. {
  97. if (this.references == null)
  98. return false;
  99. return this.references.containsKey(paramObject);
  100. }
  101. public void decrementIdent()
  102. {
  103. this.indentCount -= 1;
  104. }
  105. public List<AfterFilter> getAfterFilters()
  106. {
  107. if (this.afterFilters == null)
  108. this.afterFilters = new ArrayList();
  109. return this.afterFilters;
  110. }
  111. public List<AfterFilter> getAfterFiltersDirect()
  112. {
  113. return this.afterFilters;
  114. }
  115. public List<BeforeFilter> getBeforeFilters()
  116. {
  117. if (this.beforeFilters == null)
  118. this.beforeFilters = new ArrayList();
  119. return this.beforeFilters;
  120. }
  121. public List<BeforeFilter> getBeforeFiltersDirect()
  122. {
  123. return this.beforeFilters;
  124. }
  125. public SerialContext getContext()
  126. {
  127. return this.context;
  128. }
  129. public DateFormat getDateFormat()
  130. {
  131. if ((this.dateFormat == null) && (this.dateFormatPattern != null))
  132. this.dateFormat = new SimpleDateFormat(this.dateFormatPattern);
  133. return this.dateFormat;
  134. }
  135. public String getDateFormatPattern()
  136. {
  137. if ((this.dateFormat instanceof SimpleDateFormat))
  138. return ((SimpleDateFormat)this.dateFormat).toPattern();
  139. return this.dateFormatPattern;
  140. }
  141. public int getIndentCount()
  142. {
  143. return this.indentCount;
  144. }
  145. public SerializeConfig getMapping()
  146. {
  147. return this.config;
  148. }
  149. public List<NameFilter> getNameFilters()
  150. {
  151. if (this.nameFilters == null)
  152. this.nameFilters = new ArrayList();
  153. return this.nameFilters;
  154. }
  155. public List<NameFilter> getNameFiltersDirect()
  156. {
  157. return this.nameFilters;
  158. }
  159. public ObjectSerializer getObjectWriter(Class<?> paramClass)
  160. {
  161. Object localObject4 = (ObjectSerializer)this.config.get(paramClass);
  162. Object localObject1 = localObject4;
  163. Object localObject5;
  164. Type localType;
  165. Object localObject2;
  166. if (localObject4 == null)
  167. try
  168. {
  169. localObject1 = ServiceLoader.load(AutowiredObjectSerializer.class, Thread.currentThread().getContextClassLoader()).iterator();
  170. while (((Iterator)localObject1).hasNext())
  171. {
  172. localObject4 = ((Iterator)localObject1).next();
  173. if ((localObject4 instanceof AutowiredObjectSerializer))
  174. {
  175. localObject4 = (AutowiredObjectSerializer)localObject4;
  176. localObject5 = ((AutowiredObjectSerializer)localObject4).getAutowiredFor().iterator();
  177. while (((Iterator)localObject5).hasNext())
  178. {
  179. localType = (Type)((Iterator)localObject5).next();
  180. this.config.put(localType, localObject4);
  181. }
  182. }
  183. }
  184. }
  185. catch (ClassCastException localClassCastException1)
  186. {
  187. localObject2 = (ObjectSerializer)this.config.get(paramClass);
  188. }
  189. localObject4 = localObject2;
  190. if (localObject2 == null)
  191. {
  192. localObject5 = JSON.class.getClassLoader();
  193. localObject4 = localObject2;
  194. if (localObject5 != Thread.currentThread().getContextClassLoader())
  195. try
  196. {
  197. localObject2 = ServiceLoader.load(AutowiredObjectSerializer.class, (ClassLoader)localObject5).iterator();
  198. while (((Iterator)localObject2).hasNext())
  199. {
  200. localObject4 = ((Iterator)localObject2).next();
  201. if ((localObject4 instanceof AutowiredObjectSerializer))
  202. {
  203. localObject4 = (AutowiredObjectSerializer)localObject4;
  204. localObject5 = ((AutowiredObjectSerializer)localObject4).getAutowiredFor().iterator();
  205. while (((Iterator)localObject5).hasNext())
  206. {
  207. localType = (Type)((Iterator)localObject5).next();
  208. this.config.put(localType, localObject4);
  209. }
  210. }
  211. }
  212. }
  213. catch (ClassCastException localClassCastException2)
  214. {
  215. localObject4 = (ObjectSerializer)this.config.get(paramClass);
  216. }
  217. }
  218. Object localObject3 = localObject4;
  219. if (localObject4 == null)
  220. {
  221. if (!Map.class.isAssignableFrom(paramClass))
  222. break label295;
  223. this.config.put(paramClass, MapSerializer.instance);
  224. }
  225. label794:
  226. while (true)
  227. {
  228. localObject3 = (ObjectSerializer)this.config.get(paramClass);
  229. return localObject3;
  230. label295: if (List.class.isAssignableFrom(paramClass))
  231. {
  232. this.config.put(paramClass, ListSerializer.instance);
  233. }
  234. else if (Collection.class.isAssignableFrom(paramClass))
  235. {
  236. this.config.put(paramClass, CollectionSerializer.instance);
  237. }
  238. else if (Date.class.isAssignableFrom(paramClass))
  239. {
  240. this.config.put(paramClass, DateSerializer.instance);
  241. }
  242. else if (JSONAware.class.isAssignableFrom(paramClass))
  243. {
  244. this.config.put(paramClass, JSONAwareSerializer.instance);
  245. }
  246. else if (JSONStreamAware.class.isAssignableFrom(paramClass))
  247. {
  248. this.config.put(paramClass, JSONStreamAwareSerializer.instance);
  249. }
  250. else if ((paramClass.isEnum()) || ((paramClass.getSuperclass() != null) && (paramClass.getSuperclass().isEnum())))
  251. {
  252. this.config.put(paramClass, EnumSerializer.instance);
  253. }
  254. else if (paramClass.isArray())
  255. {
  256. localObject3 = paramClass.getComponentType();
  257. localObject4 = getObjectWriter((Class)localObject3);
  258. this.config.put(paramClass, new ArraySerializer((Class)localObject3, (ObjectSerializer)localObject4));
  259. }
  260. else if (Throwable.class.isAssignableFrom(paramClass))
  261. {
  262. this.config.put(paramClass, new ExceptionSerializer(paramClass));
  263. }
  264. else if (TimeZone.class.isAssignableFrom(paramClass))
  265. {
  266. this.config.put(paramClass, TimeZoneSerializer.instance);
  267. }
  268. else if (Appendable.class.isAssignableFrom(paramClass))
  269. {
  270. this.config.put(paramClass, AppendableSerializer.instance);
  271. }
  272. else if (Charset.class.isAssignableFrom(paramClass))
  273. {
  274. this.config.put(paramClass, CharsetSerializer.instance);
  275. }
  276. else if (Enumeration.class.isAssignableFrom(paramClass))
  277. {
  278. this.config.put(paramClass, EnumerationSeriliazer.instance);
  279. }
  280. else if (Calendar.class.isAssignableFrom(paramClass))
  281. {
  282. this.config.put(paramClass, CalendarSerializer.instance);
  283. }
  284. else if (Clob.class.isAssignableFrom(paramClass))
  285. {
  286. this.config.put(paramClass, ClobSeriliazer.instance);
  287. }
  288. else
  289. {
  290. int m = 0;
  291. int n = 0;
  292. localObject3 = paramClass.getInterfaces();
  293. int i1 = localObject3.length;
  294. int k = 0;
  295. while (true)
  296. {
  297. int i = m;
  298. int j = n;
  299. if (k < i1)
  300. {
  301. localObject4 = localObject3[k];
  302. if (!((Class)localObject4).getName().equals("net.sf.cglib.proxy.Factory"))
  303. break label762;
  304. i = 1;
  305. j = n;
  306. }
  307. while (true)
  308. {
  309. if ((i == 0) && (j == 0))
  310. break label794;
  311. localObject3 = getObjectWriter(paramClass.getSuperclass());
  312. this.config.put(paramClass, localObject3);
  313. return localObject3;
  314. label762: if (!((Class)localObject4).getName().equals("javassist.util.proxy.ProxyObject"))
  315. break;
  316. j = 1;
  317. i = m;
  318. }
  319. k += 1;
  320. }
  321. if (Proxy.isProxyClass(paramClass))
  322. this.config.put(paramClass, this.config.createJavaBeanSerializer(paramClass));
  323. else
  324. this.config.put(paramClass, this.config.createJavaBeanSerializer(paramClass));
  325. }
  326. }
  327. }
  328. public List<PropertyFilter> getPropertyFilters()
  329. {
  330. if (this.propertyFilters == null)
  331. this.propertyFilters = new ArrayList();
  332. return this.propertyFilters;
  333. }
  334. public List<PropertyFilter> getPropertyFiltersDirect()
  335. {
  336. return this.propertyFilters;
  337. }
  338. public List<PropertyPreFilter> getPropertyPreFilters()
  339. {
  340. if (this.propertyPreFilters == null)
  341. this.propertyPreFilters = new ArrayList();
  342. return this.propertyPreFilters;
  343. }
  344. public List<PropertyPreFilter> getPropertyPreFiltersDirect()
  345. {
  346. return this.propertyPreFilters;
  347. }
  348. public SerialContext getSerialContext(Object paramObject)
  349. {
  350. if (this.references == null)
  351. return null;
  352. return (SerialContext)this.references.get(paramObject);
  353. }
  354. public List<ValueFilter> getValueFilters()
  355. {
  356. if (this.valueFilters == null)
  357. this.valueFilters = new ArrayList();
  358. return this.valueFilters;
  359. }
  360. public List<ValueFilter> getValueFiltersDirect()
  361. {
  362. return this.valueFilters;
  363. }
  364. public SerializeWriter getWriter()
  365. {
  366. return this.out;
  367. }
  368. public void incrementIndent()
  369. {
  370. this.indentCount += 1;
  371. }
  372. public boolean isEnabled(SerializerFeature paramSerializerFeature)
  373. {
  374. return this.out.isEnabled(paramSerializerFeature);
  375. }
  376. public final boolean isWriteAsArray(Object paramObject, Type paramType)
  377. {
  378. return this.out.isEnabled(SerializerFeature.BeanToArray);
  379. }
  380. public final boolean isWriteClassName(Type paramType, Object paramObject)
  381. {
  382. if (!this.out.isEnabled(SerializerFeature.WriteClassName));
  383. while (true)
  384. {
  385. return false;
  386. if ((paramType == null) && (isEnabled(SerializerFeature.NotWriteRootClassName)))
  387. if (this.context.getParent() != null)
  388. break label47;
  389. label47: for (int i = 1; i == 0; i = 0)
  390. return true;
  391. }
  392. }
  393. public void popContext()
  394. {
  395. if (this.context != null)
  396. this.context = this.context.getParent();
  397. }
  398. public void println()
  399. {
  400. this.out.write('\n');
  401. int i = 0;
  402. while (i < this.indentCount)
  403. {
  404. this.out.write(this.indent);
  405. i += 1;
  406. }
  407. }
  408. public void setContext(SerialContext paramSerialContext)
  409. {
  410. this.context = paramSerialContext;
  411. }
  412. public void setContext(SerialContext paramSerialContext, Object paramObject1, Object paramObject2)
  413. {
  414. if (isEnabled(SerializerFeature.DisableCircularReferenceDetect))
  415. return;
  416. this.context = new SerialContext(paramSerialContext, paramObject1, paramObject2);
  417. if (this.references == null)
  418. this.references = new IdentityHashMap();
  419. this.references.put(paramObject1, this.context);
  420. }
  421. public void setContext(Object paramObject1, Object paramObject2)
  422. {
  423. setContext(this.context, paramObject1, paramObject2);
  424. }
  425. public void setDateFormat(String paramString)
  426. {
  427. this.dateFormatPattern = paramString;
  428. if (this.dateFormat != null)
  429. this.dateFormat = null;
  430. }
  431. public void setDateFormat(DateFormat paramDateFormat)
  432. {
  433. this.dateFormat = paramDateFormat;
  434. if (this.dateFormatPattern != null)
  435. this.dateFormatPattern = null;
  436. }
  437. public String toString()
  438. {
  439. return this.out.toString();
  440. }
  441. public final void write(Object paramObject)
  442. {
  443. if (paramObject == null)
  444. {
  445. this.out.writeNull();
  446. return;
  447. }
  448. ObjectSerializer localObjectSerializer = getObjectWriter(paramObject.getClass());
  449. try
  450. {
  451. localObjectSerializer.write(this, paramObject, null, null);
  452. return;
  453. }
  454. catch (IOException paramObject)
  455. {
  456. }
  457. throw new JSONException(paramObject.getMessage(), paramObject);
  458. }
  459. public final void write(String paramString)
  460. {
  461. StringSerializer.instance.write(this, paramString);
  462. }
  463. protected final void writeKeyValue(char paramChar, String paramString, Object paramObject)
  464. {
  465. if (paramChar != 0)
  466. this.out.write(paramChar);
  467. this.out.writeFieldName(paramString);
  468. write(paramObject);
  469. }
  470. public void writeNull()
  471. {
  472. this.out.writeNull();
  473. }
  474. public void writeReference(Object paramObject)
  475. {
  476. SerialContext localSerialContext1 = getContext();
  477. if (paramObject == localSerialContext1.getObject())
  478. {
  479. this.out.write("{\"$ref\":\"@\"}");
  480. return;
  481. }
  482. SerialContext localSerialContext2 = localSerialContext1.getParent();
  483. if ((localSerialContext2 != null) && (paramObject == localSerialContext2.getObject()))
  484. {
  485. this.out.write("{\"$ref\":\"..\"}");
  486. return;
  487. }
  488. while (true)
  489. {
  490. if (localSerialContext1.getParent() == null)
  491. {
  492. if (paramObject != localSerialContext1.getObject())
  493. break;
  494. this.out.write("{\"$ref\":\"$\"}");
  495. return;
  496. }
  497. localSerialContext1 = localSerialContext1.getParent();
  498. }
  499. paramObject = getSerialContext(paramObject).getPath();
  500. this.out.write("{\"$ref\":\"");
  501. this.out.write(paramObject);
  502. this.out.write("\"}");
  503. }
  504. public final void writeWithFieldName(Object paramObject1, Object paramObject2)
  505. {
  506. writeWithFieldName(paramObject1, paramObject2, null);
  507. }
  508. public final void writeWithFieldName(Object paramObject1, Object paramObject2, Type paramType)
  509. {
  510. if (paramObject1 == null);
  511. try
  512. {
  513. this.out.writeNull();
  514. return;
  515. getObjectWriter(paramObject1.getClass()).write(this, paramObject1, paramObject2, paramType);
  516. return;
  517. }
  518. catch (IOException paramObject1)
  519. {
  520. }
  521. throw new JSONException(paramObject1.getMessage(), paramObject1);
  522. }
  523. public final void writeWithFormat(Object paramObject, String paramString)
  524. {
  525. if ((paramObject instanceof Date))
  526. {
  527. DateFormat localDateFormat = getDateFormat();
  528. Object localObject = localDateFormat;
  529. if (localDateFormat == null)
  530. localObject = new SimpleDateFormat(paramString);
  531. paramObject = ((DateFormat)localObject).format((Date)paramObject);
  532. this.out.writeString(paramObject);
  533. return;
  534. }
  535. write(paramObject);
  536. }
  537. }
  538. /* Location: C:\Users\User\dex2jar-2.0\dex\qting\classes-dex2jar.jar
  539. * Qualified Name: com.alibaba.fastjson.serializer.JSONSerializer
  540. * JD-Core Version: 0.6.2
  541. */