/src/test/java/com/alibaba/json/bvt/writeAsArray/WriteAsArray_string.java

https://github.com/alibaba/fastjson · Java · 99 lines · 81 code · 18 blank · 0 comment · 0 complexity · 4479b55c81edb52153fbba4ec4146980 MD5 · raw file

  1. package com.alibaba.json.bvt.writeAsArray;
  2. import org.junit.Assert;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONException;
  5. import com.alibaba.fastjson.parser.Feature;
  6. import com.alibaba.fastjson.serializer.SerializerFeature;
  7. import junit.framework.TestCase;
  8. public class WriteAsArray_string extends TestCase {
  9. public void test_0() throws Exception {
  10. Model model = new Model();
  11. String text = JSON.toJSONString(model, SerializerFeature.BeanToArray);
  12. Assert.assertEquals("[null]", text);
  13. Model model2 = JSON.parseObject(text, Model.class, Feature.SupportArrayToBean);
  14. Assert.assertNull(model2.name);
  15. }
  16. public void test_1() throws Exception {
  17. Model model = new Model();
  18. model.name = "abc";
  19. String text = JSON.toJSONString(model, SerializerFeature.BeanToArray);
  20. Assert.assertEquals("[\"abc\"]", text);
  21. Model model2 = JSON.parseObject(text, Model.class, Feature.SupportArrayToBean);
  22. Assert.assertEquals(model.name, model2.name);
  23. }
  24. public void test_error_0() throws Exception {
  25. Exception error = null;
  26. try {
  27. JSON.parseObject("[n", Model.class, Feature.SupportArrayToBean);
  28. } catch (JSONException ex) {
  29. error = ex;
  30. }
  31. Assert.assertNotNull(error);
  32. }
  33. public void test_error_1() throws Exception {
  34. Exception error = null;
  35. try {
  36. JSON.parseObject("[nu", Model.class, Feature.SupportArrayToBean);
  37. } catch (JSONException ex) {
  38. error = ex;
  39. }
  40. Assert.assertNotNull(error);
  41. }
  42. public void test_error_2() throws Exception {
  43. Exception error = null;
  44. try {
  45. JSON.parseObject("[nul", Model.class, Feature.SupportArrayToBean);
  46. } catch (JSONException ex) {
  47. error = ex;
  48. }
  49. Assert.assertNotNull(error);
  50. }
  51. public void test_error_3() throws Exception {
  52. Exception error = null;
  53. try {
  54. JSON.parseObject("[null", Model.class, Feature.SupportArrayToBean);
  55. } catch (JSONException ex) {
  56. error = ex;
  57. }
  58. Assert.assertNotNull(error);
  59. }
  60. public void test_error_4() throws Exception {
  61. Exception error = null;
  62. try {
  63. JSON.parseObject("[\"ab", Model.class, Feature.SupportArrayToBean);
  64. } catch (JSONException ex) {
  65. error = ex;
  66. }
  67. Assert.assertNotNull(error);
  68. }
  69. public void test_error_5() throws Exception {
  70. Exception error = null;
  71. try {
  72. JSON.parseObject("[\"ab\"", Model.class, Feature.SupportArrayToBean);
  73. } catch (JSONException ex) {
  74. error = ex;
  75. }
  76. Assert.assertNotNull(error);
  77. }
  78. public static class Model {
  79. public String name;
  80. }
  81. }