Skip to content

Commit 2ea7b19

Browse files
committed
Start work on #517
1 parent 42020fd commit 2ea7b19

File tree

5 files changed

+124
-28
lines changed

5 files changed

+124
-28
lines changed

src/main/java/com/fasterxml/jackson/core/JsonGenerator.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ public void writeStartArray(int size) throws IOException {
755755
public abstract void writeEndArray() throws IOException;
756756

757757
/**
758-
* Method for writing starting marker of a JSON Object value
758+
* Method for writing starting marker of an Object value
759759
* (character '{'; plus possible white space decoration
760760
* if pretty-printing is enabled).
761761
*<p>
@@ -766,18 +766,17 @@ public void writeStartArray(int size) throws IOException {
766766
public abstract void writeStartObject() throws IOException;
767767

768768
/**
769-
* Method for writing starting marker of a JSON Object value
770-
* (character '{'; plus possible white space decoration
771-
* if pretty-printing is enabled), to represent Java given
772-
* as the argument. Argument is offered as metadata, but more
769+
* Method for writing starting marker of an Object value
770+
* to represent the given Java Object value.
771+
* Argument is offered as metadata, but more
773772
* importantly it should be assigned as the "current value"
774773
* for the Object content that gets constructed and initialized.
775774
*<p>
776775
* Object values can be written in any context where values
777776
* are allowed: meaning everywhere except for when
778777
* a field name is expected.
779778
*
780-
* @since 2.8.
779+
* @since 2.8
781780
*/
782781
public void writeStartObject(Object forValue) throws IOException
783782
{
@@ -786,7 +785,29 @@ public void writeStartObject(Object forValue) throws IOException
786785
}
787786

788787
/**
789-
* Method for writing closing marker of a JSON Object value
788+
* Method for writing starting marker of an Object value
789+
* to represent the given Java Object value.
790+
* Argument is offered as metadata, but more
791+
* importantly it should be assigned as the "current value"
792+
* for the Object content that gets constructed and initialized.
793+
* In addition, caller knows number of key/value pairs ("properties")
794+
* that will get written for the Object value: this is relevant for
795+
* some format backends (but not, as an example, for JSON).
796+
*<p>
797+
* Object values can be written in any context where values
798+
* are allowed: meaning everywhere except for when
799+
* a field name is expected.
800+
*
801+
* @since 2.10
802+
*/
803+
public void writeStartObject(Object forValue, int size) throws IOException
804+
{
805+
writeStartObject();
806+
setCurrentValue(forValue);
807+
}
808+
809+
/**
810+
* Method for writing closing marker of an Object value
790811
* (character '}'; plus possible white space decoration
791812
* if pretty-printing is enabled).
792813
*<p>
@@ -1691,7 +1712,7 @@ public final void writeArrayFieldStart(String fieldName) throws IOException {
16911712

16921713
/**
16931714
* Convenience method for outputting a field entry ("member")
1694-
* (that will contain a JSON Object value), and the START_OBJECT marker.
1715+
* (that will contain an Object value), and the START_OBJECT marker.
16951716
* Equivalent to:
16961717
*<pre>
16971718
* writeFieldName(fieldName);

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

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ protected JsonWriteContext(int type, JsonWriteContext parent, DupDetector dups)
7373
_index = -1;
7474
}
7575

76+
/* @since 2.10 */
77+
protected JsonWriteContext(int type, JsonWriteContext parent, DupDetector dups,
78+
Object currValue) {
79+
super();
80+
_type = type;
81+
_parent = parent;
82+
_dups = dups;
83+
_index = -1;
84+
_currentValue = currValue;
85+
}
86+
7687
protected JsonWriteContext reset(int type) {
7788
_type = type;
7889
_index = -1;
@@ -83,6 +94,17 @@ protected JsonWriteContext reset(int type) {
8394
return this;
8495
}
8596

97+
/* @since 2.10 */
98+
protected JsonWriteContext reset(int type, Object currValue) {
99+
_type = type;
100+
_index = -1;
101+
_currentName = null;
102+
_gotName = false;
103+
_currentValue = currValue;
104+
if (_dups != null) { _dups.reset(); }
105+
return this;
106+
}
107+
86108
public JsonWriteContext withDupDetector(DupDetector dups) {
87109
_dups = dups;
88110
return this;
@@ -117,21 +139,45 @@ public static JsonWriteContext createRootContext(DupDetector dd) {
117139
public JsonWriteContext createChildArrayContext() {
118140
JsonWriteContext ctxt = _child;
119141
if (ctxt == null) {
120-
_child = ctxt = new JsonWriteContext(TYPE_ARRAY, this, (_dups == null) ? null : _dups.child());
142+
_child = ctxt = new JsonWriteContext(TYPE_ARRAY, this,
143+
(_dups == null) ? null : _dups.child());
121144
return ctxt;
122145
}
123146
return ctxt.reset(TYPE_ARRAY);
124147
}
125148

149+
/* @since 2.10 */
150+
public JsonWriteContext createChildArrayContext(Object currValue) {
151+
JsonWriteContext ctxt = _child;
152+
if (ctxt == null) {
153+
_child = ctxt = new JsonWriteContext(TYPE_ARRAY, this,
154+
(_dups == null) ? null : _dups.child(), currValue);
155+
return ctxt;
156+
}
157+
return ctxt.reset(TYPE_ARRAY, currValue);
158+
}
159+
126160
public JsonWriteContext createChildObjectContext() {
127161
JsonWriteContext ctxt = _child;
128162
if (ctxt == null) {
129-
_child = ctxt = new JsonWriteContext(TYPE_OBJECT, this, (_dups == null) ? null : _dups.child());
163+
_child = ctxt = new JsonWriteContext(TYPE_OBJECT, this,
164+
(_dups == null) ? null : _dups.child());
130165
return ctxt;
131166
}
132167
return ctxt.reset(TYPE_OBJECT);
133168
}
134169

170+
/* @since 2.10 */
171+
public JsonWriteContext createChildObjectContext(Object currValue) {
172+
JsonWriteContext ctxt = _child;
173+
if (ctxt == null) {
174+
_child = ctxt = new JsonWriteContext(TYPE_OBJECT, this,
175+
(_dups == null) ? null : _dups.child(), currValue);
176+
return ctxt;
177+
}
178+
return ctxt.reset(TYPE_OBJECT, currValue);
179+
}
180+
135181
@Override public final JsonWriteContext getParent() { return _parent; }
136182
@Override public final String getCurrentName() { return _currentName; }
137183
// @since 2.9
@@ -152,7 +198,7 @@ public JsonWriteContext clearAndGetParent() {
152198
// could also clear the current name, but seems cheap enough to leave?
153199
return _parent;
154200
}
155-
201+
156202
public DupDetector getDupDetector() {
157203
return _dups;
158204
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,21 @@ public final void writeStartArray() throws IOException
325325
}
326326
}
327327

328+
@Override // since 2.10
329+
public void writeStartArray(int size) throws IOException
330+
{
331+
_verifyValueWrite("start an array");
332+
_writeContext = _writeContext.createChildArrayContext();
333+
if (_cfgPrettyPrinter != null) {
334+
_cfgPrettyPrinter.writeStartArray(this);
335+
} else {
336+
if (_outputTail >= _outputEnd) {
337+
_flushBuffer();
338+
}
339+
_outputBuffer[_outputTail++] = BYTE_LBRACKET;
340+
}
341+
}
342+
328343
@Override
329344
public final void writeEndArray() throws IOException
330345
{
@@ -361,11 +376,8 @@ public final void writeStartObject() throws IOException
361376
public void writeStartObject(Object forValue) throws IOException
362377
{
363378
_verifyValueWrite("start an object");
364-
JsonWriteContext ctxt = _writeContext.createChildObjectContext();
379+
JsonWriteContext ctxt = _writeContext.createChildObjectContext(forValue);
365380
_writeContext = ctxt;
366-
if (forValue != null) {
367-
ctxt.setCurrentValue(forValue);
368-
}
369381
if (_cfgPrettyPrinter != null) {
370382
_cfgPrettyPrinter.writeStartObject(this);
371383
} else {
@@ -375,7 +387,7 @@ public void writeStartObject(Object forValue) throws IOException
375387
_outputBuffer[_outputTail++] = '{';
376388
}
377389
}
378-
390+
379391
@Override
380392
public final void writeEndObject() throws IOException
381393
{

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,21 @@ public void writeStartArray() throws IOException
258258
}
259259
}
260260

261+
@Override // since 2.10
262+
public void writeStartArray(int size) throws IOException
263+
{
264+
_verifyValueWrite("start an array");
265+
_writeContext = _writeContext.createChildArrayContext();
266+
if (_cfgPrettyPrinter != null) {
267+
_cfgPrettyPrinter.writeStartArray(this);
268+
} else {
269+
if (_outputTail >= _outputEnd) {
270+
_flushBuffer();
271+
}
272+
_outputBuffer[_outputTail++] = '[';
273+
}
274+
}
275+
261276
@Override
262277
public void writeEndArray() throws IOException
263278
{
@@ -275,15 +290,11 @@ public void writeEndArray() throws IOException
275290
_writeContext = _writeContext.clearAndGetParent();
276291
}
277292

278-
@Override // since 2.8
279-
public void writeStartObject(Object forValue) throws IOException
293+
@Override
294+
public void writeStartObject() throws IOException
280295
{
281296
_verifyValueWrite("start an object");
282-
JsonWriteContext ctxt = _writeContext.createChildObjectContext();
283-
_writeContext = ctxt;
284-
if (forValue != null) {
285-
ctxt.setCurrentValue(forValue);
286-
}
297+
_writeContext = _writeContext.createChildObjectContext();
287298
if (_cfgPrettyPrinter != null) {
288299
_cfgPrettyPrinter.writeStartObject(this);
289300
} else {
@@ -293,12 +304,13 @@ public void writeStartObject(Object forValue) throws IOException
293304
_outputBuffer[_outputTail++] = '{';
294305
}
295306
}
296-
297-
@Override
298-
public void writeStartObject() throws IOException
307+
308+
@Override // since 2.8
309+
public void writeStartObject(Object forValue) throws IOException
299310
{
300311
_verifyValueWrite("start an object");
301-
_writeContext = _writeContext.createChildObjectContext();
312+
JsonWriteContext ctxt = _writeContext.createChildObjectContext(forValue);
313+
_writeContext = ctxt;
302314
if (_cfgPrettyPrinter != null) {
303315
_cfgPrettyPrinter.writeStartObject(this);
304316
} else {

src/main/java/com/fasterxml/jackson/core/util/JsonGeneratorDelegate.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public JsonGenerator setPrettyPrinter(PrettyPrinter pp) {
195195

196196
@Override
197197
public void writeStartArray(int size) throws IOException { delegate.writeStartArray(size); }
198-
198+
199199
@Override
200200
public void writeEndArray() throws IOException { delegate.writeEndArray(); }
201201

@@ -205,6 +205,11 @@ public JsonGenerator setPrettyPrinter(PrettyPrinter pp) {
205205
@Override
206206
public void writeStartObject(Object forValue) throws IOException { delegate.writeStartObject(forValue); }
207207

208+
@Override
209+
public void writeStartObject(Object forValue, int size) throws IOException {
210+
delegate.writeStartObject(forValue, size);
211+
}
212+
208213
@Override
209214
public void writeEndObject() throws IOException { delegate.writeEndObject(); }
210215

0 commit comments

Comments
 (0)