Subversion Repositories bacoAlunos

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1878 jmachado 1
package com.owlike.genson.stream;
2
 
3
import java.io.Closeable;
4
import java.io.Flushable;
5
import java.io.IOException;
6
 
7
/**
8
 * ObjectWriter defines the api allowing to write data to different format and the contract for
9
 * classes that implement ObjectWriter to provide different formats support. Implementations are
10
 * extremely efficient as they use low level stream operations and optimizations. If you want
11
 * optimal performance you can directly use the streaming api (ObjectWriter and {@link ObjectReader}
12
 * ) without the databind support that comes with the converters. This will be very close in terms
13
 * of performance as writing manually formated data to the stream.
14
 * <p/>
15
 * If you want to write the array new int[1, 2, 3] to the stream with ObjectWriter:
16
 * <p/>
17
 * <pre>
18
 * writer.beginArray().writeValue(1).writeValue(2).writeValue(3).endArray();
19
 * </pre>
20
 * <p/>
21
 * And to write Person (we simplify, in practice you must handle null values):
22
 * <p/>
23
 * <pre>
24
 * class Person {
25
 *      public static void main(String[] args) {
26
 *              // we will write from Person to json string
27
 *              Person p = new Person();
28
 *              p.name = &quot;eugen&quot;;
29
 *              p.age = 26;
30
 *              p.childrenYearOfBirth = new ArrayList&lt;Integer&gt;();
31
 *              StringWriter sw = new StringWriter();
32
 *              ObjectWriter writer = new JsonWriter(sw);
33
 *              p.write(writer);
34
 *              writer.flush();
35
 *              writer.close();
36
 *              // will write {&quot;name&quot;:&quot;eugen&quot;,&quot;age&quot;:26,&quot;childrenYearOfBirth&quot;:[]}
37
 *              System.out.println(sw.toString());
38
 *  }
39
 *
40
 *      String name;
41
 *      int age;
42
 *      List&lt;Integer&gt; childrenYearOfBirth;
43
 *
44
 *      public void write(ObjectWriter writer) {
45
 *              writer.beginObject().writeName(&quot;name&quot;);
46
 *              if (name == null) writer.writeNull();
47
 *              else writer.writeValue(name)
48
 *              writer.writeName(&quot;age&quot;).writeAge(age)
49
 *                              .writeName(&quot;childrenYearOfBirth&quot;);
50
 *              if (childrenYearOfBirth == null) writer.writeNull();
51
 *              else {
52
 *                      writer.beginArray();
53
 *                      for (Integer year : childrenYearOfBirth)
54
 *                              writer.writeValue(year);
55
 *                      writer.endArray()
56
 *    }
57
 *              writer.endObject();
58
 *  }
59
 * }
60
 * </pre>
61
 * <p/>
62
 * Be careful if you instantiate ObjectWriter your self you are responsible of flushing and closing
63
 * the stream.
64
 *
65
 * @author eugen
66
 * @see JsonWriter
67
 * @see ObjectReader
68
 * @see JsonReader
69
 */
70
public interface ObjectWriter {
71
 
72
  /**
73
   * Starts to write an array (use it also for collections). An array is a suite of values that
74
   * may be literals, arrays or objects. When you finished writing the values don't forget to call
75
   * endArray().
76
   *
77
   * @return a reference to this allowing to chain method calls.
78
   * @throws JsonStreamException if trying to produce invalid json
79
   */
80
  public ObjectWriter beginArray();
81
 
82
  /**
83
   * Ends the array, if beginArray was not called, implementations should throw an exception.
84
   *
85
   * @return a reference to this allowing to chain method calls.
86
   * @throws JsonStreamException if trying to produce invalid json
87
   */
88
  public ObjectWriter endArray();
89
 
90
  /**
91
   * Starts a object, objects are a suite of name/value pairs, values may be literals, arrays or
92
   * objects. Don't forget to call endObject.
93
   *
94
   * @return a reference to this allowing to chain method calls.
95
   * @throws JsonStreamException if trying to produce invalid json
96
   */
97
  public ObjectWriter beginObject();
98
 
99
  /**
100
   * Ends the object being written, if beginObject was not called an exception will be throwed.
101
   *
102
   * @return a reference to this allowing to chain method calls.
103
   * @throws JsonStreamException if trying to produce invalid json
104
   */
105
  public ObjectWriter endObject();
106
 
107
  /**
108
   * Writes the name of a property. Names can be written only in objects and must be called before
109
   * writing the properties value.
110
   *
111
   * @param name a non null String
112
   * @return a reference to this, allowing to chain method calls.
113
   * @throws JsonStreamException if trying to produce invalid json
114
   */
115
  public ObjectWriter writeName(1.5.0/docs/api/java/lang/String.html">String name);
116
 
117
  /**
118
   * Will write the name without escaping special characters, assuming it has been done by the caller or the string
119
   * doesn't contain any character needing to be escaped.
120
   * @param name a non null escaped String
121
   * @return a reference to this, allowing to chain method calls.
122
   * @throws JsonStreamException if trying to produce invalid json
123
   */
124
  public ObjectWriter writeEscapedName(char[] name);
125
 
126
  /**
127
   * Writes a value to the stream. Values can be written in arrays and in objects (after writing
128
   * the name).
129
   *
130
   * @param value to write.
131
   * @return a reference to this, allowing to chain method calls.
132
   * @throws JsonStreamException if trying to produce invalid json
133
   */
134
  public ObjectWriter writeValue(int value);
135
 
136
  /**
137
   * See {@link #writeValue(int)}.
138
   *
139
   * @throws JsonStreamException if trying to produce invalid json
140
   * @see #writeValue(int)
141
   */
142
  public ObjectWriter writeValue(double value);
143
 
144
  /**
145
   * See {@link #writeValue(int)}.
146
   *
147
   * @throws JsonStreamException if trying to produce invalid json
148
   * @see #writeValue(int)
149
   */
150
  public ObjectWriter writeValue(long value);
151
 
152
  /**
153
   * See {@link #writeValue(int)}.
154
   *
155
   * @throws JsonStreamException if trying to produce invalid json
156
   * @see #writeValue(int)
157
   */
158
  public ObjectWriter writeValue(short value);
159
 
160
  /**
161
   * @see #writeValue(int)
162
   */
163
  public ObjectWriter writeValue(float value);
164
 
165
  /**
166
   * See {@link #writeValue(int)}.
167
   *
168
   * @throws JsonStreamException if trying to produce invalid json
169
   * @see #writeValue(int)
170
   */
171
  public ObjectWriter writeValue(boolean value);
172
 
173
  /**
174
   * @see #writeString(String)
175
   */
176
  public ObjectWriter writeBoolean(1.5.0/docs/api/java/lang/Boolean.html">Boolean value);
177
 
178
  /**
179
   * See {@link #writeValue(int)}.
180
   *
181
   * @throws JsonStreamException if trying to produce invalid json
182
   * @see #writeValue(int)
183
   */
184
  public ObjectWriter writeValue(1.5.0/docs/api/java/lang/Number.html">Number value);
185
 
186
  /**
187
   * @see #writeString(String)
188
   */
189
  public ObjectWriter writeNumber(1.5.0/docs/api/java/lang/Number.html">Number value);
190
 
191
  /**
192
   * See {@link #writeValue(int)}.
193
   *
194
   * @throws JsonStreamException if trying to produce invalid json
195
   * @see #writeValue(int)
196
   */
197
  public ObjectWriter writeValue(1.5.0/docs/api/java/lang/String.html">String value);
198
 
199
  /**
200
   * Similar to writeValue(String) but is null safe, meaning that if the value is null,
201
   * then the write will call writeNull for you.
202
   */
203
  public ObjectWriter writeString(1.5.0/docs/api/java/lang/String.html">String value);
204
 
205
  /**
206
   * Writes an array of bytes as a base64 encoded string. See {@link #writeValue(int)}.
207
   *
208
   * @throws JsonStreamException if trying to produce invalid json
209
   * @see #writeValue(int)
210
   */
211
  public ObjectWriter writeValue(byte[] value);
212
 
213
  /**
214
   * @see #writeString(String)
215
   */
216
  public ObjectWriter writeBytes(byte[] value);
217
 
218
  /**
219
   * Writes value as is without any pre-processing, it's faster than {@link #writeValue(String)}
220
   * but should be used only if you know that it is safe.
221
   *
222
   * @throws JsonStreamException if trying to produce invalid json
223
   * @see #writeValue(int)
224
   */
225
  public ObjectWriter writeUnsafeValue(1.5.0/docs/api/java/lang/String.html">String value);
226
 
227
  /**
228
   * Must be called when a null value is encountered. Implementations will deal with the null
229
   * representation (just skip it or write null, etc).
230
   *
231
   * @return a reference to this allowing to chain method calls.
232
   * @throws JsonStreamException if trying to produce invalid json
233
   * @see #writeValue(int)
234
   */
235
  public ObjectWriter writeNull();
236
 
237
  /**
238
   * This method is a kind of cheat as it allows us to start writing metadata and then still be
239
   * able to call beginObject. This is mainly intended to be used in wrapped converters that want
240
   * to handle a part of the serialization and then let the chain continue. Have a look at <a
241
   * href=
242
   * "http://code.google.com/p/genson/source/browse/src/main/java/com/owlike/genson/convert/ClassMetadataConverter.java"
243
   * >ClassMetadataConverter</a>
244
   *
245
   * @return a reference to this allowing to chain method calls.
246
   * @throws JsonStreamException
247
   * @see #writeMetadata(String, String)
248
   */
249
  public ObjectWriter beginNextObjectMetadata();
250
 
251
  /**
252
   * Metadata is a suite of name/value pairs, names will be prepended with '@' (handled by the
253
   * library). Metadata feature is experimental for the moment so things may change a bit. The
254
   * signature will not, but the way it is implemented could... Actually the contract is that
255
   * metadata must be written first and only in objects. If it does not respect these conditions
256
   * ObjectReader won't be able to detect it as metadata. Here is an example of two ways to write
257
   * object metadata.
258
   * <p/>
259
   * <pre>
260
   * // here it is transparent for the library if you write metadata or something else, you must call
261
   * // beginObject before being able to start writing its metadata.
262
   * writer.beginObject().writeMetadata(&quot;doc&quot;, &quot;Object documentation bla bla...&quot;).writeName(&quot;name&quot;)
263
   *            .writeNull().endObject().flush();
264
   *
265
   * // previous example works fine, but if you want to write some metadata and still be able to call
266
   * // beginObject (for example in a converter you want to write some metadata and have all the existing
267
   * // continue to work with calling beginObject) you must use beginNextObjectMetadata.
268
   *
269
   * // written from a first converter
270
   * writer.beginNextObjectMetadata().writeMetadata(&quot;dataFromConverter1&quot;, &quot;writtenFromConverter1&quot;);
271
   * // written from a second converter after first one
272
   * writer.beginNextObjectMetadata().writeMetadata(&quot;dataFromConverter2&quot;, &quot;writtenFromConverter2&quot;);
273
   * // finally concrete properties will be written from a custom converter
274
   * writer.beginObject().writeName(&quot;name&quot;).writeNull().endObject().flush();
275
   * </pre>
276
   *
277
   * @param name  of the metadata property, should not start with '@', the library will add it.
278
   * @param value of the metadata property.
279
   * @return a reference to this allowing to chain method calls.
280
   * @throws JsonStreamException
281
   * @see #beginNextObjectMetadata()
282
   */
283
  public ObjectWriter writeMetadata(1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/String.html">String value);
284
 
285
  /**
286
   * @see #writeString(String, String)
287
   */
288
  public ObjectWriter writeBoolean(1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/Boolean.html">Boolean value);
289
 
290
  /**
291
   * @see #writeString(String, String)
292
   */
293
  public ObjectWriter writeNumber(1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/Number.html">Number value);
294
 
295
  /**
296
   * Will write the name and the value, it is just a shortcut for writer.writeName("key").writeString(value).
297
   * Note if the value is null, writeNull is used.
298
   */
299
  public ObjectWriter writeString(1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/String.html">String value);
300
 
301
  /**
302
   * @see #writeString(String, String)
303
   */
304
  public ObjectWriter writeBytes(1.5.0/docs/api/java/lang/String.html">String name, byte[] value);
305
 
306
  public void flush();
307
 
308
  public void close();
309
 
310
  public JsonType enclosingType();
311
}