Fix #257
diff --git a/release-notes/VERSION b/release-notes/VERSION
index 085dbfd..d7bccec 100644
--- a/release-notes/VERSION
+++ b/release-notes/VERSION
@@ -19,6 +19,7 @@
#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)
diff --git a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
index cef4f9b..80d5f91 100644
--- a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
+++ b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
@@ -756,6 +756,26 @@
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
* if pretty-printing is enabled).
diff --git a/src/main/java/com/fasterxml/jackson/core/base/GeneratorBase.java b/src/main/java/com/fasterxml/jackson/core/base/GeneratorBase.java
index c99832b..b35916b 100644
--- a/src/main/java/com/fasterxml/jackson/core/base/GeneratorBase.java
+++ b/src/main/java/com/fasterxml/jackson/core/base/GeneratorBase.java
@@ -266,6 +266,16 @@
//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
diff --git a/src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java
index 8f17ea3..d5a33fe 100644
--- a/src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java
+++ b/src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java
@@ -309,6 +309,25 @@
}
}
+ @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
{
diff --git a/src/main/java/com/fasterxml/jackson/core/json/WriterBasedJsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/json/WriterBasedJsonGenerator.java
index 316886b..6e5b9ff 100644
--- a/src/main/java/com/fasterxml/jackson/core/json/WriterBasedJsonGenerator.java
+++ b/src/main/java/com/fasterxml/jackson/core/json/WriterBasedJsonGenerator.java
@@ -195,7 +195,7 @@
*/
@Override
- public void writeStartArray() throws IOException, JsonGenerationException
+ public void writeStartArray() throws IOException
{
_verifyValueWrite("start an array");
_writeContext = _writeContext.createChildArrayContext();
@@ -210,7 +210,7 @@
}
@Override
- public void writeEndArray() throws IOException, JsonGenerationException
+ public void writeEndArray() throws IOException
{
if (!_writeContext.inArray()) {
_reportError("Current context not an ARRAY but "+_writeContext.getTypeDesc());
@@ -226,8 +226,27 @@
_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();
@@ -242,7 +261,7 @@
}
@Override
- public void writeEndObject() throws IOException, JsonGenerationException
+ public void writeEndObject() throws IOException
{
if (!_writeContext.inObject()) {
_reportError("Current context not an object but "+_writeContext.getTypeDesc());
diff --git a/src/main/java/com/fasterxml/jackson/core/util/JsonGeneratorDelegate.java b/src/main/java/com/fasterxml/jackson/core/util/JsonGeneratorDelegate.java
index 686919c..40cd508 100644
--- a/src/main/java/com/fasterxml/jackson/core/util/JsonGeneratorDelegate.java
+++ b/src/main/java/com/fasterxml/jackson/core/util/JsonGeneratorDelegate.java
@@ -200,6 +200,9 @@
@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(); }