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

/sensei-core/src/main/java/com/senseidb/util/JSONUtil.java

https://bitbucket.org/icosplays/sensei
Java | 1092 lines | 696 code | 122 blank | 274 comment | 119 complexity | c022c39446875764b20495c3e6dfd59b MD5 | raw file
  1. package com.senseidb.util;
  2. import java.lang.reflect.Array;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.TreeSet;
  10. import com.alibaba.fastjson.JSON;
  11. import com.alibaba.fastjson.parser.ParserConfig;
  12. import com.alibaba.fastjson.util.FieldInfo;
  13. import com.alibaba.fastjson.util.TypeUtils;
  14. import org.json.JSONArray;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17. import proj.zoie.api.DataConsumer.DataEvent;
  18. public class JSONUtil
  19. {
  20. public static class FastJSONObject extends JSONObject
  21. {
  22. private com.alibaba.fastjson.JSONObject _inner;
  23. public FastJSONObject()
  24. {
  25. _inner = new com.alibaba.fastjson.JSONObject();
  26. }
  27. public FastJSONObject(String str) throws JSONException
  28. {
  29. try
  30. {
  31. _inner = (com.alibaba.fastjson.JSONObject)JSON.parse(str);
  32. }
  33. catch (Exception e)
  34. {
  35. throw new JSONException(e);
  36. }
  37. }
  38. public FastJSONObject(Map value)
  39. {
  40. if (value instanceof com.alibaba.fastjson.JSONObject)
  41. {
  42. _inner = (com.alibaba.fastjson.JSONObject)value;
  43. }
  44. else
  45. {
  46. _inner = (com.alibaba.fastjson.JSONObject)toJSON(value);
  47. }
  48. }
  49. public FastJSONObject(com.alibaba.fastjson.JSONObject inner)
  50. {
  51. _inner = inner;
  52. }
  53. public com.alibaba.fastjson.JSONObject getInnerJSONObject()
  54. {
  55. return _inner;
  56. }
  57. /**
  58. * Accumulate values under a key. It is similar to the put method except
  59. * that if there is already an object stored under the key then a
  60. * JSONArray is stored under the key to hold all of the accumulated values.
  61. * If there is already a JSONArray, then the new value is appended to it.
  62. * In contrast, the put method replaces the previous value.
  63. *
  64. * If only one value is accumulated that is not a JSONArray, then the
  65. * result will be the same as using put. But if multiple values are
  66. * accumulated, then the result will be like append.
  67. * @param key A key string.
  68. * @param value An object to be accumulated under the key.
  69. * @return this.
  70. * @throws JSONException If the value is an invalid number
  71. * or if the key is null.
  72. */
  73. public JSONObject accumulate(
  74. String key,
  75. Object value
  76. ) throws JSONException
  77. {
  78. Object object = _inner.get(key);
  79. if (object == null)
  80. {
  81. com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();
  82. array.add(value);
  83. _inner.put(key, array);
  84. }
  85. else if (object instanceof com.alibaba.fastjson.JSONArray)
  86. {
  87. ((com.alibaba.fastjson.JSONArray)object).add(value);
  88. }
  89. else
  90. {
  91. com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();
  92. array.add(object);
  93. array.add(value);
  94. _inner.put(key, array);
  95. }
  96. return this;
  97. }
  98. /**
  99. * Append values to the array under a key. If the key does not exist in the
  100. * JSONObject, then the key is put in the JSONObject with its value being a
  101. * JSONArray containing the value parameter. If the key was already
  102. * associated with a JSONArray, then the value parameter is appended to it.
  103. * @param key A key string.
  104. * @param value An object to be accumulated under the key.
  105. * @return this.
  106. * @throws JSONException If the key is null or if the current value
  107. * associated with the key is not a JSONArray.
  108. */
  109. public JSONObject append(String key, Object value) throws JSONException
  110. {
  111. Object object = _inner.get(key);
  112. if (object == null)
  113. {
  114. com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();
  115. array.add(value);
  116. _inner.put(key, array);
  117. }
  118. else if (object instanceof com.alibaba.fastjson.JSONArray)
  119. {
  120. ((com.alibaba.fastjson.JSONArray)object).add(value);
  121. }
  122. else
  123. {
  124. throw new JSONException("JSONObject[" + key +
  125. "] is not a JSONArray.");
  126. }
  127. return this;
  128. }
  129. /**
  130. * Get the value object associated with a key.
  131. *
  132. * @param key A key string.
  133. * @return The object associated with the key.
  134. * @throws JSONException if the key is not found.
  135. */
  136. public Object get(String key) throws JSONException
  137. {
  138. Object object = this.opt(key);
  139. if (object == null)
  140. {
  141. throw new JSONException("JSONObject[" + key +
  142. "] not found.");
  143. }
  144. return object;
  145. }
  146. /**
  147. * Get the JSONArray value associated with a key.
  148. *
  149. * @param key A key string.
  150. * @return A JSONArray which is the value.
  151. * @throws JSONException if the key is not found or
  152. * if the value is not a JSONArray.
  153. */
  154. public JSONArray getJSONArray(String key) throws JSONException
  155. {
  156. Object object = this.get(key);
  157. if (object instanceof com.alibaba.fastjson.JSONArray)
  158. {
  159. return new FastJSONArray((com.alibaba.fastjson.JSONArray)object);
  160. }
  161. else if (object instanceof Collection)
  162. {
  163. return new FastJSONArray((com.alibaba.fastjson.JSONArray)toJSON(object));
  164. }
  165. else if (object instanceof JSONArray) {
  166. return (JSONArray)object;
  167. }
  168. throw new JSONException("JSONObject[" + quote(key) +
  169. "] is not a JSONArray.");
  170. }
  171. /**
  172. * Get the JSONObject value associated with a key.
  173. *
  174. * @param key A key string.
  175. * @return A JSONObject which is the value.
  176. * @throws JSONException if the key is not found or
  177. * if the value is not a JSONObject.
  178. */
  179. public JSONObject getJSONObject(String key) throws JSONException
  180. {
  181. Object object = this.get(key);
  182. if (object instanceof com.alibaba.fastjson.JSONObject)
  183. {
  184. return new FastJSONObject((com.alibaba.fastjson.JSONObject)object);
  185. }
  186. else if (object instanceof Map)
  187. {
  188. return new FastJSONObject((com.alibaba.fastjson.JSONObject)toJSON(object));
  189. }
  190. else if (object instanceof JSONObject) {
  191. return (JSONObject)object;
  192. }
  193. throw new JSONException("JSONObject[" + quote(key) +
  194. "] is not a JSONObject.");
  195. }
  196. /**
  197. * Determine if the JSONObject contains a specific key.
  198. * @param key A key string.
  199. * @return true if the key exists in the JSONObject.
  200. */
  201. public boolean has(String key) {
  202. return _inner.containsKey(key);
  203. }
  204. /**
  205. * Get an enumeration of the keys of the JSONObject.
  206. *
  207. * @return An iterator of the keys.
  208. */
  209. public Iterator keys() {
  210. return _inner.keySet().iterator();
  211. }
  212. public Iterator sortedKeys() {
  213. return new TreeSet(_inner.keySet()).iterator();
  214. }
  215. /**
  216. * Get the number of keys stored in the JSONObject.
  217. *
  218. * @return The number of keys in the JSONObject.
  219. */
  220. public int length() {
  221. return _inner.size();
  222. }
  223. /**
  224. * Produce a JSONArray containing the names of the elements of this
  225. * JSONObject.
  226. * @return A JSONArray containing the key strings, or null if the JSONObject
  227. * is empty.
  228. */
  229. public JSONArray names() {
  230. JSONArray ja = new FastJSONArray();
  231. Iterator keys = this.keys();
  232. while (keys.hasNext()) {
  233. ja.put(keys.next());
  234. }
  235. return ja.length() == 0 ? null : ja;
  236. }
  237. /**
  238. * Get an optional value associated with a key.
  239. * @param key A key string.
  240. * @return An object which is the value, or null if there is no value.
  241. */
  242. public Object opt(String key) {
  243. if (key == null)
  244. {
  245. return null;
  246. }
  247. Object object = _inner.get(key);
  248. if (object == null)
  249. {
  250. return null;
  251. }
  252. else if (object instanceof com.alibaba.fastjson.JSONObject)
  253. {
  254. return new FastJSONObject((com.alibaba.fastjson.JSONObject)object);
  255. }
  256. else if (object instanceof com.alibaba.fastjson.JSONArray)
  257. {
  258. return new FastJSONArray((com.alibaba.fastjson.JSONArray)object);
  259. }
  260. else if (object instanceof Map)
  261. {
  262. return new FastJSONObject((com.alibaba.fastjson.JSONObject)toJSON(object));
  263. }
  264. else if (object instanceof Collection)
  265. {
  266. return new FastJSONArray((com.alibaba.fastjson.JSONArray)toJSON(object));
  267. }
  268. return object;
  269. }
  270. /**
  271. * Get an optional JSONArray associated with a key.
  272. * It returns null if there is no such key, or if its value is not a
  273. * JSONArray.
  274. *
  275. * @param key A key string.
  276. * @return A JSONArray which is the value.
  277. */
  278. public JSONArray optJSONArray(String key)
  279. {
  280. try
  281. {
  282. return this.getJSONArray(key);
  283. }
  284. catch(Exception e)
  285. {
  286. return null;
  287. }
  288. }
  289. /**
  290. * Get an optional JSONObject associated with a key.
  291. * It returns null if there is no such key, or if its value is not a
  292. * JSONObject.
  293. *
  294. * @param key A key string.
  295. * @return A JSONObject which is the value.
  296. */
  297. public JSONObject optJSONObject(String key)
  298. {
  299. try
  300. {
  301. return this.getJSONObject(key);
  302. }
  303. catch(Exception e)
  304. {
  305. return null;
  306. }
  307. }
  308. /**
  309. * Get an optional string associated with a key.
  310. * It returns the defaultValue if there is no such key.
  311. *
  312. * @param key A key string.
  313. * @param defaultValue The default.
  314. * @return A string which is the value.
  315. */
  316. public String optString(String key, String defaultValue)
  317. {
  318. Object object = this.opt(key);
  319. if (object == null || NULL.equals(object))
  320. {
  321. return defaultValue;
  322. }
  323. return object.toString();
  324. }
  325. /**
  326. * Put a key/value pair in the JSONObject, where the value will be a
  327. * JSONArray which is produced from a Collection.
  328. * @param key A key string.
  329. * @param value A Collection value.
  330. * @return this.
  331. * @throws JSONException
  332. */
  333. public JSONObject put(String key, Collection value) throws JSONException
  334. {
  335. this.put(key, (Object)value);
  336. return this;
  337. }
  338. /**
  339. * Put a key/value pair in the JSONObject, where the value will be a
  340. * JSONObject which is produced from a Map.
  341. * @param key A key string.
  342. * @param value A Map value.
  343. * @return this.
  344. * @throws JSONException
  345. */
  346. public JSONObject put(String key, Map value) throws JSONException {
  347. this.put(key, (Object)value);
  348. return this;
  349. }
  350. /**
  351. * Put a key/value pair in the JSONObject. If the value is null,
  352. * then the key will be removed from the JSONObject if it is present.
  353. * @param key A key string.
  354. * @param value An object which is the value. It should be of one of these
  355. * types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,
  356. * or the JSONObject.NULL object.
  357. * @return this.
  358. * @throws JSONException If the value is non-finite number
  359. * or if the key is null.
  360. */
  361. public JSONObject put(String key, Object value) throws JSONException
  362. {
  363. if (key == null)
  364. {
  365. throw new JSONException("Null key.");
  366. }
  367. if (value != null)
  368. {
  369. if (value instanceof FastJSONObject)
  370. {
  371. _inner.put(key, ((FastJSONObject)value).getInnerJSONObject());
  372. }
  373. else if (value instanceof FastJSONArray)
  374. {
  375. _inner.put(key, ((FastJSONArray)value).getInnerJSONArray());
  376. }
  377. else
  378. {
  379. if (value instanceof JSONObject || value instanceof JSONArray)
  380. {
  381. throw new JSONException("the value is not fast version of JSONObjects: " + value);
  382. }
  383. _inner.put(key, toJSON(value));
  384. }
  385. }
  386. else
  387. {
  388. this.remove(key);
  389. }
  390. return this;
  391. }
  392. /**
  393. * Remove a name and its value, if present.
  394. * @param key The name to be removed.
  395. * @return The value that was associated with the name,
  396. * or null if there was no value.
  397. */
  398. public Object remove(String key) {
  399. return _inner.remove(key);
  400. }
  401. /**
  402. * Make a JSON text of this JSONObject. For compactness, no whitespace
  403. * is added. If this would not result in a syntactically correct JSON text,
  404. * then null will be returned instead.
  405. * <p>
  406. * Warning: This method assumes that the data structure is acyclical.
  407. *
  408. * @return a printable, displayable, portable, transmittable
  409. * representation of the object, beginning
  410. * with <code>{</code>&nbsp;<small>(left brace)</small> and ending
  411. * with <code>}</code>&nbsp;<small>(right brace)</small>.
  412. */
  413. public String toString()
  414. {
  415. try
  416. {
  417. return _inner.toString();
  418. }
  419. catch(Exception e)
  420. {
  421. return null;
  422. }
  423. }
  424. /**
  425. * Not a prettyprinted JSON text, just the same as toString().
  426. */
  427. public String toString(int indentFactor) throws JSONException
  428. {
  429. return _inner.toString();
  430. }
  431. }
  432. public static class FastJSONArray extends JSONArray
  433. {
  434. private com.alibaba.fastjson.JSONArray _inner;
  435. /**
  436. * Construct an empty JSONArray.
  437. */
  438. public FastJSONArray()
  439. {
  440. _inner = new com.alibaba.fastjson.JSONArray();
  441. }
  442. public FastJSONArray(String str) throws JSONException
  443. {
  444. try
  445. {
  446. _inner = (com.alibaba.fastjson.JSONArray)JSON.parse(str);
  447. }
  448. catch (Exception e)
  449. {
  450. throw new JSONException(e);
  451. }
  452. }
  453. public FastJSONArray(Collection value)
  454. {
  455. if (value instanceof com.alibaba.fastjson.JSONArray)
  456. {
  457. _inner = (com.alibaba.fastjson.JSONArray)value;
  458. }
  459. else
  460. {
  461. _inner = (com.alibaba.fastjson.JSONArray)toJSON(value);
  462. }
  463. }
  464. public FastJSONArray(com.alibaba.fastjson.JSONArray inner)
  465. {
  466. _inner = inner;
  467. }
  468. public com.alibaba.fastjson.JSONArray getInnerJSONArray()
  469. {
  470. return _inner;
  471. }
  472. /**
  473. * Get the object value associated with an index.
  474. * @param index
  475. * The index must be between 0 and length() - 1.
  476. * @return An object value.
  477. * @throws JSONException If there is no value for the index.
  478. */
  479. public Object get(int index) throws JSONException
  480. {
  481. Object object = this.opt(index);
  482. if (object == null)
  483. {
  484. throw new JSONException("JSONArray[" + index + "] not found.");
  485. }
  486. return object;
  487. }
  488. /**
  489. * Get the JSONArray associated with an index.
  490. * @param index The index must be between 0 and length() - 1.
  491. * @return A JSONArray value.
  492. * @throws JSONException If there is no value for the index. or if the
  493. * value is not a JSONArray
  494. */
  495. public JSONArray getJSONArray(int index) throws JSONException
  496. {
  497. Object object = this.get(index);
  498. if (object instanceof com.alibaba.fastjson.JSONArray)
  499. {
  500. return new FastJSONArray((com.alibaba.fastjson.JSONArray)object);
  501. }
  502. else if (object instanceof Collection)
  503. {
  504. return new FastJSONArray((com.alibaba.fastjson.JSONArray)toJSON(object));
  505. }
  506. else if (object instanceof JSONArray) {
  507. return (JSONArray)object;
  508. }
  509. throw new JSONException("JSONArray[" + index +
  510. "] is not a JSONArray.");
  511. }
  512. /**
  513. * Get the JSONObject associated with an index.
  514. * @param index subscript
  515. * @return A JSONObject value.
  516. * @throws JSONException If there is no value for the index or if the
  517. * value is not a JSONObject
  518. */
  519. public JSONObject getJSONObject(int index) throws JSONException
  520. {
  521. Object object = this.get(index);
  522. if (object instanceof com.alibaba.fastjson.JSONObject)
  523. {
  524. return new FastJSONObject((com.alibaba.fastjson.JSONObject)object);
  525. }
  526. else if (object instanceof Map)
  527. {
  528. return new FastJSONObject((com.alibaba.fastjson.JSONObject)toJSON(object));
  529. }
  530. else if (object instanceof JSONObject) {
  531. return (JSONObject)object;
  532. }
  533. throw new JSONException("JSONArray[" + index +
  534. "] is not a JSONObject.");
  535. }
  536. /**
  537. * Get the number of elements in the JSONArray, included nulls.
  538. *
  539. * @return The length (or size).
  540. */
  541. public int length()
  542. {
  543. return _inner.size();
  544. }
  545. /**
  546. * Get the optional object value associated with an index.
  547. * @param index The index must be between 0 and length() - 1.
  548. * @return An object value, or null if there is no
  549. * object at that index.
  550. */
  551. public Object opt(int index)
  552. {
  553. if (index < 0 || index >= this.length())
  554. {
  555. return null;
  556. }
  557. Object object = _inner.get(index);
  558. if (object == null)
  559. {
  560. return null;
  561. }
  562. else if (object instanceof com.alibaba.fastjson.JSONObject)
  563. {
  564. return new FastJSONObject((com.alibaba.fastjson.JSONObject)object);
  565. }
  566. else if (object instanceof com.alibaba.fastjson.JSONArray)
  567. {
  568. return new FastJSONArray((com.alibaba.fastjson.JSONArray)object);
  569. }
  570. else if (object instanceof Map)
  571. {
  572. return new FastJSONObject((com.alibaba.fastjson.JSONObject)toJSON(object));
  573. }
  574. else if (object instanceof Collection)
  575. {
  576. return new FastJSONArray((com.alibaba.fastjson.JSONArray)toJSON(object));
  577. }
  578. return object;
  579. }
  580. /**
  581. * Get the optional JSONArray associated with an index.
  582. * @param index subscript
  583. * @return A JSONArray value, or null if the index has no value,
  584. * or if the value is not a JSONArray.
  585. */
  586. public JSONArray optJSONArray(int index)
  587. {
  588. try
  589. {
  590. return this.getJSONArray(index);
  591. }
  592. catch(Exception e)
  593. {
  594. return null;
  595. }
  596. }
  597. /**
  598. * Get the optional JSONObject associated with an index.
  599. * Null is returned if the key is not found, or null if the index has
  600. * no value, or if the value is not a JSONObject.
  601. *
  602. * @param index The index must be between 0 and length() - 1.
  603. * @return A JSONObject value.
  604. */
  605. public JSONObject optJSONObject(int index)
  606. {
  607. try
  608. {
  609. return this.getJSONObject(index);
  610. }
  611. catch(Exception e)
  612. {
  613. return null;
  614. }
  615. }
  616. /**
  617. * Get the optional string associated with an index.
  618. * The defaultValue is returned if the key is not found.
  619. *
  620. * @param index The index must be between 0 and length() - 1.
  621. * @param defaultValue The default value.
  622. * @return A String value.
  623. */
  624. public String optString(int index, String defaultValue)
  625. {
  626. Object object = this.opt(index);
  627. if (object == null || JSONObject.NULL.equals(object))
  628. {
  629. return defaultValue;
  630. }
  631. return object.toString();
  632. }
  633. /**
  634. * Put a value in the JSONArray, where the value will be a
  635. * JSONArray which is produced from a Collection.
  636. * @param value A Collection value.
  637. * @return this.
  638. */
  639. public JSONArray put(Collection value)
  640. {
  641. this.put((Object)value);
  642. return this;
  643. }
  644. /**
  645. * Put a value in the JSONArray, where the value will be a
  646. * JSONObject which is produced from a Map.
  647. * @param value A Map value.
  648. * @return this.
  649. */
  650. public JSONArray put(Map value)
  651. {
  652. this.put((Object)value);
  653. return this;
  654. }
  655. /**
  656. * Append an object value. This increases the array's length by one.
  657. * @param value An object value. The value should be a
  658. * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
  659. * JSONObject.NULL object.
  660. * @return this.
  661. */
  662. public JSONArray put(Object value)
  663. {
  664. if (value instanceof FastJSONObject)
  665. {
  666. _inner.add(((FastJSONObject)value).getInnerJSONObject());
  667. }
  668. else if (value instanceof FastJSONArray)
  669. {
  670. _inner.add(((FastJSONArray)value).getInnerJSONArray());
  671. }
  672. else
  673. {
  674. if (value instanceof JSONObject || value instanceof JSONArray)
  675. {
  676. throw new IllegalArgumentException("the value is not fast version of JSONObjects: " + value);
  677. }
  678. _inner.add(toJSON(value));
  679. }
  680. return this;
  681. }
  682. /**
  683. * Put a value in the JSONArray, where the value will be a
  684. * JSONArray which is produced from a Collection.
  685. * @param index The subscript.
  686. * @param value A Collection value.
  687. * @return this.
  688. * @throws JSONException If the index is negative or if the value is
  689. * not finite.
  690. */
  691. public JSONArray put(int index, Collection value) throws JSONException
  692. {
  693. this.put(index, (Object)value);
  694. return this;
  695. }
  696. /**
  697. * Put a value in the JSONArray, where the value will be a
  698. * JSONObject that is produced from a Map.
  699. * @param index The subscript.
  700. * @param value The Map value.
  701. * @return this.
  702. * @throws JSONException If the index is negative or if the the value is
  703. * an invalid number.
  704. */
  705. public JSONArray put(int index, Map value) throws JSONException
  706. {
  707. this.put(index, (Object)value);
  708. return this;
  709. }
  710. /**
  711. * Put or replace an object value in the JSONArray. If the index is greater
  712. * than the length of the JSONArray, then null elements will be added as
  713. * necessary to pad it out.
  714. * @param index The subscript.
  715. * @param value The value to put into the array. The value should be a
  716. * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
  717. * JSONObject.NULL object.
  718. * @return this.
  719. * @throws JSONException If the index is negative or if the the value is
  720. * an invalid number.
  721. */
  722. public JSONArray put(int index, Object value) throws JSONException
  723. {
  724. if (index < 0)
  725. {
  726. throw new JSONException("JSONArray[" + index + "] not found.");
  727. }
  728. if (index < this.length())
  729. {
  730. if (value instanceof FastJSONObject)
  731. {
  732. _inner.set(index, ((FastJSONObject)value).getInnerJSONObject());
  733. }
  734. else if (value instanceof FastJSONArray)
  735. {
  736. _inner.set(index, ((FastJSONArray)value).getInnerJSONArray());
  737. }
  738. else
  739. {
  740. if (value instanceof JSONObject || value instanceof JSONArray)
  741. {
  742. throw new IllegalArgumentException("the value is not fast version of JSONObjects: " + value);
  743. }
  744. _inner.set(index, value);
  745. }
  746. }
  747. else
  748. {
  749. while (index != this.length())
  750. {
  751. this.put(JSONObject.NULL);
  752. }
  753. this.put(value);
  754. }
  755. return this;
  756. }
  757. /**
  758. * Remove an index and close the hole.
  759. * @param index The index of the element to be removed.
  760. * @return The value that was associated with the index,
  761. * or null if there was no value.
  762. */
  763. public Object remove(int index)
  764. {
  765. Object o = this.opt(index);
  766. _inner.remove(index);
  767. return o;
  768. }
  769. /**
  770. * Make a JSON text of this JSONArray. For compactness, no
  771. * unnecessary whitespace is added. If it is not possible to produce a
  772. * syntactically correct JSON text then null will be returned instead. This
  773. * could occur if the array contains an invalid number.
  774. * <p>
  775. * Warning: This method assumes that the data structure is acyclical.
  776. *
  777. * @return a printable, displayable, transmittable
  778. * representation of the array.
  779. */
  780. public String toString()
  781. {
  782. try
  783. {
  784. return _inner.toString();
  785. }
  786. catch (Exception e)
  787. {
  788. return null;
  789. }
  790. }
  791. /**
  792. * Not a prettyprinted JSON text, just the same as toString().
  793. */
  794. public String toString(int indentFactor) throws JSONException
  795. {
  796. return this.toString();
  797. }
  798. }
  799. public static String normalize(String str)
  800. {
  801. return str.replaceAll("[\\s\\\\]*\n[\\s\\\\]*", " ");
  802. }
  803. @SuppressWarnings("unchecked")
  804. public static Object toJSON(Object javaObject)
  805. {
  806. if (javaObject == null) {
  807. return null;
  808. }
  809. if (javaObject instanceof JSON) {
  810. return (JSON) javaObject;
  811. }
  812. if (javaObject instanceof FastJSONObject) {
  813. return ((FastJSONObject)javaObject).getInnerJSONObject();
  814. }
  815. if (javaObject instanceof FastJSONArray) {
  816. return ((FastJSONArray)javaObject).getInnerJSONArray();
  817. }
  818. if (javaObject instanceof Map) {
  819. Map<Object, Object> map = (Map<Object, Object>) javaObject;
  820. com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject(map.size());
  821. for (Map.Entry<Object, Object> entry : map.entrySet()) {
  822. Object key = entry.getKey();
  823. String jsonKey = TypeUtils.castToString(key);
  824. Object jsonValue = toJSON(entry.getValue());
  825. json.put(jsonKey, jsonValue);
  826. }
  827. return json;
  828. }
  829. if (javaObject instanceof Collection) {
  830. Collection<Object> collection = (Collection<Object>) javaObject;
  831. com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray(collection.size());
  832. for (Object item : collection) {
  833. Object jsonValue = toJSON(item);
  834. array.add(jsonValue);
  835. }
  836. return array;
  837. }
  838. Class<?> clazz = javaObject.getClass();
  839. if (clazz.isEnum()) {
  840. return ((Enum<?>) javaObject).name();
  841. }
  842. if (clazz.isArray()) {
  843. int len = Array.getLength(javaObject);
  844. com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray(len);
  845. for (int i = 0; i < len; ++i) {
  846. Object item = Array.get(javaObject, i);
  847. Object jsonValue = toJSON(item);
  848. array.add(jsonValue);
  849. }
  850. return array;
  851. }
  852. if (ParserConfig.getGlobalInstance().isPrimitive(clazz)) {
  853. return javaObject;
  854. }
  855. try {
  856. List<FieldInfo> getters = TypeUtils.computeGetters(clazz, null);
  857. com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject(getters.size());
  858. for (FieldInfo field : getters) {
  859. Object value = field.get(javaObject);
  860. Object jsonValue = toJSON(value);
  861. json.put(field.getName(), jsonValue);
  862. }
  863. return json;
  864. } catch (Exception e) {
  865. throw new com.alibaba.fastjson.JSONException("toJSON error", e);
  866. }
  867. }
  868. public static boolean optBooleanValue(com.alibaba.fastjson.JSONObject json, String key)
  869. throws com.alibaba.fastjson.JSONException
  870. {
  871. return optBooleanValue(json, key, false);
  872. }
  873. public static boolean optBooleanValue(com.alibaba.fastjson.JSONObject json, String key, boolean defaultValue)
  874. throws com.alibaba.fastjson.JSONException
  875. {
  876. Boolean val = json.getBoolean(key);
  877. if (val == null)
  878. return defaultValue;
  879. return val.booleanValue();
  880. }
  881. public static boolean optBooleanValue(com.alibaba.fastjson.JSONArray json, int index)
  882. throws com.alibaba.fastjson.JSONException
  883. {
  884. return optBooleanValue(json, index, false);
  885. }
  886. public static boolean optBooleanValue(com.alibaba.fastjson.JSONArray json, int index, boolean defaultValue)
  887. throws com.alibaba.fastjson.JSONException
  888. {
  889. Boolean val = json.getBoolean(index);
  890. if (val == null)
  891. return defaultValue;
  892. return val.booleanValue();
  893. }
  894. public static com.alibaba.fastjson.JSONObject optJSONObject(com.alibaba.fastjson.JSONObject json, String key)
  895. throws com.alibaba.fastjson.JSONException
  896. {
  897. Object obj = json.get(key);
  898. if (obj == null)
  899. return null;
  900. if (!(obj instanceof com.alibaba.fastjson.JSONObject))
  901. {
  902. try
  903. {
  904. return (com.alibaba.fastjson.JSONObject)toJSON(obj);
  905. }
  906. catch(Exception e)
  907. {
  908. return null;
  909. }
  910. }
  911. return (com.alibaba.fastjson.JSONObject)obj;
  912. }
  913. public static com.alibaba.fastjson.JSONObject optJSONObject(com.alibaba.fastjson.JSONArray json, int index)
  914. throws com.alibaba.fastjson.JSONException
  915. {
  916. Object obj = json.get(index);
  917. if (obj == null)
  918. return null;
  919. if (!(obj instanceof com.alibaba.fastjson.JSONObject))
  920. {
  921. try
  922. {
  923. return (com.alibaba.fastjson.JSONObject)toJSON(obj);
  924. }
  925. catch(Exception e)
  926. {
  927. return null;
  928. }
  929. }
  930. return (com.alibaba.fastjson.JSONObject)obj;
  931. }
  932. public static com.alibaba.fastjson.JSONArray optJSONArray(com.alibaba.fastjson.JSONObject json, String key)
  933. throws com.alibaba.fastjson.JSONException
  934. {
  935. Object obj = json.get(key);
  936. if (obj == null)
  937. return null;
  938. if (!(obj instanceof com.alibaba.fastjson.JSONArray))
  939. {
  940. try
  941. {
  942. return (com.alibaba.fastjson.JSONArray)toJSON(obj);
  943. }
  944. catch(Exception e)
  945. {
  946. return null;
  947. }
  948. }
  949. return (com.alibaba.fastjson.JSONArray)obj;
  950. }
  951. public static com.alibaba.fastjson.JSONArray optJSONArray(com.alibaba.fastjson.JSONArray json, int index)
  952. throws com.alibaba.fastjson.JSONException
  953. {
  954. Object obj = json.get(index);
  955. if (obj == null)
  956. return null;
  957. if (!(obj instanceof com.alibaba.fastjson.JSONArray))
  958. {
  959. try
  960. {
  961. return (com.alibaba.fastjson.JSONArray)toJSON(obj);
  962. }
  963. catch(Exception e)
  964. {
  965. return null;
  966. }
  967. }
  968. return (com.alibaba.fastjson.JSONArray)obj;
  969. }
  970. }