/src/test/java/com/alibaba/json/bvt/JSONObjectTest_readObject.java

https://github.com/alibaba/fastjson · Java · 156 lines · 113 code · 43 blank · 0 comment · 0 complexity · 90a8d22a8c3f490d19388f7c261db61f MD5 · raw file

  1. package com.alibaba.json.bvt;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.parser.ParserConfig;
  5. import junit.framework.TestCase;
  6. import java.io.ByteArrayInputStream;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.ObjectInputStream;
  9. import java.io.ObjectOutputStream;
  10. public class JSONObjectTest_readObject extends TestCase {
  11. public void test_0() throws Exception {
  12. JSONObject jsonObject = new JSONObject();
  13. jsonObject.put("id", 123);
  14. jsonObject.put("obj", new JSONObject());
  15. ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
  16. ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
  17. objOut.writeObject(jsonObject);
  18. objOut.flush();
  19. byte[] bytes = bytesOut.toByteArray();
  20. ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
  21. ObjectInputStream objIn = new ObjectInputStream(bytesIn);
  22. Object obj = objIn.readObject();
  23. assertEquals(JSONObject.class, obj.getClass());
  24. assertEquals(jsonObject, obj);
  25. }
  26. public void test_2() throws Exception {
  27. JSONObject jsonObject = JSON.parseObject("{123:345}");
  28. ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
  29. ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
  30. objOut.writeObject(jsonObject);
  31. objOut.flush();
  32. byte[] bytes = bytesOut.toByteArray();
  33. ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
  34. ObjectInputStream objIn = new ObjectInputStream(bytesIn);
  35. Object obj = objIn.readObject();
  36. assertEquals(JSONObject.class, obj.getClass());
  37. assertEquals(jsonObject, obj);
  38. }
  39. public void test_3() throws Exception {
  40. JSONObject jsonObject = JSON.parseObject("{123:345,\"items\":[1,2,3,4]}");
  41. ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
  42. ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
  43. objOut.writeObject(jsonObject);
  44. objOut.flush();
  45. byte[] bytes = bytesOut.toByteArray();
  46. ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
  47. ObjectInputStream objIn = new ObjectInputStream(bytesIn);
  48. Object obj = objIn.readObject();
  49. assertEquals(JSONObject.class, obj.getClass());
  50. assertEquals(jsonObject, obj);
  51. }
  52. public void test_4() throws Exception {
  53. JSONObject jsonObject = new JSONObject();
  54. jsonObject.put("val", new Byte[]{});
  55. ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
  56. ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
  57. objOut.writeObject(jsonObject);
  58. objOut.flush();
  59. byte[] bytes = bytesOut.toByteArray();
  60. ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
  61. ObjectInputStream objIn = new ObjectInputStream(bytesIn);
  62. Object obj = objIn.readObject();
  63. assertEquals(JSONObject.class, obj.getClass());
  64. assertEquals(jsonObject.toJSONString(), JSON.toJSONString(obj));
  65. }
  66. public void test_5() throws Exception {
  67. JSONObject jsonObject = new JSONObject();
  68. jsonObject.put("val", new byte[]{});
  69. ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
  70. ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
  71. objOut.writeObject(jsonObject);
  72. objOut.flush();
  73. byte[] bytes = bytesOut.toByteArray();
  74. ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
  75. ObjectInputStream objIn = new ObjectInputStream(bytesIn);
  76. Object obj = objIn.readObject();
  77. assertEquals(JSONObject.class, obj.getClass());
  78. assertEquals(jsonObject.toJSONString(), JSON.toJSONString(obj));
  79. }
  80. public void test_6() throws Exception {
  81. JSONObject jsonObject = new JSONObject();
  82. jsonObject.put("val", new Character[]{});
  83. jsonObject.put("cls", java.lang.Number.class);
  84. jsonObject.put("nums", new java.lang.Number[] {});
  85. ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
  86. ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
  87. objOut.writeObject(jsonObject);
  88. objOut.flush();
  89. byte[] bytes = bytesOut.toByteArray();
  90. ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
  91. ObjectInputStream objIn = new ObjectInputStream(bytesIn);
  92. Object obj = objIn.readObject();
  93. assertEquals(JSONObject.class, obj.getClass());
  94. assertEquals(jsonObject.toJSONString(), JSON.toJSONString(obj));
  95. }
  96. public void test_7() throws Exception {
  97. ParserConfig.global.setSafeMode(true);
  98. try {
  99. JSONObject jsonObject = new JSONObject();
  100. jsonObject.put("m", new java.util.HashMap());
  101. ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
  102. ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
  103. objOut.writeObject(jsonObject);
  104. objOut.flush();
  105. byte[] bytes = bytesOut.toByteArray();
  106. ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
  107. ObjectInputStream objIn = new ObjectInputStream(bytesIn);
  108. Object obj = objIn.readObject();
  109. } finally {
  110. ParserConfig.global.setSafeMode(false);
  111. }
  112. }
  113. }