PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/sxyseo/dubbox
Java | 100 lines | 61 code | 19 blank | 20 comment | 0 complexity | 89dd1362b4fa85cc1ebc54a91128ceb3 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 java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.io.OutputStreamWriter;
  20. import java.io.PrintWriter;
  21. import java.io.Writer;
  22. import com.alibaba.dubbo.common.serialize.ObjectOutput;
  23. import com.alibaba.fastjson.serializer.JSONSerializer;
  24. import com.alibaba.fastjson.serializer.SerializeWriter;
  25. import com.alibaba.fastjson.serializer.SerializerFeature;
  26. /**
  27. * JsonObjectOutput
  28. *
  29. * @author william.liangf
  30. */
  31. public class FastJsonObjectOutput implements ObjectOutput {
  32. private final PrintWriter writer;
  33. public FastJsonObjectOutput(OutputStream out) {
  34. this(new OutputStreamWriter(out));
  35. }
  36. public FastJsonObjectOutput(Writer writer) {
  37. this.writer = new PrintWriter(writer);
  38. }
  39. public void writeBool(boolean v) throws IOException {
  40. writeObject(v);
  41. }
  42. public void writeByte(byte v) throws IOException {
  43. writeObject(v);
  44. }
  45. public void writeShort(short v) throws IOException {
  46. writeObject(v);
  47. }
  48. public void writeInt(int v) throws IOException {
  49. writeObject(v);
  50. }
  51. public void writeLong(long v) throws IOException {
  52. writeObject(v);
  53. }
  54. public void writeFloat(float v) throws IOException {
  55. writeObject(v);
  56. }
  57. public void writeDouble(double v) throws IOException {
  58. writeObject(v);
  59. }
  60. public void writeUTF(String v) throws IOException {
  61. writeObject(v);
  62. }
  63. public void writeBytes(byte[] b) throws IOException {
  64. writer.println(new String(b));
  65. }
  66. public void writeBytes(byte[] b, int off, int len) throws IOException {
  67. writer.println(new String(b, off, len));
  68. }
  69. public void writeObject(Object obj) throws IOException {
  70. SerializeWriter out = new SerializeWriter();
  71. JSONSerializer serializer = new JSONSerializer(out);
  72. serializer.config(SerializerFeature.WriteEnumUsingToString, true);
  73. serializer.write(obj);
  74. out.writeTo(writer);
  75. writer.println();
  76. writer.flush();
  77. }
  78. public void flushBuffer() throws IOException {
  79. writer.flush();
  80. }
  81. }