Skip to content

Commit ce1b47d

Browse files
committed
Further promote write/output context out of shared GeneratorBase, to allow full redefinition by backends
1 parent 705123a commit ce1b47d

File tree

3 files changed

+93
-91
lines changed

3 files changed

+93
-91
lines changed

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

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.fasterxml.jackson.core.base;
22

33
import com.fasterxml.jackson.core.*;
4-
import com.fasterxml.jackson.core.json.DupDetector;
5-
import com.fasterxml.jackson.core.json.JsonWriteContext;
6-
import com.fasterxml.jackson.core.json.PackageVersion;
74
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
85

96
import java.io.IOException;
@@ -49,9 +46,9 @@ public abstract class GeneratorBase extends JsonGenerator
4946
protected final static int MAX_BIG_DECIMAL_SCALE = 9999;
5047

5148
/*
52-
/**********************************************************
49+
/**********************************************************************
5350
/* Configuration
54-
/**********************************************************
51+
/**********************************************************************
5552
*/
5653

5754
/**
@@ -77,17 +74,11 @@ public abstract class GeneratorBase extends JsonGenerator
7774
protected boolean _cfgNumbersAsStrings;
7875

7976
/*
80-
/**********************************************************
77+
/**********************************************************************
8178
/* State
82-
/**********************************************************
79+
/**********************************************************************
8380
*/
8481

85-
/**
86-
* Object that keeps track of the current contextual state
87-
* of the generator.
88-
*/
89-
protected JsonWriteContext _outputContext;
90-
9182
/**
9283
* Flag that indicates whether generator is closed or not. Gets
9384
* set when it is closed by an explicit call
@@ -96,52 +87,27 @@ public abstract class GeneratorBase extends JsonGenerator
9687
protected boolean _closed;
9788

9889
/*
99-
/**********************************************************
90+
/**********************************************************************
10091
/* Life-cycle
101-
/**********************************************************
92+
/**********************************************************************
10293
*/
10394

10495
protected GeneratorBase(ObjectWriteContext writeCtxt, int features) {
10596
super();
10697
_objectWriteContext = writeCtxt;
10798
_streamWriteFeatures = features;
99+
/*
108100
DupDetector dups = StreamWriteFeature.STRICT_DUPLICATE_DETECTION.enabledIn(features)
109101
? DupDetector.rootDetector(this) : null;
110102
_outputContext = JsonWriteContext.createRootContext(dups);
103+
*/
111104
_cfgNumbersAsStrings = StreamWriteFeature.WRITE_NUMBERS_AS_STRINGS.enabledIn(features);
112105
}
113106

114-
protected GeneratorBase(ObjectWriteContext writeCtxt, int features, JsonWriteContext ctxt) {
115-
super();
116-
_objectWriteContext = writeCtxt;
117-
_streamWriteFeatures = features;
118-
_outputContext = ctxt;
119-
_cfgNumbersAsStrings = StreamWriteFeature.WRITE_NUMBERS_AS_STRINGS.enabledIn(features);
120-
}
121-
122-
/**
123-
* Implemented with standard version number detection algorithm, typically using
124-
* a simple generated class, with information extracted from Maven project file
125-
* during build.
126-
*/
127-
@Override public Version version() { return PackageVersion.VERSION; }
128-
129-
@Override
130-
public Object getCurrentValue() {
131-
return _outputContext.getCurrentValue();
132-
}
133-
134-
@Override
135-
public void setCurrentValue(Object v) {
136-
if (_outputContext != null) {
137-
_outputContext.setCurrentValue(v);
138-
}
139-
}
140-
141107
/*
142-
/**********************************************************
108+
/**********************************************************************
143109
/* Configuration
144-
/**********************************************************
110+
/**********************************************************************
145111
*/
146112

147113
@Override public final boolean isEnabled(StreamWriteFeature f) { return (_streamWriteFeatures & f.getMask()) != 0; }
@@ -150,8 +116,6 @@ public void setCurrentValue(Object v) {
150116
@Override
151117
public int formatWriteFeatures() { return 0; }
152118

153-
//public JsonGenerator configure(Feature f, boolean state) { }
154-
155119
@Override
156120
public JsonGenerator enable(StreamWriteFeature f) {
157121
final int mask = f.getMask();
@@ -178,18 +142,22 @@ public JsonGenerator disable(StreamWriteFeature f) {
178142
}
179143

180144
/*
181-
/**********************************************************
145+
/**********************************************************************
182146
/* Public API, accessors
183-
/**********************************************************
147+
/**********************************************************************
184148
*/
185149

186-
@Override public TokenStreamContext getOutputContext() { return _outputContext; }
150+
// public Object getCurrentValue();
151+
// public void setCurrentValue(Object v);
152+
153+
// public TokenStreamContext getOutputContext();
154+
187155
@Override public ObjectWriteContext getObjectWriteContext() { return _objectWriteContext; }
188156

189157
/*
190-
/**********************************************************
158+
/**********************************************************************
191159
/* Public API, write methods, structural
192-
/**********************************************************
160+
/**********************************************************************
193161
*/
194162

195163
//public void writeStartArray() throws IOException
@@ -200,8 +168,8 @@ public JsonGenerator disable(StreamWriteFeature f) {
200168
@Override
201169
public void writeStartArray(Object forValue, int size) throws IOException {
202170
writeStartArray(size);
203-
if ((_outputContext != null) && (forValue != null)) {
204-
_outputContext.setCurrentValue(forValue);
171+
if (forValue != null) {
172+
setCurrentValue(forValue);
205173
}
206174
}
207175

@@ -215,9 +183,9 @@ public void writeStartObject(Object forValue) throws IOException
215183
}
216184

217185
/*
218-
/**********************************************************
186+
/**********************************************************************
219187
/* Public API, write methods, textual
220-
/**********************************************************
188+
/**********************************************************************
221189
*/
222190

223191
@Override public void writeFieldName(SerializableString name) throws IOException {
@@ -267,9 +235,9 @@ public int writeBinary(Base64Variant b64variant, InputStream data, int dataLengt
267235
}
268236

269237
/*
270-
/**********************************************************
238+
/**********************************************************************
271239
/* Public API, write methods, primitive
272-
/**********************************************************
240+
/**********************************************************************
273241
*/
274242

275243
// Not implemented at this level, added as placeholders
@@ -285,9 +253,9 @@ public abstract void writeNull()
285253
*/
286254

287255
/*
288-
/**********************************************************
256+
/**********************************************************************
289257
/* Public API, write methods, POJOs, trees
290-
/**********************************************************
258+
/**********************************************************************
291259
*/
292260

293261
@Override
@@ -314,19 +282,19 @@ public void writeTree(TreeNode rootNode) throws IOException {
314282
}
315283

316284
/*
317-
/**********************************************************
285+
/**********************************************************************
318286
/* Public API, low-level output handling
319-
/**********************************************************
287+
/**********************************************************************
320288
*/
321289

322290
@Override public abstract void flush() throws IOException;
323291
@Override public void close() throws IOException { _closed = true; }
324292
@Override public boolean isClosed() { return _closed; }
325293

326294
/*
327-
/**********************************************************
295+
/**********************************************************************
328296
/* Package methods for this, sub-classes
329-
/**********************************************************
297+
/**********************************************************************
330298
*/
331299

332300
/**
@@ -372,9 +340,9 @@ protected String _asString(BigDecimal value) throws IOException {
372340
}
373341

374342
/*
375-
/**********************************************************
343+
/**********************************************************************
376344
/* UTF-8 related helper method(s)
377-
/**********************************************************
345+
/**********************************************************************
378346
*/
379347

380348
protected final int _decodeSurrogate(int surr1, int surr2) throws IOException

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

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.fasterxml.jackson.core.io.CharTypes;
88
import com.fasterxml.jackson.core.io.CharacterEscapes;
99
import com.fasterxml.jackson.core.io.IOContext;
10-
import com.fasterxml.jackson.core.util.VersionUtil;
1110

1211
/**
1312
* Intermediate base class shared by JSON-backed generators
@@ -16,9 +15,9 @@
1615
public abstract class JsonGeneratorImpl extends GeneratorBase
1716
{
1817
/*
19-
/**********************************************************
18+
/**********************************************************************
2019
/* Constants
21-
/**********************************************************
20+
/**********************************************************************
2221
*/
2322

2423
/**
@@ -28,9 +27,9 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
2827
protected final static int[] DEFAULT_OUTPUT_ESCAPES = CharTypes.get7BitOutputEscapes();
2928

3029
/*
31-
/**********************************************************
30+
/**********************************************************************
3231
/* Configuration, basic I/O, features
33-
/**********************************************************
32+
/**********************************************************************
3433
*/
3534

3635
protected final IOContext _ioContext;
@@ -43,9 +42,9 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
4342
protected int _formatWriteFeatures;
4443

4544
/*
46-
/**********************************************************
45+
/**********************************************************************
4746
/* Configuration, output escaping
48-
/**********************************************************
47+
/**********************************************************************
4948
*/
5049

5150
/**
@@ -76,11 +75,11 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
7675
* NOTE: not all sub-classes make use of this setting.
7776
*/
7877
protected int _maximumNonEscapedChar;
79-
78+
8079
/*
81-
/**********************************************************
80+
/**********************************************************************
8281
/* Configuration, other
83-
/**********************************************************
82+
/**********************************************************************
8483
*/
8584

8685
/**
@@ -102,9 +101,21 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
102101
protected PrettyPrinter _cfgPrettyPrinter;
103102

104103
/*
105-
/**********************************************************
104+
/**********************************************************************
105+
/* Output state
106+
/**********************************************************************
107+
*/
108+
109+
/**
110+
* Object that keeps track of the current contextual state
111+
* of the generator.
112+
*/
113+
protected JsonWriteContext _outputContext;
114+
115+
/*
116+
/**********************************************************************
106117
/* Life-cycle
107-
/**********************************************************
118+
/**********************************************************************
108119
*/
109120

110121
public JsonGeneratorImpl(ObjectWriteContext writeCtxt, IOContext ctxt,
@@ -126,26 +137,28 @@ public JsonGeneratorImpl(ObjectWriteContext writeCtxt, IOContext ctxt,
126137
_rootValueSeparator = rootValueSeparator;
127138

128139
_cfgPrettyPrinter = pp;
140+
141+
DupDetector dups = StreamWriteFeature.STRICT_DUPLICATE_DETECTION.enabledIn(streamWriteFeatures)
142+
? DupDetector.rootDetector(this) : null;
143+
_outputContext = JsonWriteContext.createRootContext(dups);
144+
129145
// 03-Oct-2017, tatu: Not clean (shouldn't call non-static methods from ctor),
130146
// but for now best way to avoid code duplication
131147
setCharacterEscapes(charEsc);
132148
}
133149

134150
/*
135-
/**********************************************************
151+
/**********************************************************************
136152
/* Versioned
137-
/**********************************************************
153+
/**********************************************************************
138154
*/
139155

140-
@Override
141-
public Version version() {
142-
return VersionUtil.versionFor(getClass());
143-
}
156+
@Override public Version version() { return PackageVersion.VERSION; }
144157

145158
/*
146-
/**********************************************************
159+
/**********************************************************************
147160
/* Overridden configuration methods
148-
/**********************************************************
161+
/**********************************************************************
149162
*/
150163

151164
@Override
@@ -172,9 +185,30 @@ public CharacterEscapes getCharacterEscapes() {
172185
}
173186

174187
/*
175-
/**********************************************************
188+
/**********************************************************************
189+
/* Overridden output state handling methods
190+
/**********************************************************************
191+
*/
192+
193+
@Override
194+
public final TokenStreamContext getOutputContext() { return _outputContext; }
195+
196+
@Override
197+
public final Object getCurrentValue() {
198+
return _outputContext.getCurrentValue();
199+
}
200+
201+
@Override
202+
public final void setCurrentValue(Object v) {
203+
if (_outputContext != null) {
204+
_outputContext.setCurrentValue(v);
205+
}
206+
}
207+
208+
/*
209+
/**********************************************************************
176210
/* Partial API
177-
/**********************************************************
211+
/**********************************************************************
178212
*/
179213

180214
// // Overrides just to make things final, to possibly help with inlining
@@ -187,9 +221,9 @@ public final void writeStringField(String fieldName, String value) throws IOExce
187221
}
188222

189223
/*
190-
/**********************************************************
224+
/**********************************************************************
191225
/* Shared helper methods
192-
/**********************************************************
226+
/**********************************************************************
193227
*/
194228

195229
protected void _verifyPrettyValueWrite(String typeMsg, int status) throws IOException

0 commit comments

Comments
 (0)