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

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 175 lines | 127 code | 24 blank | 24 comment | 12 complexity | cd58d81620a8e56858c11e8acde985ed 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.File;
  18. import java.io.Serializable;
  19. import java.lang.reflect.Modifier;
  20. import java.lang.reflect.Type;
  21. import java.math.BigDecimal;
  22. import java.math.BigInteger;
  23. import java.net.Inet4Address;
  24. import java.net.Inet6Address;
  25. import java.net.InetAddress;
  26. import java.net.InetSocketAddress;
  27. import java.net.URI;
  28. import java.net.URL;
  29. import java.nio.charset.Charset;
  30. import java.text.SimpleDateFormat;
  31. import java.util.Locale;
  32. import java.util.TimeZone;
  33. import java.util.UUID;
  34. import java.util.concurrent.atomic.AtomicBoolean;
  35. import java.util.concurrent.atomic.AtomicInteger;
  36. import java.util.concurrent.atomic.AtomicIntegerArray;
  37. import java.util.concurrent.atomic.AtomicLong;
  38. import java.util.concurrent.atomic.AtomicLongArray;
  39. import java.util.concurrent.atomic.AtomicReference;
  40. import java.util.regex.Pattern;
  41. import com.alibaba.fastjson.JSONException;
  42. import com.alibaba.fastjson.annotation.JSONType;
  43. import com.alibaba.fastjson.util.ASMUtils;
  44. import com.alibaba.fastjson.util.IdentityHashMap;
  45. /**
  46. * circular references detect
  47. *
  48. * @author wenshao<szujobs@hotmail.com>
  49. */
  50. public class SerializeConfig extends IdentityHashMap<Type, ObjectSerializer> {
  51. private final static SerializeConfig globalInstance = new SerializeConfig();
  52. private boolean asm = !ASMUtils.isAndroid(); ;
  53. private final ASMSerializerFactory asmFactory = new ASMSerializerFactory();
  54. public final ObjectSerializer createASMSerializer(Class<?> clazz) throws Exception {
  55. return asmFactory.createJavaBeanSerializer(clazz);
  56. }
  57. public ObjectSerializer createJavaBeanSerializer(Class<?> clazz) {
  58. if (!Modifier.isPublic(clazz.getModifiers())) {
  59. return new JavaBeanSerializer(clazz);
  60. }
  61. boolean asm = this.asm;
  62. if (asm && asmFactory.isExternalClass(clazz) || clazz == Serializable.class || clazz == Object.class) {
  63. asm = false;
  64. }
  65. {
  66. JSONType annotation = clazz.getAnnotation(JSONType.class);
  67. if (annotation != null && annotation.asm() == false) {
  68. asm = false;
  69. }
  70. }
  71. if (asm) {
  72. try {
  73. return createASMSerializer(clazz);
  74. } catch (ClassCastException e) {
  75. // skip
  76. return new JavaBeanSerializer(clazz);
  77. } catch (Throwable e) {
  78. throw new JSONException("create asm serializer error, class " + clazz, e);
  79. }
  80. }
  81. return new JavaBeanSerializer(clazz);
  82. }
  83. public boolean isAsmEnable() {
  84. return asm;
  85. }
  86. public void setAsmEnable(boolean asmEnable) {
  87. this.asm = asmEnable;
  88. }
  89. public final static SerializeConfig getGlobalInstance() {
  90. return globalInstance;
  91. }
  92. public SerializeConfig(){
  93. this(DEFAULT_TABLE_SIZE);
  94. }
  95. public SerializeConfig(int tableSize){
  96. super(tableSize);
  97. put(Boolean.class, BooleanSerializer.instance);
  98. put(Character.class, CharacterSerializer.instance);
  99. put(Byte.class, ByteSerializer.instance);
  100. put(Short.class, ShortSerializer.instance);
  101. put(Integer.class, IntegerSerializer.instance);
  102. put(Long.class, LongSerializer.instance);
  103. put(Float.class, FloatSerializer.instance);
  104. put(Double.class, DoubleSerializer.instance);
  105. put(BigDecimal.class, BigDecimalSerializer.instance);
  106. put(BigInteger.class, BigIntegerSerializer.instance);
  107. put(String.class, StringSerializer.instance);
  108. put(byte[].class, ByteArraySerializer.instance);
  109. put(short[].class, ShortArraySerializer.instance);
  110. put(int[].class, IntArraySerializer.instance);
  111. put(long[].class, LongArraySerializer.instance);
  112. put(float[].class, FloatArraySerializer.instance);
  113. put(double[].class, DoubleArraySerializer.instance);
  114. put(boolean[].class, BooleanArraySerializer.instance);
  115. put(char[].class, CharArraySerializer.instance);
  116. put(Object[].class, ObjectArraySerializer.instance);
  117. put(Class.class, ClassSerializer.instance);
  118. put(SimpleDateFormat.class, DateFormatSerializer.instance);
  119. put(Locale.class, LocaleSerializer.instance);
  120. put(TimeZone.class, TimeZoneSerializer.instance);
  121. put(UUID.class, UUIDSerializer.instance);
  122. put(InetAddress.class, InetAddressSerializer.instance);
  123. put(Inet4Address.class, InetAddressSerializer.instance);
  124. put(Inet6Address.class, InetAddressSerializer.instance);
  125. put(InetSocketAddress.class, InetSocketAddressSerializer.instance);
  126. put(File.class, FileSerializer.instance);
  127. put(URI.class, URISerializer.instance);
  128. put(URL.class, URLSerializer.instance);
  129. put(Appendable.class, AppendableSerializer.instance);
  130. put(StringBuffer.class, AppendableSerializer.instance);
  131. put(StringBuilder.class, AppendableSerializer.instance);
  132. put(Pattern.class, PatternSerializer.instance);
  133. put(Charset.class, CharsetSerializer.instance);
  134. // atomic
  135. put(AtomicBoolean.class, AtomicBooleanSerializer.instance);
  136. put(AtomicInteger.class, AtomicIntegerSerializer.instance);
  137. put(AtomicLong.class, AtomicLongSerializer.instance);
  138. put(AtomicReference.class, AtomicReferenceSerializer.instance);
  139. put(AtomicIntegerArray.class, AtomicIntegerArraySerializer.instance);
  140. put(AtomicLongArray.class, AtomicLongArraySerializer.instance);
  141. // awt
  142. try {
  143. put(Class.forName("java.awt.Color"), ColorSerializer.instance);
  144. put(Class.forName("java.awt.Font"), FontSerializer.instance);
  145. put(Class.forName("java.awt.Point"), PointSerializer.instance);
  146. put(Class.forName("java.awt.Rectangle"), RectangleSerializer.instance);
  147. } catch (Throwable e) {
  148. // skip
  149. }
  150. }
  151. }