/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectOutput.java

https://github.com/apache/incubator-dubbo · Java · 113 lines · 74 code · 20 blank · 19 comment · 0 complexity · c92f7a58a17a4235f6227457b394fd33 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.dubbo.common.serialize.fastjson;
  18. import org.apache.dubbo.common.serialize.ObjectOutput;
  19. import com.alibaba.fastjson.serializer.JSONSerializer;
  20. import com.alibaba.fastjson.serializer.SerializeWriter;
  21. import com.alibaba.fastjson.serializer.SerializerFeature;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import java.io.OutputStreamWriter;
  25. import java.io.PrintWriter;
  26. import java.io.Writer;
  27. /**
  28. * FastJson object output implementation
  29. */
  30. public class FastJsonObjectOutput implements ObjectOutput {
  31. private final PrintWriter writer;
  32. public FastJsonObjectOutput(OutputStream out) {
  33. this(new OutputStreamWriter(out));
  34. }
  35. public FastJsonObjectOutput(Writer writer) {
  36. this.writer = new PrintWriter(writer);
  37. }
  38. @Override
  39. public void writeBool(boolean v) throws IOException {
  40. writeObject(v);
  41. }
  42. @Override
  43. public void writeByte(byte v) throws IOException {
  44. writeObject(v);
  45. }
  46. @Override
  47. public void writeShort(short v) throws IOException {
  48. writeObject(v);
  49. }
  50. @Override
  51. public void writeInt(int v) throws IOException {
  52. writeObject(v);
  53. }
  54. @Override
  55. public void writeLong(long v) throws IOException {
  56. writeObject(v);
  57. }
  58. @Override
  59. public void writeFloat(float v) throws IOException {
  60. writeObject(v);
  61. }
  62. @Override
  63. public void writeDouble(double v) throws IOException {
  64. writeObject(v);
  65. }
  66. @Override
  67. public void writeUTF(String v) throws IOException {
  68. writeObject(v);
  69. }
  70. @Override
  71. public void writeBytes(byte[] b) throws IOException {
  72. writer.println(new String(b));
  73. }
  74. @Override
  75. public void writeBytes(byte[] b, int off, int len) throws IOException {
  76. writer.println(new String(b, off, len));
  77. }
  78. @Override
  79. public void writeObject(Object obj) throws IOException {
  80. SerializeWriter out = new SerializeWriter();
  81. JSONSerializer serializer = new JSONSerializer(out);
  82. serializer.config(SerializerFeature.WriteEnumUsingToString, true);
  83. serializer.write(obj);
  84. out.writeTo(writer);
  85. out.close(); // for reuse SerializeWriter buf
  86. writer.println();
  87. writer.flush();
  88. }
  89. @Override
  90. public void flushBuffer() throws IOException {
  91. writer.flush();
  92. }
  93. }