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.OutputStream; |
22 | |
import java.io.OutputStreamWriter; |
23 | |
import java.io.Serializable; |
24 | |
import java.io.Writer; |
25 | |
import java.nio.charset.Charset; |
26 | |
import java.util.Collections; |
27 | |
import java.util.HashMap; |
28 | |
import java.util.Map; |
29 | |
|
30 | |
import javax.json.JsonWriter; |
31 | |
import javax.json.JsonWriterFactory; |
32 | |
import javax.json.stream.JsonGenerator; |
33 | |
import javax.json.stream.JsonGeneratorFactory; |
34 | |
|
35 | |
class JsonWriterFactoryImpl implements JsonWriterFactory, Serializable { |
36 | 1 | private static final Charset UTF8_CHARSET = Charset.forName("UTF-8"); |
37 | 1 | private final Map<String, Object> internalConfig = new HashMap<String, Object>(); |
38 | 1 | private static final String[] SUPPORTED_CONFIG_KEYS = new String[] { |
39 | |
|
40 | |
JsonGenerator.PRETTY_PRINTING |
41 | |
|
42 | |
}; |
43 | |
private final JsonGeneratorFactory factory; |
44 | |
|
45 | 1 | JsonWriterFactoryImpl(final Map<String, ?> config) { |
46 | 1 | if (config != null) { |
47 | |
|
48 | 0 | for (final String configKey : SUPPORTED_CONFIG_KEYS) { |
49 | 0 | if (config.containsKey(configKey)) { |
50 | 0 | internalConfig.put(configKey, config.get(configKey)); |
51 | |
} |
52 | |
} |
53 | |
|
54 | 0 | this.factory = new JsonGeneratorFactoryImpl(internalConfig); |
55 | |
} else { |
56 | 1 | this.factory = new JsonGeneratorFactoryImpl(null); |
57 | |
} |
58 | |
|
59 | 1 | } |
60 | |
|
61 | |
@Override |
62 | |
public JsonWriter createWriter(final Writer writer) { |
63 | 1 | return new JsonWriterImpl(factory.createGenerator(writer)); |
64 | |
} |
65 | |
|
66 | |
@Override |
67 | |
public JsonWriter createWriter(final OutputStream out) { |
68 | 1 | return createWriter(new OutputStreamWriter(out, UTF8_CHARSET)); |
69 | |
} |
70 | |
|
71 | |
@Override |
72 | |
public JsonWriter createWriter(final OutputStream out, final Charset charset) { |
73 | 0 | return createWriter(new OutputStreamWriter(out, charset)); |
74 | |
} |
75 | |
|
76 | |
@Override |
77 | |
public Map<String, ?> getConfigInUse() { |
78 | 0 | return Collections.unmodifiableMap(internalConfig); |
79 | |
} |
80 | |
} |