Skip to content

Commit 27f9fb3

Browse files
committed
Further clean up of shared GeneratorBase
1 parent 303b7ed commit 27f9fb3

File tree

4 files changed

+87
-96
lines changed

4 files changed

+87
-96
lines changed

src/main/java/com/fasterxml/jackson/core/base/GeneratorBase.java

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,7 @@ public abstract class GeneratorBase extends JsonGenerator
1919
public final static int SURR2_FIRST = 0xDC00;
2020
public final static int SURR2_LAST = 0xDFFF;
2121

22-
/**
23-
* Set of feature masks related to features that need updates of other
24-
* local configuration or state.
25-
*/
26-
protected final static int DERIVED_FEATURES_MASK =
27-
StreamWriteFeature.WRITE_NUMBERS_AS_STRINGS.getMask()
28-
| StreamWriteFeature.STRICT_DUPLICATE_DETECTION.getMask()
29-
;
30-
31-
// // // Constants for validation messages (since 2.6)
22+
// // // Constants for validation messages
3223

3324
protected final static String WRITE_BINARY = "write a binary value";
3425
protected final static String WRITE_BOOLEAN = "write a boolean value";
@@ -66,13 +57,6 @@ public abstract class GeneratorBase extends JsonGenerator
6657
*/
6758
protected int _streamWriteFeatures;
6859

69-
/**
70-
* Flag set to indicate that implicit conversion from number
71-
* to JSON String is needed (as per
72-
* {@link com.fasterxml.jackson.core.StreamWriteFeature#WRITE_NUMBERS_AS_STRINGS}).
73-
*/
74-
protected boolean _cfgNumbersAsStrings;
75-
7660
/*
7761
/**********************************************************************
7862
/* State
@@ -92,16 +76,10 @@ public abstract class GeneratorBase extends JsonGenerator
9276
/**********************************************************************
9377
*/
9478

95-
protected GeneratorBase(ObjectWriteContext writeCtxt, int features) {
79+
protected GeneratorBase(ObjectWriteContext writeCtxt, int streamWriteFeatures) {
9680
super();
9781
_objectWriteContext = writeCtxt;
98-
_streamWriteFeatures = features;
99-
/*
100-
DupDetector dups = StreamWriteFeature.STRICT_DUPLICATE_DETECTION.enabledIn(features)
101-
? DupDetector.rootDetector(this) : null;
102-
_outputContext = JsonWriteContext.createRootContext(dups);
103-
*/
104-
_cfgNumbersAsStrings = StreamWriteFeature.WRITE_NUMBERS_AS_STRINGS.enabledIn(features);
82+
_streamWriteFeatures = streamWriteFeatures;
10583
}
10684

10785
/*
@@ -113,31 +91,17 @@ protected GeneratorBase(ObjectWriteContext writeCtxt, int features) {
11391
@Override public final boolean isEnabled(StreamWriteFeature f) { return (_streamWriteFeatures & f.getMask()) != 0; }
11492
@Override public int streamWriteFeatures() { return _streamWriteFeatures; }
11593

116-
@Override
117-
public int formatWriteFeatures() { return 0; }
94+
// public int formatWriteFeatures();
11895

11996
@Override
12097
public JsonGenerator enable(StreamWriteFeature f) {
121-
final int mask = f.getMask();
122-
_streamWriteFeatures |= mask;
123-
if ((mask & DERIVED_FEATURES_MASK) != 0) {
124-
// why not switch? Requires addition of a generated class, alas
125-
if (f == StreamWriteFeature.WRITE_NUMBERS_AS_STRINGS) {
126-
_cfgNumbersAsStrings = true;
127-
}
128-
}
98+
_streamWriteFeatures |= f.getMask();
12999
return this;
130100
}
131101

132102
@Override
133103
public JsonGenerator disable(StreamWriteFeature f) {
134-
final int mask = f.getMask();
135-
_streamWriteFeatures &= ~mask;
136-
if ((mask & DERIVED_FEATURES_MASK) != 0) {
137-
if (f == StreamWriteFeature.WRITE_NUMBERS_AS_STRINGS) {
138-
_cfgNumbersAsStrings = false;
139-
}
140-
}
104+
_streamWriteFeatures &= ~f.getMask();
141105
return this;
142106
}
143107

@@ -168,18 +132,14 @@ public JsonGenerator disable(StreamWriteFeature f) {
168132
@Override
169133
public void writeStartArray(Object forValue, int size) throws IOException {
170134
writeStartArray(size);
171-
if (forValue != null) {
172-
setCurrentValue(forValue);
173-
}
135+
setCurrentValue(forValue);
174136
}
175137

176138
@Override
177139
public void writeStartObject(Object forValue) throws IOException
178140
{
179141
writeStartObject();
180-
if (forValue != null) {
181-
setCurrentValue(forValue);
182-
}
142+
setCurrentValue(forValue);
183143
}
184144

185145
/*

src/main/java/com/fasterxml/jackson/core/json/JsonGeneratorImpl.java

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
8282
/**********************************************************************
8383
*/
8484

85+
/**
86+
* Object that handles pretty-printing (usually additional
87+
* white space to make results more human-readable) during
88+
* output. If null, no pretty-printing is done.
89+
*/
90+
protected PrettyPrinter _cfgPrettyPrinter;
91+
8592
/**
8693
* Separator to use, if any, between root-level values.
8794
*/
@@ -94,11 +101,11 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
94101
protected boolean _cfgUnqNames;
95102

96103
/**
97-
* Object that handles pretty-printing (usually additional
98-
* white space to make results more human-readable) during
99-
* output. If null, no pretty-printing is done.
104+
* Flag set to indicate that implicit conversion from number
105+
* to JSON String is needed (as per
106+
* {@link com.fasterxml.jackson.core.StreamWriteFeature#WRITE_NUMBERS_AS_STRINGS}).
100107
*/
101-
protected PrettyPrinter _cfgPrettyPrinter;
108+
protected boolean _cfgNumbersAsStrings;
102109

103110
/*
104111
/**********************************************************************
@@ -109,7 +116,7 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
109116
/**
110117
* Object that keeps track of the current contextual state of the generator.
111118
*/
112-
protected JsonWriteContext _outputContext;
119+
protected JsonWriteContext _tokenWriteContext;
113120

114121
/*
115122
/**********************************************************************
@@ -133,13 +140,14 @@ public JsonGeneratorImpl(ObjectWriteContext writeCtxt, IOContext ctxt,
133140
}
134141
_maximumNonEscapedChar = maxNonEscaped;
135142
_cfgUnqNames = !JsonWriteFeature.QUOTE_FIELD_NAMES.enabledIn(formatWriteFeatures);
143+
_cfgNumbersAsStrings = StreamWriteFeature.WRITE_NUMBERS_AS_STRINGS.enabledIn(streamWriteFeatures);
136144
_rootValueSeparator = rootValueSeparator;
137145

138146
_cfgPrettyPrinter = pp;
139147

140148
final DupDetector dups = StreamWriteFeature.STRICT_DUPLICATE_DETECTION.enabledIn(streamWriteFeatures)
141149
? DupDetector.rootDetector(this) : null;
142-
_outputContext = JsonWriteContext.createRootContext(dups);
150+
_tokenWriteContext = JsonWriteContext.createRootContext(dups);
143151

144152
// 03-Oct-2017, tatu: Not clean (shouldn't call non-static methods from ctor),
145153
// but for now best way to avoid code duplication
@@ -160,6 +168,29 @@ public JsonGeneratorImpl(ObjectWriteContext writeCtxt, IOContext ctxt,
160168
/**********************************************************************
161169
*/
162170

171+
@Override
172+
public JsonGenerator enable(StreamWriteFeature f) {
173+
super.enable(f);
174+
if (f == StreamWriteFeature.WRITE_NUMBERS_AS_STRINGS) {
175+
_cfgNumbersAsStrings = true;
176+
}
177+
return this;
178+
}
179+
180+
@Override
181+
public JsonGenerator disable(StreamWriteFeature f) {
182+
super.disable(f);
183+
if (f == StreamWriteFeature.WRITE_NUMBERS_AS_STRINGS) {
184+
_cfgNumbersAsStrings = false;
185+
}
186+
return this;
187+
}
188+
189+
@Override
190+
public int formatWriteFeatures() {
191+
return _formatWriteFeatures;
192+
}
193+
163194
@Override
164195
public JsonGenerator setHighestNonEscapedChar(int charCode) {
165196
_maximumNonEscapedChar = (charCode < 0) ? 0 : charCode;
@@ -190,16 +221,16 @@ public CharacterEscapes getCharacterEscapes() {
190221
*/
191222

192223
@Override
193-
public final TokenStreamContext getOutputContext() { return _outputContext; }
224+
public final TokenStreamContext getOutputContext() { return _tokenWriteContext; }
194225

195226
@Override
196227
public final Object getCurrentValue() {
197-
return _outputContext.getCurrentValue();
228+
return _tokenWriteContext.getCurrentValue();
198229
}
199230

200231
@Override
201232
public final void setCurrentValue(Object v) {
202-
_outputContext.setCurrentValue(v);
233+
_tokenWriteContext.setCurrentValue(v);
203234
}
204235

205236
/*
@@ -238,9 +269,9 @@ protected void _verifyPrettyValueWrite(String typeMsg, int status) throws IOExce
238269
break;
239270
case JsonWriteContext.STATUS_OK_AS_IS:
240271
// First entry, but of which context?
241-
if (_outputContext.inArray()) {
272+
if (_tokenWriteContext.inArray()) {
242273
_cfgPrettyPrinter.beforeArrayValues(this);
243-
} else if (_outputContext.inObject()) {
274+
} else if (_tokenWriteContext.inObject()) {
244275
_cfgPrettyPrinter.beforeObjectEntries(this);
245276
}
246277
break;
@@ -256,6 +287,6 @@ protected void _verifyPrettyValueWrite(String typeMsg, int status) throws IOExce
256287
protected void _reportCantWriteValueExpectName(String typeMsg) throws IOException
257288
{
258289
_reportError(String.format("Can not %s, expecting field name (context: %s)",
259-
typeMsg, _outputContext.typeDesc()));
290+
typeMsg, _tokenWriteContext.typeDesc()));
260291
}
261292
}

src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public void writeFieldName(String name) throws IOException
200200
_writePPFieldName(name);
201201
return;
202202
}
203-
final int status = _outputContext.writeFieldName(name);
203+
final int status = _tokenWriteContext.writeFieldName(name);
204204
if (status == JsonWriteContext.STATUS_EXPECT_VALUE) {
205205
_reportError("Can not write a field name, expecting a value");
206206
}
@@ -250,7 +250,7 @@ public void writeFieldName(SerializableString name) throws IOException
250250
_writePPFieldName(name);
251251
return;
252252
}
253-
final int status = _outputContext.writeFieldName(name.getValue());
253+
final int status = _tokenWriteContext.writeFieldName(name.getValue());
254254
if (status == JsonWriteContext.STATUS_EXPECT_VALUE) {
255255
_reportError("Can not write a field name, expecting a value");
256256
}
@@ -299,7 +299,7 @@ private final void _writeUnq(SerializableString name) throws IOException {
299299
public final void writeStartArray() throws IOException
300300
{
301301
_verifyValueWrite("start an array");
302-
_outputContext = _outputContext.createChildArrayContext();
302+
_tokenWriteContext = _tokenWriteContext.createChildArrayContext();
303303
if (_cfgPrettyPrinter != null) {
304304
_cfgPrettyPrinter.writeStartArray(this);
305305
} else {
@@ -314,7 +314,7 @@ public final void writeStartArray() throws IOException
314314
public final void writeStartArray(int len) throws IOException
315315
{
316316
_verifyValueWrite("start an array");
317-
_outputContext = _outputContext.createChildArrayContext();
317+
_tokenWriteContext = _tokenWriteContext.createChildArrayContext();
318318
if (_cfgPrettyPrinter != null) {
319319
_cfgPrettyPrinter.writeStartArray(this);
320320
} else {
@@ -329,7 +329,7 @@ public final void writeStartArray(int len) throws IOException
329329
public final void writeStartArray(Object forValue, int len) throws IOException
330330
{
331331
_verifyValueWrite("start an array");
332-
_outputContext = _outputContext.createChildArrayContext(forValue);
332+
_tokenWriteContext = _tokenWriteContext.createChildArrayContext(forValue);
333333
if (_cfgPrettyPrinter != null) {
334334
_cfgPrettyPrinter.writeStartArray(this);
335335
} else {
@@ -343,25 +343,25 @@ public final void writeStartArray(Object forValue, int len) throws IOException
343343
@Override
344344
public final void writeEndArray() throws IOException
345345
{
346-
if (!_outputContext.inArray()) {
347-
_reportError("Current context not Array but "+_outputContext.typeDesc());
346+
if (!_tokenWriteContext.inArray()) {
347+
_reportError("Current context not Array but "+_tokenWriteContext.typeDesc());
348348
}
349349
if (_cfgPrettyPrinter != null) {
350-
_cfgPrettyPrinter.writeEndArray(this, _outputContext.getEntryCount());
350+
_cfgPrettyPrinter.writeEndArray(this, _tokenWriteContext.getEntryCount());
351351
} else {
352352
if (_outputTail >= _outputEnd) {
353353
_flushBuffer();
354354
}
355355
_outputBuffer[_outputTail++] = BYTE_RBRACKET;
356356
}
357-
_outputContext = _outputContext.clearAndGetParent();
357+
_tokenWriteContext = _tokenWriteContext.clearAndGetParent();
358358
}
359359

360360
@Override
361361
public final void writeStartObject() throws IOException
362362
{
363363
_verifyValueWrite("start an object");
364-
_outputContext = _outputContext.createChildObjectContext();
364+
_tokenWriteContext = _tokenWriteContext.createChildObjectContext();
365365
if (_cfgPrettyPrinter != null) {
366366
_cfgPrettyPrinter.writeStartObject(this);
367367
} else {
@@ -376,8 +376,8 @@ public final void writeStartObject() throws IOException
376376
public void writeStartObject(Object forValue) throws IOException
377377
{
378378
_verifyValueWrite("start an object");
379-
JsonWriteContext ctxt = _outputContext.createChildObjectContext(forValue);
380-
_outputContext = ctxt;
379+
JsonWriteContext ctxt = _tokenWriteContext.createChildObjectContext(forValue);
380+
_tokenWriteContext = ctxt;
381381
if (_cfgPrettyPrinter != null) {
382382
_cfgPrettyPrinter.writeStartObject(this);
383383
} else {
@@ -391,18 +391,18 @@ public void writeStartObject(Object forValue) throws IOException
391391
@Override
392392
public final void writeEndObject() throws IOException
393393
{
394-
if (!_outputContext.inObject()) {
395-
_reportError("Current context not Object but "+_outputContext.typeDesc());
394+
if (!_tokenWriteContext.inObject()) {
395+
_reportError("Current context not Object but "+_tokenWriteContext.typeDesc());
396396
}
397397
if (_cfgPrettyPrinter != null) {
398-
_cfgPrettyPrinter.writeEndObject(this, _outputContext.getEntryCount());
398+
_cfgPrettyPrinter.writeEndObject(this, _tokenWriteContext.getEntryCount());
399399
} else {
400400
if (_outputTail >= _outputEnd) {
401401
_flushBuffer();
402402
}
403403
_outputBuffer[_outputTail++] = BYTE_RCURLY;
404404
}
405-
_outputContext = _outputContext.clearAndGetParent();
405+
_tokenWriteContext = _tokenWriteContext.clearAndGetParent();
406406
}
407407

408408
/**
@@ -411,7 +411,7 @@ public final void writeEndObject() throws IOException
411411
*/
412412
protected final void _writePPFieldName(String name) throws IOException
413413
{
414-
int status = _outputContext.writeFieldName(name);
414+
int status = _tokenWriteContext.writeFieldName(name);
415415
if (status == JsonWriteContext.STATUS_EXPECT_VALUE) {
416416
_reportError("Can not write a field name, expecting a value");
417417
}
@@ -451,7 +451,7 @@ protected final void _writePPFieldName(String name) throws IOException
451451

452452
protected final void _writePPFieldName(SerializableString name) throws IOException
453453
{
454-
final int status = _outputContext.writeFieldName(name.getValue());
454+
final int status = _tokenWriteContext.writeFieldName(name.getValue());
455455
if (status == JsonWriteContext.STATUS_EXPECT_VALUE) {
456456
_reportError("Can not write a field name, expecting a value");
457457
}
@@ -1101,7 +1101,7 @@ public void writeNull() throws IOException
11011101
@Override
11021102
protected final void _verifyValueWrite(String typeMsg) throws IOException
11031103
{
1104-
final int status = _outputContext.writeValue();
1104+
final int status = _tokenWriteContext.writeValue();
11051105
if (_cfgPrettyPrinter != null) {
11061106
// Otherwise, pretty printer knows what to do...
11071107
_verifyPrettyValueWrite(typeMsg, status);

0 commit comments

Comments
 (0)