/modeshape-schematic/src/main/java/org/modeshape/schematic/internal/document/JsonWriter.java

https://github.com/kbachl/modeshape · Java · 161 lines · 25 code · 6 blank · 130 comment · 0 complexity · b21534d52715fe864f4637ece74e82dc MD5 · raw file

  1. /*
  2. * ModeShape (http://www.modeshape.org)
  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 org.modeshape.schematic.internal.document;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.io.Writer;
  20. import java.util.Date;
  21. import java.util.UUID;
  22. import java.util.regex.Pattern;
  23. import org.modeshape.schematic.document.Binary;
  24. import org.modeshape.schematic.document.Code;
  25. import org.modeshape.schematic.document.CodeWithScope;
  26. import org.modeshape.schematic.document.Document;
  27. import org.modeshape.schematic.document.MaxKey;
  28. import org.modeshape.schematic.document.MinKey;
  29. import org.modeshape.schematic.document.ObjectId;
  30. import org.modeshape.schematic.document.Symbol;
  31. import org.modeshape.schematic.document.Timestamp;
  32. /**
  33. * A component that writes modified JSON representations from the in-memory {@link Document} representation.
  34. * <p>
  35. * The modified JSON format is nearly identical to the JSON serialization used by MongoDB. All standard JSON values are written as
  36. * expected, but the types unique to BSON are written as follows:
  37. * <table border="1" cellspacing="0" cellpadding="3">
  38. * <tr>
  39. * <th>BSON Type</th>
  40. * <th>Class</th>
  41. * <th>Format</th>
  42. * <th>Example</th>
  43. * </tr>
  44. * <tr>
  45. * <td>Symbol</td>
  46. * <td>{@link Symbol}</td>
  47. * <td>"<i>value</i>"</td>
  48. * <td>"The quick brown fox"</td>
  49. * </tr>
  50. * <tr>
  51. * <td>Regular Expression</td>
  52. * <td>{@link Pattern}</td>
  53. * <td>{ "$regex" : "<i>pattern</i>", "$options" : "<i>flags</i>" }</td>
  54. * <td>{ "$regex" : "[CH]at\sin", "$options" : "im" }</td>
  55. * </tr>
  56. * <tr>
  57. * <td>Date</td>
  58. * <td>{@link Date}</td>
  59. * <td>{ "$date" : "<i>yyyy-MM-dd</i>T<i>HH:mm:ss</i>Z" }</td>
  60. * <td>{ "$date" : "2011-06-11T08:44:25Z" }</td>
  61. * </tr>
  62. * <tr>
  63. * <td>Timestamp</td>
  64. * <td>{@link Timestamp}</td>
  65. * <td>{ "$ts" : <i>timeValue</i>, "$inc" : <i>incValue</i> }</td>
  66. * <td>"\/TS("2011-06-11T08:44:25Z")\/"</td>
  67. * </tr>
  68. * <tr>
  69. * <td>ObjectId</td>
  70. * <td>{@link ObjectId}</td>
  71. * <td>{ "$oid" : "<i>12bytesOfIdInBase16</i>" }</td>
  72. * <td>{ "$oid" : "0000012c0000c8000900000f" }</td>
  73. * </tr>
  74. * <tr>
  75. * <td>Binary</td>
  76. * <td>{@link Binary}</td>
  77. * <td>{ "$type" : <i>typeAsInt</i>, "$base64" : "<i>bytesInBase64</i>" }"</td>
  78. * <td>{ "$type" : 0, "$base64" : "TWFuIGlzIGRpc3R" }"</td>
  79. * </tr>
  80. * <tr>
  81. * <td>UUID</td>
  82. * <td>{@link UUID}</td>
  83. * <td>{ "$uuid" : "<i>string-form-of-uuid</i>" }</td>
  84. * <td>{ "$uuid" : "09e0e949-bba4-459c-bb1d-9352e5ee8958" }</td>
  85. * </tr>
  86. * <tr>
  87. * <td>Code</td>
  88. * <td>{@link Code}</td>
  89. * <td>{ "$code" : "<i>code</i>" }</td>
  90. * <td>{ "$code" : "244-I2" }</td>
  91. * </tr>
  92. * <tr>
  93. * <td>CodeWithScope</td>
  94. * <td>{@link CodeWithScope}</td>
  95. * <td>{ "$code" : "<i>code</i>", "$scope" : <i>scope document</i> }</td>
  96. * <td>{ "$code" : "244-I2", "$scope" : { "name" : "Joe" } }</td>
  97. * </tr>
  98. * <tr>
  99. * <td>MinKey</td>
  100. * <td>{@link MinKey}</td>
  101. * <td>"MinKey"</td>
  102. * <td>"MinKey"</td>
  103. * </tr>
  104. * <tr>
  105. * <td>MaxKey</td>
  106. * <td>{@link MaxKey}</td>
  107. * <td>"MaxKey"</td>
  108. * <td>"MaxKey"</td>
  109. * </tr>
  110. * <tr>
  111. * <td>Null value</td>
  112. * <td></td>
  113. * <td>null</td>
  114. * <td>null</td>
  115. * </tr>
  116. * </table>
  117. * </p>
  118. *
  119. * @author Randall Hauch <rhauch@redhat.com> (C) 2011 Red Hat Inc.
  120. */
  121. public interface JsonWriter {
  122. /**
  123. * Write to the supplied stream the modified JSON representation of the supplied in-memory {@link Document}.
  124. *
  125. * @param object the BSON object or BSON value; may not be null
  126. * @param stream the output stream; may not be null
  127. * @throws IOException if there was a problem reading from the stream
  128. */
  129. void write( Object object,
  130. OutputStream stream ) throws IOException;
  131. /**
  132. * Write to the supplied writer the modified JSON representation of the supplied in-memory {@link Document}.
  133. *
  134. * @param object the BSON object or BSON value; may not be null
  135. * @param writer the writer; may not be null
  136. * @throws IOException if there was a problem reading from the stream
  137. */
  138. void write( Object object,
  139. Writer writer ) throws IOException;
  140. /**
  141. * Write to the supplied string builder the modified JSON representation of the supplied in-memory {@link Document} .
  142. *
  143. * @param object the BSON object or BSON value; may not be null
  144. * @param builder the string builder; may not be null
  145. */
  146. void write( Object object,
  147. StringBuilder builder );
  148. /**
  149. * Write and return the modified JSON representation of the supplied in-memory {@link Document}.
  150. *
  151. * @param object the BSON object or BSON value; may not be null
  152. * @return the JSON string representation; never null
  153. */
  154. String write( Object object );
  155. }