PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/alibaba/fastjson/JSONWriter.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 151 lines | 119 code | 25 blank | 7 comment | 55 complexity | 659ab9af1f211869a72d163c861b9132 MD5 | raw file
  1. package com.alibaba.fastjson;
  2. import java.io.IOException;
  3. import java.io.Writer;
  4. import com.alibaba.fastjson.serializer.JSONSerializer;
  5. import com.alibaba.fastjson.serializer.SerializeWriter;
  6. public class JSONWriter {
  7. private SerializeWriter writer;
  8. private final Writer out;
  9. private JSONSerializer serializer;
  10. private Context context;
  11. public static enum State {
  12. BeginObject, //
  13. PropertyKey, //
  14. PropertyValue, //
  15. BeginArray, //
  16. ArrayValue
  17. }
  18. public static class Context {
  19. private final Context parent;
  20. private State state;
  21. public Context(Context parent, State state){
  22. this.parent = parent;
  23. this.state = state;
  24. }
  25. public Context getParent() {
  26. return parent;
  27. }
  28. public State getState() {
  29. return state;
  30. }
  31. public void setState(State state) {
  32. this.state = state;
  33. }
  34. }
  35. public JSONWriter(Writer out){
  36. this.out = out;
  37. writer = new SerializeWriter();
  38. serializer = new JSONSerializer(writer);
  39. }
  40. public void flush() throws IOException {
  41. writer.writeTo(out);
  42. writer = new SerializeWriter();
  43. serializer = new JSONSerializer(writer);
  44. }
  45. public void close() throws IOException {
  46. if (writer.size() != 0) {
  47. flush();
  48. }
  49. }
  50. public void writeStartObject() {
  51. if (context == null) {
  52. context = new Context(null, State.BeginObject);
  53. } else {
  54. if (context.getState() == State.PropertyKey) {
  55. writer.write(':');
  56. } else if (context.getState() == State.ArrayValue) {
  57. writer.write(',');
  58. } else if (context.getState() == State.BeginObject) {
  59. // skip
  60. } else if (context.getState() == State.BeginArray) {
  61. // skip
  62. } else {
  63. throw new JSONException("illegal state : " + context.getState());
  64. }
  65. context = new Context(context, State.BeginObject);
  66. }
  67. writer.write('{');
  68. }
  69. public void writeEndObject() {
  70. writer.write('}');
  71. context = context.getParent();
  72. if (context == null) {
  73. // skip
  74. } else if (context.getState() == State.PropertyKey) {
  75. context.setState(State.PropertyValue);
  76. } else if (context.getState() == State.BeginArray) {
  77. context.setState(State.ArrayValue);
  78. } else if (context.getState() == State.ArrayValue) {
  79. // skip
  80. }
  81. }
  82. public void writeKey(String key) {
  83. if (context.getState() == State.PropertyValue) {
  84. writer.write(',');
  85. }
  86. writer.writeString(key);
  87. context.setState(State.PropertyKey);
  88. }
  89. public void writeValue(Object object) {
  90. if (context.getState() == State.PropertyKey) {
  91. writer.write(':');
  92. }
  93. serializer.write(object);
  94. context.setState(State.PropertyValue);
  95. }
  96. public void writeStartArray() {
  97. if (context == null) {
  98. context = new Context(null, State.BeginArray);
  99. } else {
  100. if (context.getState() == State.PropertyKey) {
  101. writer.write(':');
  102. } else if (context.getState() == State.ArrayValue) {
  103. writer.write(',');
  104. } else if (context.getState() == State.BeginArray) {
  105. // skipe
  106. } else {
  107. throw new JSONException("illegal state : " + context.getState());
  108. }
  109. context = new Context(context, State.BeginArray);
  110. }
  111. writer.write('[');
  112. }
  113. public void writeEndArray() {
  114. writer.write(']');
  115. context = context.getParent();
  116. if (context == null) {
  117. // skip
  118. } else if (context.getState() == State.PropertyKey) {
  119. context.setState(State.PropertyValue);
  120. } else if (context.getState() == State.BeginArray) {
  121. context.setState(State.ArrayValue);
  122. } else if (context.getState() == State.ArrayValue) {
  123. // skip
  124. }
  125. }
  126. }