1 package com.fasterxml.jackson.core.util;
2
3 import java.io.IOException;
4
5 import com.fasterxml.jackson.core.JsonGenerator;
6 import com.fasterxml.jackson.core.PrettyPrinter;
7
8 /**
9 * {@link PrettyPrinter} implementation that adds no indentation,
10 * just implements everything necessary for value output to work
11 * as expected, and provide simpler extension points to allow
12 * for creating simple custom implementations that add specific
13 * decoration or overrides. Since behavior then is very similar
14 * to using no pretty printer at all, usually sub-classes are used.
15 *<p>
16 * Beyond purely minimal implementation, there is limited amount of
17 * configurability which may be useful for actual use: for example,
18 * it is possible to redefine separator used between root-level
19 * values (default is single space; can be changed to line-feed).
20 *<p>
21 * Note: does NOT implement {@link Instantiatable} since this is
22 * a stateless implementation; that is, a single instance can be
23 * shared between threads.
24 */
25 public class MinimalPrettyPrinter
26 implements PrettyPrinter, java.io.Serializable
27 {
28 private static final long serialVersionUID = 1L;
29
30 protected String _rootValueSeparator;
31
32 /**
33 * @since 2.9
34 */
35 protected Separators _separators;
36
37 /*
38 /**********************************************************
39 /* Life-cycle, construction, configuration
40 /**********************************************************
41 */
42
43 public MinimalPrettyPrinter() {
44 this(DEFAULT_ROOT_VALUE_SEPARATOR.toString());
45 }
46
47 public MinimalPrettyPrinter(String rootValueSeparator) {
48 _rootValueSeparator = rootValueSeparator;
49 _separators = DEFAULT_SEPARATORS;
50 }
51
52 public void setRootValueSeparator(String sep) {
53 _rootValueSeparator = sep;
54 }
55
56 /**
57 * @since 2.9
58 */
59 public MinimalPrettyPrinter setSeparators(Separators separators) {
60 _separators = separators;
61 return this;
62 }
63
64 /*
65 /**********************************************************
66 /* PrettyPrinter impl
67 /**********************************************************
68 */
69
70 @Override
71 public void writeRootValueSeparator(JsonGenerator g) throws IOException
72 {
73 if (_rootValueSeparator != null) {
74 g.writeRaw(_rootValueSeparator);
75 }
76 }
77
78 @Override
79 public void writeStartObject(JsonGenerator g) throws IOException
80 {
81 g.writeRaw('{');
82 }
83
84 @Override
85 public void beforeObjectEntries(JsonGenerator g) throws IOException
86 {
87 // nothing special, since no indentation is added
88 }
89
90 /**
91 * Method called after an object field has been output, but
92 * before the value is output.
93 *<p>
94 * Default handling will just output a single
95 * colon to separate the two, without additional spaces.
96 */
97 @Override
98 public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException
99 {
100 g.writeRaw(_separators.getObjectFieldValueSeparator());
101 }
102
103 /**
104 * Method called after an object entry (field:value) has been completely
105 * output, and before another value is to be output.
106 *<p>
107 * Default handling (without pretty-printing) will output a single
108 * comma to separate the two.
109 */
110 @Override
111 public void writeObjectEntrySeparator(JsonGenerator g) throws IOException
112 {
113 g.writeRaw(_separators.getObjectEntrySeparator());
114 }
115
116 @Override
117 public void writeEndObject(JsonGenerator g, int nrOfEntries) throws IOException
118 {
119 g.writeRaw('}');
120 }
121
122 @Override
123 public void writeStartArray(JsonGenerator g) throws IOException
124 {
125 g.writeRaw('[');
126 }
127
128 @Override
129 public void beforeArrayValues(JsonGenerator g) throws IOException
130 {
131 // nothing special, since no indentation is added
132 }
133
134 /**
135 * Method called after an array value has been completely
136 * output, and before another value is to be output.
137 *<p>
138 * Default handling (without pretty-printing) will output a single
139 * comma to separate values.
140 */
141 @Override
142 public void writeArrayValueSeparator(JsonGenerator g) throws IOException
143 {
144 g.writeRaw(_separators.getArrayValueSeparator());
145 }
146
147 @Override
148 public void writeEndArray(JsonGenerator g, int nrOfValues) throws IOException
149 {
150 g.writeRaw(']');
151 }
152 }
153