1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.core; |
20 | |
|
21 | |
import java.io.Serializable; |
22 | |
|
23 | |
import javax.json.JsonArray; |
24 | |
import javax.json.JsonObject; |
25 | |
import javax.json.JsonStructure; |
26 | |
import javax.json.JsonWriter; |
27 | |
import javax.json.stream.JsonGenerator; |
28 | |
|
29 | |
class JsonWriterImpl implements JsonWriter, Serializable{ |
30 | |
private final JsonGenerator generator; |
31 | 1 | private boolean closed = false; |
32 | |
|
33 | 1 | JsonWriterImpl(final JsonGenerator generator) { |
34 | 1 | this.generator = generator; |
35 | 1 | } |
36 | |
|
37 | |
@Override |
38 | |
public void writeArray(final JsonArray array) { |
39 | 0 | checkClosed(); |
40 | 0 | generator.write(array); |
41 | 0 | close(); |
42 | 0 | } |
43 | |
|
44 | |
@Override |
45 | |
public void writeObject(final JsonObject object) { |
46 | 0 | checkClosed(); |
47 | 0 | generator.write(object); |
48 | 0 | close(); |
49 | 0 | } |
50 | |
|
51 | |
@Override |
52 | |
public void write(final JsonStructure value) { |
53 | 1 | checkClosed(); |
54 | 1 | generator.write(value); |
55 | 1 | close(); |
56 | 1 | } |
57 | |
|
58 | |
@Override |
59 | |
public void close() { |
60 | |
|
61 | 2 | if(!closed) { |
62 | 1 | closed = true; |
63 | 1 | generator.close(); |
64 | |
} |
65 | 2 | } |
66 | |
|
67 | |
private void checkClosed() { |
68 | 1 | if(closed) { |
69 | 0 | throw new IllegalStateException("writeArray(), writeObject(), write() or close() method was already called"); |
70 | |
} |
71 | |
|
72 | 1 | } |
73 | |
} |