JSONObject.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * Copyright 1999-2017 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;
  17. import com.alibaba.fastjson.annotation.JSONField;
  18. import com.alibaba.fastjson.parser.Feature;
  19. import com.alibaba.fastjson.parser.ParserConfig;
  20. import com.alibaba.fastjson.util.TypeUtils;
  21. import common.YosException;
  22. import common.data.Row;
  23. import common.data.Rows;
  24. import common.data.db.DBConnect;
  25. import java.io.*;
  26. import java.lang.reflect.Field;
  27. import java.lang.reflect.InvocationHandler;
  28. import java.lang.reflect.Method;
  29. import java.lang.reflect.Type;
  30. import java.math.BigDecimal;
  31. import java.math.BigInteger;
  32. import java.text.ParseException;
  33. import java.text.SimpleDateFormat;
  34. import java.util.*;
  35. import static com.alibaba.fastjson.util.TypeUtils.*;
  36. /**
  37. * @author wenshao[szujobs@hotmail.com]
  38. */
  39. public class JSONObject extends JSON implements Map<String, Object>, Cloneable, Serializable, InvocationHandler {
  40. private static final long serialVersionUID = 1L;
  41. private static final int DEFAULT_INITIAL_CAPACITY = 16;
  42. private final Map<String, Object> map;
  43. public JSONObject() {
  44. this(DEFAULT_INITIAL_CAPACITY, false);
  45. }
  46. public JSONObject(Map<String, Object> map) {
  47. if (map == null) {
  48. throw new IllegalArgumentException("map is null.");
  49. }
  50. this.map = map;
  51. }
  52. public JSONObject(boolean ordered) {
  53. this(DEFAULT_INITIAL_CAPACITY, ordered);
  54. }
  55. public JSONObject(int initialCapacity) {
  56. this(initialCapacity, false);
  57. }
  58. public JSONObject(int initialCapacity, boolean ordered) {
  59. if (ordered) {
  60. map = new LinkedHashMap<String, Object>(initialCapacity);
  61. } else {
  62. map = new HashMap<String, Object>(initialCapacity);
  63. }
  64. }
  65. public int size() {
  66. return map.size();
  67. }
  68. public boolean isEmpty() {
  69. return map.isEmpty();
  70. }
  71. public boolean containsKey(Object key) {
  72. return map.containsKey(key);
  73. }
  74. public boolean containsValue(Object value) {
  75. return map.containsValue(value);
  76. }
  77. public Object get(Object key) {
  78. Object val = map.get(key);
  79. if (val == null && key instanceof Number) {
  80. val = map.get(key.toString());
  81. }
  82. return val;
  83. }
  84. public JSONObject getJSONObject(String key) throws YosException {
  85. fieldContainsCheck(key, "JSONObject");
  86. Object value = map.get(key);
  87. if (value instanceof JSONObject) {
  88. return (JSONObject) value;
  89. }
  90. if (value instanceof Map) {
  91. return new JSONObject((Map) value);
  92. }
  93. if (value instanceof String) {
  94. return JSON.parseObject((String) value);
  95. }
  96. return (JSONObject) toJSON(value);
  97. }
  98. public JSONArray getJSONArray(String key) throws YosException {
  99. fieldContainsCheck(key, "JSONArray");
  100. Object value = map.get(key);
  101. if (value instanceof JSONArray) {
  102. return (JSONArray) value;
  103. }
  104. if (value instanceof List) {
  105. return new JSONArray((List) value);
  106. }
  107. if (value instanceof String) {
  108. return (JSONArray) JSON.parse((String) value);
  109. }
  110. return (JSONArray) toJSON(value);
  111. }
  112. public <T> T getObject(String key, Class<T> clazz) throws YosException {
  113. fieldContainsCheck(key, clazz.getSimpleName());
  114. Object obj = map.get(key);
  115. return TypeUtils.castToJavaBean(obj, clazz);
  116. }
  117. public <T> T getObject(String key, Type type) throws YosException {
  118. fieldContainsCheck(key, type.getTypeName());
  119. Object obj = map.get(key);
  120. return TypeUtils.cast(obj, type, ParserConfig.getGlobalInstance());
  121. }
  122. public <T> T getObject(String key, TypeReference typeReference) throws YosException {
  123. fieldContainsCheck(key, typeReference.getType().getTypeName());
  124. Object obj = map.get(key);
  125. if (typeReference == null) {
  126. return (T) obj;
  127. }
  128. return TypeUtils.cast(obj, typeReference.getType(), ParserConfig.getGlobalInstance());
  129. }
  130. public Boolean getBoolean(String key) throws YosException {
  131. fieldContainsCheck(key, "Boolean");
  132. Object value = get(key);
  133. if (value == null) {
  134. return null;
  135. }
  136. return castToBoolean(value);
  137. }
  138. public byte[] getBytes(String key) throws YosException {
  139. fieldContainsCheck(key, "Bytes");
  140. Object value = get(key);
  141. if (value == null) {
  142. return null;
  143. }
  144. return castToBytes(value);
  145. }
  146. public boolean getBooleanValue(String key) {
  147. Object value = get(key);
  148. Boolean booleanVal = castToBoolean(value);
  149. if (booleanVal == null) {
  150. return false;
  151. }
  152. return booleanVal.booleanValue();
  153. }
  154. public Byte getByte(String key) throws YosException {
  155. fieldContainsCheck(key, "Bytes");
  156. Object value = get(key);
  157. return castToByte(value);
  158. }
  159. public byte getByteValue(String key) {
  160. Object value = get(key);
  161. Byte byteVal = castToByte(value);
  162. if (byteVal == null) {
  163. return 0;
  164. }
  165. return byteVal.byteValue();
  166. }
  167. public Short getShort(String key) throws YosException {
  168. fieldContainsCheck(key, "Short");
  169. Object value = get(key);
  170. return castToShort(value);
  171. }
  172. public short getShortValue(String key) {
  173. Object value = get(key);
  174. Short shortVal = castToShort(value);
  175. if (shortVal == null) {
  176. return 0;
  177. }
  178. return shortVal.shortValue();
  179. }
  180. public Integer getInteger(String key) throws YosException {
  181. fieldContainsCheck(key, "Integer");
  182. Object value = get(key);
  183. return castToInt(value);
  184. }
  185. public int getIntValue(String key) {
  186. Object value = get(key);
  187. Integer intVal = castToInt(value);
  188. if (intVal == null) {
  189. return 0;
  190. }
  191. return intVal.intValue();
  192. }
  193. public int getIntValue(String key, int defaultvalue) {
  194. Object value = get(key);
  195. Integer intVal = castToInt(value);
  196. if (intVal == null) {
  197. return defaultvalue;
  198. }
  199. return intVal.intValue();
  200. }
  201. public Long getLong(String key) throws YosException {
  202. if (isTableUniqueColumnName(key)) {
  203. return getLongValue(key, -1L);
  204. } else {
  205. fieldContainsCheck(key, "Long");
  206. Object value = get(key);
  207. return castToLong(value);
  208. }
  209. }
  210. /**
  211. * 检查是否为表id字段
  212. *
  213. * @param key
  214. * @return
  215. */
  216. private static boolean isTableUniqueColumnName(String key) {
  217. try {
  218. if (tableuniquecolumnnameList.isEmpty()) {
  219. tableuniquecolumnnameList = new DBConnect().runSqlQuery("select uniquecolumnname from sys_object").toArrayList("uniquecolumnname");
  220. }
  221. return tableuniquecolumnnameList.contains(key);
  222. } catch (Exception e) {
  223. return false;
  224. }
  225. }
  226. private static ArrayList<String> tableuniquecolumnnameList = new ArrayList<>();
  227. public long getLongValue(String key) {
  228. Object value = get(key);
  229. Long longVal = castToLong(value);
  230. if (longVal == null) {
  231. return 0L;
  232. }
  233. return longVal.longValue();
  234. }
  235. public long getLongValue(String key, Long defaultvalue) {
  236. Object value = get(key);
  237. Long longVal = castToLong(value);
  238. if (longVal == null) {
  239. return defaultvalue;
  240. }
  241. return longVal.longValue();
  242. }
  243. public Float getFloat(String key) throws YosException {
  244. fieldContainsCheck(key, "Float");
  245. Object value = get(key);
  246. return castToFloat(value);
  247. }
  248. public float getFloatValue(String key) {
  249. Object value = get(key);
  250. Float floatValue = castToFloat(value);
  251. if (floatValue == null) {
  252. return 0F;
  253. }
  254. return floatValue.floatValue();
  255. }
  256. public Double getDouble(String key) throws YosException {
  257. fieldContainsCheck(key, "Double");
  258. Object value = get(key);
  259. return castToDouble(value);
  260. }
  261. public double getDoubleValue(String key) {
  262. Object value = get(key);
  263. Double doubleValue = castToDouble(value);
  264. if (doubleValue == null) {
  265. return 0D;
  266. }
  267. return doubleValue.doubleValue();
  268. }
  269. public BigDecimal getBigDecimal(String key) throws YosException {
  270. fieldContainsCheck(key, "BigDecimal");
  271. Object value = get(key);
  272. return castToBigDecimal(value);
  273. }
  274. public BigDecimal getBigDecimalValue(String key) throws YosException {
  275. Object value = get(key);
  276. if (value == null || value.equals("")) {
  277. return castToBigDecimal(0);
  278. }
  279. fieldContainsCheck(key, "BigDecimal");
  280. return castToBigDecimal(value);
  281. }
  282. public BigDecimal getBigDecimalValue(String key, int defaultvalue) throws YosException {
  283. Object value = get(key);
  284. if (value == null || value.equals("")) {
  285. return castToBigDecimal(defaultvalue);
  286. }
  287. fieldContainsCheck(key, "BigDecimal");
  288. return castToBigDecimal(value);
  289. }
  290. public BigInteger getBigInteger(String key) throws YosException {
  291. fieldContainsCheck(key, "BigInteger");
  292. Object value = get(key);
  293. return castToBigInteger(value);
  294. }
  295. public String getStringValue(String key) {
  296. return getStringValue(key, false);
  297. }
  298. public String getStringValue(String key, boolean Keepspecialcharacters, String defValue) {
  299. Object value = get(key);
  300. if (value == null || value.toString().isEmpty()) {
  301. return defValue;
  302. }
  303. if (!Keepspecialcharacters) {
  304. value = value.toString().replaceAll("([';])+|(--)+", "");//去除特殊字符,防止sql注入
  305. } else {
  306. value = value.toString().replace("'", "\\'");
  307. }
  308. return value.toString();
  309. }
  310. /**
  311. * 验证是否为时间格式
  312. *
  313. * @param key
  314. * @param pattern
  315. * @param defValue
  316. * @return
  317. */
  318. public String getStringValueForDate(String key, String pattern, String defValue) {
  319. Object value = get(key);
  320. if (value == null || value.toString().isEmpty()) {
  321. return defValue;
  322. }
  323. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  324. try {
  325. sdf.parse(value.toString());
  326. } catch (ParseException e) {
  327. e.printStackTrace();
  328. return defValue;
  329. }
  330. return value.toString();
  331. }
  332. public String getStringValue(String key, boolean Keepspecialcharacters) {
  333. Object value = get(key);
  334. if (value == null) {
  335. return "";
  336. }
  337. if (!Keepspecialcharacters) {
  338. value = value.toString().replaceAll("([';])+|(--)+", "");//去除特殊字符,防止sql注入
  339. } else {
  340. value = value.toString().replace("'", "\\'");
  341. }
  342. return value.toString();
  343. }
  344. public String getString(String key) throws YosException {
  345. return getString(key, false);
  346. }
  347. public String getString(String key, boolean Keepspecialcharacters) throws YosException {
  348. fieldContainsCheck(key, "String");
  349. Object value = get(key);
  350. if (value == null) {
  351. return null;
  352. }
  353. if (!Keepspecialcharacters) {
  354. value = value.toString().replaceAll("([';])+|(--)+", "");//去除特殊字符,防止sql注入
  355. } else {
  356. value = value.toString().replace("'", "\\'");
  357. }
  358. return value.toString();
  359. }
  360. public static HashMap<String, HashMap<String, Integer>> table_field_length_Map = new HashMap<String, HashMap<String, Integer>>();
  361. /**
  362. * 获取String类型的值,并进行长度校验
  363. *
  364. * @param key
  365. * @param table_fieldname 数据库表名.字段名
  366. * @return
  367. */
  368. public String getString(String key, String table_fieldname) throws YosException {
  369. return getString(key, table_fieldname, false);
  370. }
  371. public String getStringValue(String key, String table_fieldname) throws YosException {
  372. Object value = get(key);
  373. if (value == null) {
  374. return "";
  375. }
  376. return getString(key, table_fieldname, false);
  377. }
  378. /**
  379. * 获取String类型的值,并进行长度校验
  380. *
  381. * @param key
  382. * @param table_fieldname 数据库表名.字段名,如果字段名和key值相同,则可省略字段名
  383. * @return
  384. */
  385. public String getString(String key, String table_fieldname, boolean Keepspecialcharacters) throws YosException {
  386. String[] split = table_fieldname.toUpperCase().split("[.]");
  387. String tablename = split[0];
  388. if (!table_field_length_Map.containsKey(tablename)) {
  389. HashMap<String, Integer> map = new HashMap<String, Integer>();
  390. DBConnect dbConnect = new DBConnect();
  391. Rows rows = dbConnect.runSqlQuery("select column_name,numeric_precision from sys_objectcols where table_name='" + tablename + "'");
  392. for (Row row : rows) {
  393. map.put(row.getString("column_name").toUpperCase(), row.getInteger("numeric_precision"));
  394. }
  395. table_field_length_Map.put(tablename, map);
  396. }
  397. String fieldname = split.length == 1 ? key.toUpperCase() : split[1];
  398. int maxlength = table_field_length_Map.get(tablename).getOrDefault(fieldname, 0);
  399. return getString(key, maxlength, Keepspecialcharacters);
  400. }
  401. public String getStringValue(String key, String table_fieldname, boolean Keepspecialcharacters) throws YosException {
  402. Object value = get(key);
  403. if (value == null) {
  404. return "";
  405. }
  406. return getString(key, table_fieldname, Keepspecialcharacters);
  407. }
  408. /**
  409. * 获取String类型的值,并进行长度校验
  410. *
  411. * @param key
  412. * @param maxlength 最大长度限制
  413. * @return
  414. */
  415. public String getString(String key, int maxlength) throws YosException {
  416. return getString(key, maxlength, false);
  417. }
  418. public String getStringValue(String key, int maxlength) throws YosException {
  419. return getStringValue(key, maxlength, false);
  420. }
  421. /**
  422. * 获取String类型的值,并进行长度校验
  423. *
  424. * @param key
  425. * @param maxlength 最大长度限制
  426. * @return
  427. */
  428. public String getString(String key, int maxlength, boolean Keepspecialcharacters) throws YosException {
  429. fieldContainsCheck(key, "String");
  430. String fieldvalue = getString(key);
  431. if (fieldvalue != null) {
  432. if (maxlength > 0 && fieldvalue.length() > maxlength) {
  433. throw new YosException("[" + key + "] 内容超过最大字数" + maxlength + "限制");
  434. }
  435. if (!Keepspecialcharacters) {
  436. fieldvalue = fieldvalue.replaceAll("([';])+|(--)+", "");//去除特殊字符,防止sql注入
  437. }
  438. }
  439. return fieldvalue;
  440. }
  441. /**
  442. * 获取String类型的值,并进行长度校验
  443. *
  444. * @param key
  445. * @param maxlength 最大长度限制
  446. * @return
  447. */
  448. public String getStringValue(String key, int maxlength, boolean Keepspecialcharacters) throws YosException {
  449. Object value = get(key);
  450. if (value == null) {
  451. return "";
  452. }
  453. return getString(key, maxlength, Keepspecialcharacters);
  454. }
  455. public Date getDate(String key) throws YosException {
  456. fieldContainsCheck(key, "Date");
  457. Object value = get(key);
  458. return castToDate(value);
  459. }
  460. public java.sql.Date getSqlDate(String key) throws YosException {
  461. fieldContainsCheck(key, "SqlDate");
  462. Object value = get(key);
  463. return castToSqlDate(value);
  464. }
  465. public java.sql.Timestamp getTimestamp(String key) throws YosException {
  466. fieldContainsCheck(key, "Timestamp");
  467. Object value = get(key);
  468. return castToTimestamp(value);
  469. }
  470. public Object put(String key, Object value) {
  471. return map.put(key, value);
  472. }
  473. public JSONObject fluentPut(String key, Object value) {
  474. map.put(key, value);
  475. return this;
  476. }
  477. public void putAll(Map<? extends String, ? extends Object> m) {
  478. map.putAll(m);
  479. }
  480. public JSONObject fluentPutAll(Map<? extends String, ? extends Object> m) {
  481. map.putAll(m);
  482. return this;
  483. }
  484. public void clear() {
  485. map.clear();
  486. }
  487. public JSONObject fluentClear() {
  488. map.clear();
  489. return this;
  490. }
  491. public Object remove(Object key) {
  492. return map.remove(key);
  493. }
  494. public JSONObject fluentRemove(Object key) {
  495. map.remove(key);
  496. return this;
  497. }
  498. public Set<String> keySet() {
  499. return map.keySet();
  500. }
  501. public Collection<Object> values() {
  502. return map.values();
  503. }
  504. public Set<Entry<String, Object>> entrySet() {
  505. return map.entrySet();
  506. }
  507. @Override
  508. public Object clone() {
  509. return new JSONObject(map instanceof LinkedHashMap //
  510. ? new LinkedHashMap<String, Object>(map) //
  511. : new HashMap<String, Object>(map)
  512. );
  513. }
  514. public boolean equals(Object obj) {
  515. return this.map.equals(obj);
  516. }
  517. public int hashCode() {
  518. return this.map.hashCode();
  519. }
  520. public Object invoke(Object proxy, Method method, Object[] args) {
  521. Class<?>[] parameterTypes = method.getParameterTypes();
  522. if (parameterTypes.length == 1) {
  523. if (method.getName().equals("equals")) {
  524. return this.equals(args[0]);
  525. }
  526. Class<?> returnType = method.getReturnType();
  527. if (returnType != void.class) {
  528. throw new JSONException("illegal setter");
  529. }
  530. String name = null;
  531. JSONField annotation = TypeUtils.getAnnotation(method, JSONField.class);
  532. if (annotation != null) {
  533. if (annotation.name().length() != 0) {
  534. name = annotation.name();
  535. }
  536. }
  537. if (name == null) {
  538. name = method.getName();
  539. if (!name.startsWith("set")) {
  540. throw new JSONException("illegal setter");
  541. }
  542. name = name.substring(3);
  543. if (name.length() == 0) {
  544. throw new JSONException("illegal setter");
  545. }
  546. name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
  547. }
  548. map.put(name, args[0]);
  549. return null;
  550. }
  551. if (parameterTypes.length == 0) {
  552. Class<?> returnType = method.getReturnType();
  553. if (returnType == void.class) {
  554. throw new JSONException("illegal getter");
  555. }
  556. String name = null;
  557. JSONField annotation = TypeUtils.getAnnotation(method, JSONField.class);
  558. if (annotation != null) {
  559. if (annotation.name().length() != 0) {
  560. name = annotation.name();
  561. }
  562. }
  563. if (name == null) {
  564. name = method.getName();
  565. if (name.startsWith("get")) {
  566. name = name.substring(3);
  567. if (name.length() == 0) {
  568. throw new JSONException("illegal getter");
  569. }
  570. name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
  571. } else if (name.startsWith("is")) {
  572. name = name.substring(2);
  573. if (name.length() == 0) {
  574. throw new JSONException("illegal getter");
  575. }
  576. name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
  577. } else if (name.startsWith("hashCode")) {
  578. return this.hashCode();
  579. } else if (name.startsWith("toString")) {
  580. return this.toString();
  581. } else {
  582. throw new JSONException("illegal getter");
  583. }
  584. }
  585. Object value = map.get(name);
  586. return TypeUtils.cast(value, method.getGenericReturnType(), ParserConfig.getGlobalInstance());
  587. }
  588. throw new UnsupportedOperationException(method.toGenericString());
  589. }
  590. public Map<String, Object> getInnerMap() {
  591. return this.map;
  592. }
  593. private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  594. SecureObjectInputStream.ensureFields();
  595. if (SecureObjectInputStream.fields != null && !SecureObjectInputStream.fields_error) {
  596. ObjectInputStream secIn = new SecureObjectInputStream(in);
  597. try {
  598. secIn.defaultReadObject();
  599. return;
  600. } catch (NotActiveException e) {
  601. // skip
  602. }
  603. }
  604. in.defaultReadObject();
  605. for (Entry entry : map.entrySet()) {
  606. final Object key = entry.getKey();
  607. if (key != null) {
  608. ParserConfig.global.checkAutoType(key.getClass());
  609. }
  610. final Object value = entry.getValue();
  611. if (value != null) {
  612. ParserConfig.global.checkAutoType(value.getClass());
  613. }
  614. }
  615. }
  616. static class SecureObjectInputStream extends ObjectInputStream {
  617. static Field[] fields;
  618. static volatile boolean fields_error;
  619. static void ensureFields() {
  620. if (fields == null && !fields_error) {
  621. try {
  622. final Field[] declaredFields = ObjectInputStream.class.getDeclaredFields();
  623. String[] fieldnames = new String[]{"bin", "passHandle", "handles", "curContext"};
  624. Field[] array = new Field[fieldnames.length];
  625. for (int i = 0; i < fieldnames.length; i++) {
  626. Field field = TypeUtils
  627. .getField(ObjectInputStream.class
  628. , fieldnames[i]
  629. , declaredFields
  630. );
  631. field.setAccessible(true);
  632. array[i] = field;
  633. }
  634. fields = array;
  635. } catch (Throwable error) {
  636. fields_error = true;
  637. }
  638. }
  639. }
  640. public SecureObjectInputStream(ObjectInputStream in) throws IOException {
  641. super(in);
  642. try {
  643. for (int i = 0; i < fields.length; i++) {
  644. final Field field = fields[i];
  645. final Object value = field.get(in);
  646. field.set(this, value);
  647. }
  648. } catch (IllegalAccessException e) {
  649. fields_error = true;
  650. }
  651. }
  652. protected Class<?> resolveClass(ObjectStreamClass desc)
  653. throws IOException, ClassNotFoundException {
  654. String name = desc.getName();
  655. if (name.length() > 2) {
  656. int index = name.lastIndexOf('[');
  657. if (index != -1) {
  658. name = name.substring(index + 1);
  659. }
  660. if (name.length() > 2 && name.charAt(0) == 'L' && name.charAt(name.length() - 1) == ';') {
  661. name = name.substring(1, name.length() - 1);
  662. }
  663. ParserConfig.global.checkAutoType(name, null, Feature.SupportAutoType.mask);
  664. }
  665. return super.resolveClass(desc);
  666. }
  667. protected Class<?> resolveProxyClass(String[] interfaces)
  668. throws IOException, ClassNotFoundException {
  669. for (String interfacename : interfaces) {
  670. //检查是否处于黑名单
  671. ParserConfig.global.checkAutoType(interfacename, null);
  672. }
  673. return super.resolveProxyClass(interfaces);
  674. }
  675. //Hack:默认构造方法会调用这个方法,重写此方法使用反射还原部分关键属性
  676. protected void readStreamHeader() {
  677. }
  678. }
  679. public <T> T toJavaObject(Class<T> clazz) {
  680. if (clazz == Map.class || clazz == JSONObject.class || clazz == JSON.class) {
  681. return (T) this;
  682. }
  683. if (clazz == Object.class && !containsKey(JSON.DEFAULT_TYPE_KEY)) {
  684. return (T) this;
  685. }
  686. return TypeUtils.castToJavaBean(this, clazz, ParserConfig.getGlobalInstance());
  687. }
  688. public <T> T toJavaObject(Class<T> clazz, ParserConfig config, int features) {
  689. if (clazz == Map.class) {
  690. return (T) this;
  691. }
  692. if (clazz == Object.class && !containsKey(JSON.DEFAULT_TYPE_KEY)) {
  693. return (T) this;
  694. }
  695. return TypeUtils.castToJavaBean(this, clazz, config);
  696. }
  697. /**
  698. * 字段栏位是否存在判断
  699. *
  700. * @param key
  701. * @throws YosException
  702. */
  703. public void fieldContainsCheck(String key, String classname) throws YosException {
  704. if (!this.containsKey(key)) {
  705. throw new YosException(this.getClass().getSimpleName() + "对象中找不到名为" + key + "的" + classname + "类型参数信息");
  706. }
  707. }
  708. }