PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/dubbo-common/src/test/java/com/alibaba/dubbo/common/json/JacksonTest.java

https://gitlab.com/sxyseo/dubbox
Java | 177 lines | 145 code | 28 blank | 4 comment | 0 complexity | 367871044c9672aae9261acafe4b33fd MD5 | raw file
  1. package com.alibaba.dubbo.common.json;
  2. import com.alibaba.dubbo.common.serialize.support.json.FastJsonObjectOutput;
  3. import com.alibaba.dubbo.common.serialize.support.json.JacksonObjectInput;
  4. import com.alibaba.dubbo.common.serialize.support.json.JacksonObjectOutput;
  5. import com.alibaba.dubbo.common.utils.CollectionUtils;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import org.junit.Test;
  8. import java.io.*;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. /**
  13. * Created by dylan on 11/13/14.
  14. */
  15. public class JacksonTest {
  16. @Test
  17. public void testWrite() throws Exception{
  18. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  19. JacksonObjectOutput output = new JacksonObjectOutput(bos);
  20. // PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
  21. output.writeUTF("2.0.0");
  22. output.writeBool(true);
  23. output.writeByte((byte) 2);
  24. output.writeBytes(new String("abc").getBytes());
  25. output.writeDouble(2.0d);
  26. output.writeFloat(3.05f);
  27. output.writeInt(32);
  28. output.writeLong(64L);
  29. output.writeShort((short) 16);
  30. output.writeBytes(new String("afdsafdsafsa").getBytes(), 6, 4);
  31. output.writeObject(new String[]{"abc", "def", "gfk"});
  32. output.writeObject(new int[]{4,5,8});
  33. output.writeObject(new JacksonBean());
  34. output.flushBuffer();
  35. JacksonObjectInput input = new JacksonObjectInput(new ByteArrayInputStream(bos.toByteArray()));
  36. System.out.println(input.readUTF());
  37. System.out.println(input.readBool());
  38. System.out.println(input.readByte());
  39. System.out.println(new String(input.readBytes()));
  40. System.out.println(input.readDouble());
  41. System.out.println(input.readFloat());
  42. System.out.println(input.readInt());
  43. System.out.println(input.readLong());
  44. System.out.println(input.readShort());
  45. System.out.println(Arrays.toString(input.readBytes()));
  46. System.out.println(Arrays.toString(input.readObject(String[].class)));
  47. System.out.println(Arrays.toString(input.readObject(int[].class)));
  48. JacksonBean jacksonBean = input.readObject(JacksonBean.class);
  49. System.out.println(jacksonBean);
  50. }
  51. public static class JacksonBean {
  52. private boolean bValue = true;
  53. private String sValue = "string";
  54. private int iValue = 20;
  55. private double dValue = 20.5;
  56. private float fValue = 2.18f;
  57. private long lValue = 3;
  58. private Double dValue2 = null;
  59. private List<InnerBean> innerBeanList = new ArrayList<InnerBean>();
  60. public JacksonBean(){
  61. innerBeanList.add(new InnerBean());
  62. innerBeanList.add(new InnerBean());
  63. }
  64. @Override
  65. public String toString() {
  66. return "JacksonBean{" +
  67. "bValue=" + bValue +
  68. ", sValue='" + sValue + '\'' +
  69. ", iValue=" + iValue +
  70. ", dValue=" + dValue +
  71. ", fValue=" + fValue +
  72. ", lValue=" + lValue +
  73. ", dValue2=" + dValue2 +
  74. ", innerBeanList=" + innerBeanList +
  75. '}';
  76. }
  77. public boolean isbValue() {
  78. return bValue;
  79. }
  80. public void setbValue(boolean bValue) {
  81. this.bValue = bValue;
  82. }
  83. public String getsValue() {
  84. return sValue;
  85. }
  86. public void setsValue(String sValue) {
  87. this.sValue = sValue;
  88. }
  89. public int getiValue() {
  90. return iValue;
  91. }
  92. public void setiValue(int iValue) {
  93. this.iValue = iValue;
  94. }
  95. public double getdValue() {
  96. return dValue;
  97. }
  98. public void setdValue(double dValue) {
  99. this.dValue = dValue;
  100. }
  101. public float getfValue() {
  102. return fValue;
  103. }
  104. public void setfValue(float fValue) {
  105. this.fValue = fValue;
  106. }
  107. public long getlValue() {
  108. return lValue;
  109. }
  110. public void setlValue(long lValue) {
  111. this.lValue = lValue;
  112. }
  113. public Double getdValue2() {
  114. return dValue2;
  115. }
  116. public void setdValue2(Double dValue2) {
  117. this.dValue2 = dValue2;
  118. }
  119. public List<InnerBean> getInnerBeanList() {
  120. return innerBeanList;
  121. }
  122. public void setInnerBeanList(List<InnerBean> innerBeanList) {
  123. this.innerBeanList = innerBeanList;
  124. }
  125. }
  126. public static class InnerBean {
  127. private String siValue = "innerStr";
  128. private int iiValue = 18;
  129. public String getSiValue() {
  130. return siValue;
  131. }
  132. public void setSiValue(String siValue) {
  133. this.siValue = siValue;
  134. }
  135. public int getIiValue() {
  136. return iiValue;
  137. }
  138. public void setIiValue(int iiValue) {
  139. this.iiValue = iiValue;
  140. }
  141. @Override
  142. public String toString() {
  143. return "InnerBean{" +
  144. "siValue='" + siValue + '\'' +
  145. ", iiValue=" + iiValue +
  146. '}';
  147. }
  148. }
  149. }