Skip to content

Commit

Permalink
Start work on #517
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 14, 2019
1 parent 42020fd commit 2ea7b19
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 28 deletions.
37 changes: 29 additions & 8 deletions src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public void writeStartArray(int size) throws IOException {
public abstract void writeEndArray() throws IOException;

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

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

/**
* Method for writing closing marker of a JSON Object value
* Method for writing starting marker of an Object value
* to represent the given Java Object value.
* Argument is offered as metadata, but more
* importantly it should be assigned as the "current value"
* for the Object content that gets constructed and initialized.
* In addition, caller knows number of key/value pairs ("properties")
* that will get written for the Object value: this is relevant for
* some format backends (but not, as an example, for JSON).
*<p>
* Object values can be written in any context where values
* are allowed: meaning everywhere except for when
* a field name is expected.
*
* @since 2.10
*/
public void writeStartObject(Object forValue, int size) throws IOException
{
writeStartObject();
setCurrentValue(forValue);
}

/**
* Method for writing closing marker of an Object value
* (character '}'; plus possible white space decoration
* if pretty-printing is enabled).
*<p>
Expand Down Expand Up @@ -1691,7 +1712,7 @@ public final void writeArrayFieldStart(String fieldName) throws IOException {

/**
* Convenience method for outputting a field entry ("member")
* (that will contain a JSON Object value), and the START_OBJECT marker.
* (that will contain an Object value), and the START_OBJECT marker.
* Equivalent to:
*<pre>
* writeFieldName(fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ protected JsonWriteContext(int type, JsonWriteContext parent, DupDetector dups)
_index = -1;
}

/* @since 2.10 */
protected JsonWriteContext(int type, JsonWriteContext parent, DupDetector dups,
Object currValue) {
super();
_type = type;
_parent = parent;
_dups = dups;
_index = -1;
_currentValue = currValue;
}

protected JsonWriteContext reset(int type) {
_type = type;
_index = -1;
Expand All @@ -83,6 +94,17 @@ protected JsonWriteContext reset(int type) {
return this;
}

/* @since 2.10 */
protected JsonWriteContext reset(int type, Object currValue) {
_type = type;
_index = -1;
_currentName = null;
_gotName = false;
_currentValue = currValue;
if (_dups != null) { _dups.reset(); }
return this;
}

public JsonWriteContext withDupDetector(DupDetector dups) {
_dups = dups;
return this;
Expand Down Expand Up @@ -117,21 +139,45 @@ public static JsonWriteContext createRootContext(DupDetector dd) {
public JsonWriteContext createChildArrayContext() {
JsonWriteContext ctxt = _child;
if (ctxt == null) {
_child = ctxt = new JsonWriteContext(TYPE_ARRAY, this, (_dups == null) ? null : _dups.child());
_child = ctxt = new JsonWriteContext(TYPE_ARRAY, this,
(_dups == null) ? null : _dups.child());
return ctxt;
}
return ctxt.reset(TYPE_ARRAY);
}

/* @since 2.10 */
public JsonWriteContext createChildArrayContext(Object currValue) {
JsonWriteContext ctxt = _child;
if (ctxt == null) {
_child = ctxt = new JsonWriteContext(TYPE_ARRAY, this,
(_dups == null) ? null : _dups.child(), currValue);
return ctxt;
}
return ctxt.reset(TYPE_ARRAY, currValue);
}

public JsonWriteContext createChildObjectContext() {
JsonWriteContext ctxt = _child;
if (ctxt == null) {
_child = ctxt = new JsonWriteContext(TYPE_OBJECT, this, (_dups == null) ? null : _dups.child());
_child = ctxt = new JsonWriteContext(TYPE_OBJECT, this,
(_dups == null) ? null : _dups.child());
return ctxt;
}
return ctxt.reset(TYPE_OBJECT);
}

/* @since 2.10 */
public JsonWriteContext createChildObjectContext(Object currValue) {
JsonWriteContext ctxt = _child;
if (ctxt == null) {
_child = ctxt = new JsonWriteContext(TYPE_OBJECT, this,
(_dups == null) ? null : _dups.child(), currValue);
return ctxt;
}
return ctxt.reset(TYPE_OBJECT, currValue);
}

@Override public final JsonWriteContext getParent() { return _parent; }
@Override public final String getCurrentName() { return _currentName; }
// @since 2.9
Expand All @@ -152,7 +198,7 @@ public JsonWriteContext clearAndGetParent() {
// could also clear the current name, but seems cheap enough to leave?
return _parent;
}

public DupDetector getDupDetector() {
return _dups;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,21 @@ public final void writeStartArray() throws IOException
}
}

@Override // since 2.10
public void writeStartArray(int size) throws IOException
{
_verifyValueWrite("start an array");
_writeContext = _writeContext.createChildArrayContext();
if (_cfgPrettyPrinter != null) {
_cfgPrettyPrinter.writeStartArray(this);
} else {
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = BYTE_LBRACKET;
}
}

@Override
public final void writeEndArray() throws IOException
{
Expand Down Expand Up @@ -361,11 +376,8 @@ public final void writeStartObject() throws IOException
public void writeStartObject(Object forValue) throws IOException
{
_verifyValueWrite("start an object");
JsonWriteContext ctxt = _writeContext.createChildObjectContext();
JsonWriteContext ctxt = _writeContext.createChildObjectContext(forValue);
_writeContext = ctxt;
if (forValue != null) {
ctxt.setCurrentValue(forValue);
}
if (_cfgPrettyPrinter != null) {
_cfgPrettyPrinter.writeStartObject(this);
} else {
Expand All @@ -375,7 +387,7 @@ public void writeStartObject(Object forValue) throws IOException
_outputBuffer[_outputTail++] = '{';
}
}

@Override
public final void writeEndObject() throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,21 @@ public void writeStartArray() throws IOException
}
}

@Override // since 2.10
public void writeStartArray(int size) throws IOException
{
_verifyValueWrite("start an array");
_writeContext = _writeContext.createChildArrayContext();
if (_cfgPrettyPrinter != null) {
_cfgPrettyPrinter.writeStartArray(this);
} else {
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = '[';
}
}

@Override
public void writeEndArray() throws IOException
{
Expand All @@ -275,15 +290,11 @@ public void writeEndArray() throws IOException
_writeContext = _writeContext.clearAndGetParent();
}

@Override // since 2.8
public void writeStartObject(Object forValue) throws IOException
@Override
public void writeStartObject() throws IOException
{
_verifyValueWrite("start an object");
JsonWriteContext ctxt = _writeContext.createChildObjectContext();
_writeContext = ctxt;
if (forValue != null) {
ctxt.setCurrentValue(forValue);
}
_writeContext = _writeContext.createChildObjectContext();
if (_cfgPrettyPrinter != null) {
_cfgPrettyPrinter.writeStartObject(this);
} else {
Expand All @@ -293,12 +304,13 @@ public void writeStartObject(Object forValue) throws IOException
_outputBuffer[_outputTail++] = '{';
}
}
@Override
public void writeStartObject() throws IOException

@Override // since 2.8
public void writeStartObject(Object forValue) throws IOException
{
_verifyValueWrite("start an object");
_writeContext = _writeContext.createChildObjectContext();
JsonWriteContext ctxt = _writeContext.createChildObjectContext(forValue);
_writeContext = ctxt;
if (_cfgPrettyPrinter != null) {
_cfgPrettyPrinter.writeStartObject(this);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public JsonGenerator setPrettyPrinter(PrettyPrinter pp) {

@Override
public void writeStartArray(int size) throws IOException { delegate.writeStartArray(size); }

@Override
public void writeEndArray() throws IOException { delegate.writeEndArray(); }

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

@Override
public void writeStartObject(Object forValue, int size) throws IOException {
delegate.writeStartObject(forValue, size);
}

@Override
public void writeEndObject() throws IOException { delegate.writeEndObject(); }

Expand Down

0 comments on commit 2ea7b19

Please sign in to comment.