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

/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JacksonObjectOutput.java

https://gitlab.com/sxyseo/dubbox
Java | 122 lines | 72 code | 21 blank | 29 comment | 2 complexity | 79626bc2f60f2da1d34c8c296e8d3703 MD5 | raw file
  1. /*
  2. * Copyright 1999-2011 Alibaba Group.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.dubbo.common.serialize.support.json;
  17. import com.alibaba.dubbo.common.json.Jackson;
  18. import com.alibaba.dubbo.common.serialize.ObjectOutput;
  19. import com.alibaba.dubbo.common.utils.ReflectUtils;
  20. import com.fasterxml.jackson.databind.ObjectMapper;
  21. import java.io.*;
  22. import java.util.Collection;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. /**
  26. * jackson object output
  27. *
  28. * @author dylan
  29. */
  30. public class JacksonObjectOutput implements ObjectOutput {
  31. private final ObjectMapper objectMapper;
  32. private final Map<String, Object> data;
  33. private final static String KEY_PREFIX = "$";
  34. private int index = 0;
  35. private final PrintWriter writer;
  36. public JacksonObjectOutput(OutputStream out) {
  37. this(new OutputStreamWriter(out));
  38. }
  39. public JacksonObjectOutput(Writer writer) {
  40. this.objectMapper = Jackson.getObjectMapper();
  41. this.writer = new PrintWriter(writer);
  42. this.data = new HashMap<String, Object>();
  43. }
  44. public void writeBool(boolean v) throws IOException {
  45. writeObject0(v);
  46. }
  47. public void writeByte(byte v) throws IOException {
  48. writeObject0(v);
  49. }
  50. public void writeShort(short v) throws IOException {
  51. writeObject0(v);
  52. }
  53. public void writeInt(int v) throws IOException {
  54. writeObject0(v);
  55. }
  56. public void writeLong(long v) throws IOException {
  57. writeObject0(v);
  58. }
  59. public void writeFloat(float v) throws IOException {
  60. writeObject0(v);
  61. }
  62. public void writeDouble(double v) throws IOException {
  63. writeObject0(v);
  64. }
  65. public void writeUTF(String v) throws IOException {
  66. writeObject0(v);
  67. }
  68. public void writeBytes(byte[] b) throws IOException {
  69. writeObject0(new String(b));
  70. }
  71. public void writeBytes(byte[] b, int off, int len) throws IOException {
  72. writeObject0(new String(b, off, len));
  73. }
  74. public void writeObject(Object obj) throws IOException {
  75. // int i = ++index;
  76. if (obj == null) {
  77. writeObject0(obj);
  78. return;
  79. }
  80. //write data value
  81. writeObject0(obj);
  82. //write data type
  83. Class c = obj.getClass();
  84. String desc = ReflectUtils.getDesc(c);
  85. data.put(KEY_PREFIX + (index) + "t", desc);
  86. // if (obj instanceof Collection) {
  87. // //集合类型
  88. // } else if (obj instanceof Map) {
  89. // //
  90. // } else {
  91. // }
  92. }
  93. private void writeObject0(Object obj) throws IOException {
  94. data.put(KEY_PREFIX + (++index), objectMapper.writeValueAsString(obj));
  95. }
  96. public void flushBuffer() throws IOException {
  97. objectMapper.writeValue(writer, data);
  98. writer.println();
  99. writer.flush();
  100. }
  101. }