Skip to content

Commit

Permalink
Fix #257
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 22, 2016
1 parent 8244a56 commit 38e03e6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ JSON library.
#253: Add `JsonGenerator. writeEmbeddedObject()` to allow writes of opaque native types
(suggested by Gregoire C)
#255: Relax ownership checks for buffers not to require increase in size
#257: Add `writeStartObject(Object pojo)` to streamline assignment of current value

2.7.2 (26-Feb-2016)

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,26 @@ 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
* 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.
*/
public void writeStartObject(Object forValue) throws IOException
{
writeStartObject();
setCurrentValue(forValue);
}

/**
* Method for writing closing marker of a JSON Object value
* (character '}'; plus possible white space decoration
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/base/GeneratorBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ protected void _checkStdFeatureChanges(int newFeatureFlags, int changedFeatures)
//public void writeStartObject() throws IOException
//public void writeEndObject() throws IOException

@Override // since 2.8
public void writeStartObject(Object forValue) throws IOException
{
writeStartObject();
if ((_writeContext != null) && (forValue != null)) {
_writeContext.setCurrentValue(forValue);
}
setCurrentValue(forValue);
}

/*
/**********************************************************
/* Public API, write methods, textual
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,25 @@ public final void writeStartObject() throws IOException
}
}

@Override // since 2.8
public void writeStartObject(Object forValue) throws IOException
{
_verifyValueWrite("start an object");
JsonWriteContext ctxt = _writeContext.createChildObjectContext();
_writeContext = ctxt;
if (forValue != null) {
ctxt.setCurrentValue(forValue);
}
if (_cfgPrettyPrinter != null) {
_cfgPrettyPrinter.writeStartObject(this);
} else {
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = '{';
}
}

@Override
public final void writeEndObject() throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ protected void _writeFieldName(SerializableString name, boolean commaBefore) thr
*/

@Override
public void writeStartArray() throws IOException, JsonGenerationException
public void writeStartArray() throws IOException
{
_verifyValueWrite("start an array");
_writeContext = _writeContext.createChildArrayContext();
Expand All @@ -210,7 +210,7 @@ public void writeStartArray() throws IOException, JsonGenerationException
}

@Override
public void writeEndArray() throws IOException, JsonGenerationException
public void writeEndArray() throws IOException
{
if (!_writeContext.inArray()) {
_reportError("Current context not an ARRAY but "+_writeContext.getTypeDesc());
Expand All @@ -226,8 +226,27 @@ public void writeEndArray() throws IOException, JsonGenerationException
_writeContext = _writeContext.clearAndGetParent();
}

@Override // since 2.8
public void writeStartObject(Object forValue) throws IOException
{
_verifyValueWrite("start an object");
JsonWriteContext ctxt = _writeContext.createChildObjectContext();
_writeContext = ctxt;
if (forValue != null) {
ctxt.setCurrentValue(forValue);
}
if (_cfgPrettyPrinter != null) {
_cfgPrettyPrinter.writeStartObject(this);
} else {
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = '{';
}
}

@Override
public void writeStartObject() throws IOException, JsonGenerationException
public void writeStartObject() throws IOException
{
_verifyValueWrite("start an object");
_writeContext = _writeContext.createChildObjectContext();
Expand All @@ -242,7 +261,7 @@ public void writeStartObject() throws IOException, JsonGenerationException
}

@Override
public void writeEndObject() throws IOException, JsonGenerationException
public void writeEndObject() throws IOException
{
if (!_writeContext.inObject()) {
_reportError("Current context not an object but "+_writeContext.getTypeDesc());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ public JsonGenerator setPrettyPrinter(PrettyPrinter pp) {

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

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

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

0 comments on commit 38e03e6

Please sign in to comment.