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

https://github.com/alibaba/fastjson · Java · 257 lines · 214 code · 43 blank · 0 comment · 0 complexity · 763bd1927169737e59f63f33b326f64e MD5 · raw file

  1. package com.alibaba.json.bvt;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.annotation.JSONField;
  4. import junit.framework.TestCase;
  5. public class JSONFieldDefaultValueTest extends TestCase {
  6. public void test_default_value() throws Exception {
  7. Model m = new Model();
  8. String s = JSON.toJSONString(m);
  9. System.out.println(s);
  10. Model m2 = JSON.parseObject(s, Model.class);
  11. assertEquals("string", m2.getString());
  12. assertEquals(false, m2.getaBoolean());
  13. assertEquals(true, m2.getaBoolean2().booleanValue());
  14. assertEquals(0, m2.getAnInt());
  15. assertEquals(888, m2.getInteger().intValue());
  16. assertEquals(0, m2.getaShort());
  17. assertEquals(88, m2.getaShort2().shortValue());
  18. assertEquals('\u0000', m2.getaChar());
  19. assertEquals('J', m2.getCharacter().charValue());
  20. assertEquals(0, m2.getaByte());
  21. assertEquals(8, m2.getaByte2().byteValue());
  22. assertEquals(0, m2.getaLong());
  23. assertEquals(8888, m2.getaLong2().longValue());
  24. assertEquals("0.0", "" + m2.getaFloat());
  25. assertEquals("8.8", "" + m2.getaFloat2());
  26. assertEquals("0.0", "" + m2.getaDouble());
  27. assertEquals("88.88", "" + m2.getaDouble2());
  28. }
  29. public void test_not_null() throws Exception {
  30. Model m = new Model("test", true, 888, (short)88, 'J', (byte)8, 8888L, 8.8F, 88.88, false, 999, (short)99, 'C', (byte)9, 9999L, 9.9F, 99.99);
  31. String s = JSON.toJSONString(m);
  32. System.out.println(s);
  33. Model m2 = JSON.parseObject(s, Model.class);
  34. assertEquals("test", m2.getString());
  35. assertEquals(true, m2.getaBoolean());
  36. assertEquals(false, m2.getaBoolean2().booleanValue());
  37. assertEquals(888, m2.getAnInt());
  38. assertEquals(999, m2.getInteger().intValue());
  39. assertEquals(88, m2.getaShort());
  40. assertEquals(99, m2.getaShort2().shortValue());
  41. assertEquals('J', m2.getaChar());
  42. assertEquals('C', m2.getCharacter().charValue());
  43. assertEquals(8, m2.getaByte());
  44. assertEquals(9, m2.getaByte2().byteValue());
  45. assertEquals(8888, m2.getaLong());
  46. assertEquals(9999, m2.getaLong2().longValue());
  47. assertEquals("8.8", "" + m2.getaFloat());
  48. assertEquals("9.9", "" + m2.getaFloat2());
  49. assertEquals("88.88", "" + m2.getaDouble());
  50. assertEquals("99.99", "" + m2.getaDouble2());
  51. }
  52. public static class Model {
  53. @JSONField(defaultValue = "string")
  54. private String string;
  55. @JSONField(defaultValue = "true") //shouldn't work
  56. private boolean aBoolean;
  57. @JSONField(defaultValue = "888") //shouldn't work
  58. private int anInt;
  59. @JSONField(defaultValue = "88") //shouldn't work
  60. private short aShort;
  61. @JSONField(defaultValue = "J") //shouldn't work
  62. private char aChar;
  63. @JSONField(defaultValue = "8") //shouldn't work
  64. private byte aByte;
  65. @JSONField(defaultValue = "8888") //shouldn't work
  66. private long aLong;
  67. @JSONField(defaultValue = "8.8") //shouldn't work
  68. private float aFloat;
  69. @JSONField(defaultValue = "88.88") //shouldn't work
  70. private double aDouble;
  71. @JSONField(defaultValue = "true")
  72. private Boolean aBoolean2;
  73. @JSONField(defaultValue = "888")
  74. private Integer integer;
  75. @JSONField(defaultValue = "88")
  76. private Short aShort2;
  77. @JSONField(defaultValue = "J")
  78. private Character character;
  79. @JSONField(defaultValue = "8")
  80. private Byte aByte2;
  81. @JSONField(defaultValue = "8888")
  82. private Long aLong2;
  83. @JSONField(defaultValue = "8.8")
  84. private Float aFloat2;
  85. @JSONField(defaultValue = "88.88")
  86. private Double aDouble2;
  87. public Model(String string, boolean aBoolean, int anInt, short aShort, char aChar,
  88. byte aByte, long aLong, float aFloat, double aDouble,
  89. Boolean aBoolean2, Integer integer, Short aShort2, Character character,
  90. Byte aByte2, Long aLong2, Float aFloat2, Double aDouble2) {
  91. this.string = string;
  92. this.aBoolean = aBoolean;
  93. this.anInt = anInt;
  94. this.aShort = aShort;
  95. this.aChar = aChar;
  96. this.aByte = aByte;
  97. this.aLong = aLong;
  98. this.aFloat = aFloat;
  99. this.aDouble = aDouble;
  100. this.aBoolean2 = aBoolean2;
  101. this.integer = integer;
  102. this.aShort2 = aShort2;
  103. this.character = character;
  104. this.aByte2 = aByte2;
  105. this.aLong2 = aLong2;
  106. this.aFloat2 = aFloat2;
  107. this.aDouble2 = aDouble2;
  108. }
  109. public Model() {
  110. }
  111. public String getString() {
  112. return string;
  113. }
  114. public void setString(String string) {
  115. this.string = string;
  116. }
  117. public boolean getaBoolean() {
  118. return aBoolean;
  119. }
  120. public void setaBoolean(boolean aBoolean) {
  121. this.aBoolean = aBoolean;
  122. }
  123. public int getAnInt() {
  124. return anInt;
  125. }
  126. public void setAnInt(int anInt) {
  127. this.anInt = anInt;
  128. }
  129. public short getaShort() {
  130. return aShort;
  131. }
  132. public void setaShort(short aShort) {
  133. this.aShort = aShort;
  134. }
  135. public char getaChar() {
  136. return aChar;
  137. }
  138. public void setaChar(char aChar) {
  139. this.aChar = aChar;
  140. }
  141. public byte getaByte() {
  142. return aByte;
  143. }
  144. public void setaByte(byte aByte) {
  145. this.aByte = aByte;
  146. }
  147. public long getaLong() {
  148. return aLong;
  149. }
  150. public void setaLong(long aLong) {
  151. this.aLong = aLong;
  152. }
  153. public float getaFloat() {
  154. return aFloat;
  155. }
  156. public void setaFloat(float aFloat) {
  157. this.aFloat = aFloat;
  158. }
  159. public double getaDouble() {
  160. return aDouble;
  161. }
  162. public void setaDouble(double aDouble) {
  163. this.aDouble = aDouble;
  164. }
  165. public Boolean getaBoolean2() {
  166. return aBoolean2;
  167. }
  168. public void setaBoolean2(Boolean aBoolean2) {
  169. this.aBoolean2 = aBoolean2;
  170. }
  171. public Integer getInteger() {
  172. return integer;
  173. }
  174. public void setInteger(Integer integer) {
  175. this.integer = integer;
  176. }
  177. public Short getaShort2() {
  178. return aShort2;
  179. }
  180. public void setaShort2(Short aShort2) {
  181. this.aShort2 = aShort2;
  182. }
  183. public Character getCharacter() {
  184. return character;
  185. }
  186. public void setCharacter(Character character) {
  187. this.character = character;
  188. }
  189. public Byte getaByte2() {
  190. return aByte2;
  191. }
  192. public void setaByte2(Byte aByte2) {
  193. this.aByte2 = aByte2;
  194. }
  195. public Long getaLong2() {
  196. return aLong2;
  197. }
  198. public void setaLong2(Long aLong2) {
  199. this.aLong2 = aLong2;
  200. }
  201. public Float getaFloat2() {
  202. return aFloat2;
  203. }
  204. public void setaFloat2(Float aFloat2) {
  205. this.aFloat2 = aFloat2;
  206. }
  207. public Double getaDouble2() {
  208. return aDouble2;
  209. }
  210. public void setaDouble2(Double aDouble2) {
  211. this.aDouble2 = aDouble2;
  212. }
  213. }
  214. }