PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/sxyseo/dubbox
Java | 178 lines | 112 code | 18 blank | 48 comment | 4 complexity | dc12c221587fa819f716765f0e6fd7a3 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.ObjectInput;
  19. import com.alibaba.dubbo.common.utils.ReflectUtils;
  20. import com.fasterxml.jackson.databind.ObjectMapper;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.lang.reflect.Type;
  26. import java.util.Map;
  27. /**
  28. * JsonObjectInput
  29. *
  30. * @author dylan
  31. */
  32. public class JacksonObjectInput implements ObjectInput {
  33. private static Logger logger = LoggerFactory.getLogger(JacksonObjectInput.class);
  34. private final ObjectMapper objectMapper;
  35. // private final BufferedReader reader;
  36. private final Map<String, String> data;
  37. private static final String KEY_PREFIX = "$";
  38. private int index = 0;
  39. public JacksonObjectInput(InputStream inputstream) throws IOException {
  40. // this.reader = new BufferedReader(new InputStreamReader(inputstream));
  41. // String line = null;
  42. // try {
  43. // while ((line = reader.readLine()) != null) {
  44. // System.out.println(line);
  45. // }
  46. // }catch(Exception e){
  47. // e.printStackTrace();
  48. // }
  49. this.objectMapper = Jackson.getObjectMapper();
  50. try {
  51. data = objectMapper.readValue(inputstream, Map.class);
  52. } catch (IOException e) {
  53. logger.error("parse inputstream error.", e);
  54. throw e;
  55. }
  56. }
  57. public boolean readBool() throws IOException {
  58. try {
  59. return readObject(Boolean.class);
  60. } catch (ClassNotFoundException e) {
  61. throw new IOException(e.getMessage());
  62. }
  63. }
  64. public byte readByte() throws IOException {
  65. try {
  66. return readObject(Byte.class);
  67. } catch (ClassNotFoundException e) {
  68. throw new IOException(e.getMessage());
  69. }
  70. }
  71. public short readShort() throws IOException {
  72. try {
  73. return readObject(Short.class);
  74. } catch (ClassNotFoundException e) {
  75. throw new IOException(e.getMessage());
  76. }
  77. }
  78. public int readInt() throws IOException {
  79. try {
  80. return readObject(Integer.class);
  81. } catch (ClassNotFoundException e) {
  82. throw new IOException(e.getMessage());
  83. }
  84. }
  85. public long readLong() throws IOException {
  86. try {
  87. return readObject(Long.class);
  88. } catch (ClassNotFoundException e) {
  89. throw new IOException(e.getMessage());
  90. }
  91. }
  92. public float readFloat() throws IOException {
  93. try {
  94. return readObject(Float.class);
  95. } catch (ClassNotFoundException e) {
  96. throw new IOException(e.getMessage());
  97. }
  98. }
  99. public double readDouble() throws IOException {
  100. try {
  101. return readObject(Double.class);
  102. } catch (ClassNotFoundException e) {
  103. throw new IOException(e.getMessage());
  104. }
  105. }
  106. public String readUTF() throws IOException {
  107. try {
  108. return readObject(String.class);
  109. } catch (ClassNotFoundException e) {
  110. throw new IOException(e.getMessage());
  111. }
  112. }
  113. public byte[] readBytes() throws IOException {
  114. return readUTF().getBytes();
  115. }
  116. public Object readObject() throws IOException, ClassNotFoundException {
  117. // try {
  118. // String json = readLine();
  119. // if (json.startsWith("{")) {
  120. // return JSON.parse(json, Map.class);
  121. // } else {
  122. // json = "{\"value\":" + json + "}";
  123. //
  124. // @SuppressWarnings("unchecked")
  125. // Map<String, Object> map = objectMapper.readValue(json, Map.class);
  126. // return map.get("value");
  127. // }
  128. // } catch (ParseException e) {
  129. // throw new IOException(e.getMessage());
  130. // }
  131. try {
  132. return readObject(Object.class);
  133. } catch (ClassNotFoundException e) {
  134. throw new IOException(e.getMessage());
  135. }
  136. }
  137. @SuppressWarnings("unchecked")
  138. public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException {
  139. // Object value = readObject();
  140. //read data value
  141. String json = this.data.get(KEY_PREFIX + (++index));
  142. //read data type
  143. String dataType = this.data.get(KEY_PREFIX + index + "t");
  144. if (dataType != null) {
  145. Class clazz = ReflectUtils.desc2class(dataType);
  146. if (cls.isAssignableFrom(clazz)) {
  147. cls = clazz;
  148. } else {
  149. throw new IllegalArgumentException("Class \"" + clazz + "\" is not inherited from \"" + cls + "\"");
  150. }
  151. }
  152. logger.debug("index:{}, value:{}", index, json);
  153. return objectMapper.readValue(json, cls);
  154. }
  155. @SuppressWarnings("unchecked")
  156. public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException {
  157. // Object value = readObject();
  158. return readObject(cls);
  159. }
  160. }