Skip to content

Commit dcfccb0

Browse files
committed
Align MessagePackGenerator with Jackson 3 generator contract
- _verifyValueWrite: check return value of writeContext.writeValue() and report error when called in wrong context (e.g. value inside object without preceding property name) - writeName: check return value of writeContext.writeName() and report error when not in Object context or when property name is duplicated - writeStartArray / writeStartObject: call _verifyValueWrite before creating the child context, matching the CBORGenerator pattern - addValueNode: call writeContext.writeValue() to keep the Jackson context state in sync with our internal node-based state tracking; without this, _gotPropertyId was never reset after writeName, causing all subsequent writeName calls to return false - close(): delegate lifecycle (isClosed flag, _releaseBuffers) to super.close(); move AUTO_CLOSE_TARGET handling into _closeInput()
1 parent daefba9 commit dcfccb0

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

msgpack-jackson3/src/main/java/org/msgpack/jackson/dataformat/MessagePackGenerator.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ public JsonGenerator writeStartArray(Object currentValue) throws JacksonExceptio
270270
@Override
271271
public JsonGenerator writeStartArray(Object currentValue, int size) throws JacksonException
272272
{
273+
_verifyValueWrite("start an array");
273274
writeContext = writeContext.createChildArrayContext(currentValue);
274275
if (currentState == IN_OBJECT) {
275276
Node node = nodes.get(nodes.size() - 1);
@@ -310,6 +311,7 @@ public JsonGenerator writeStartObject(Object currentValue) throws JacksonExcepti
310311
@Override
311312
public JsonGenerator writeStartObject(Object forValue, int size) throws JacksonException
312313
{
314+
_verifyValueWrite("start an object");
313315
writeContext = writeContext.createChildObjectContext(forValue);
314316
if (currentState == IN_OBJECT) {
315317
Node node = nodes.get(nodes.size() - 1);
@@ -468,6 +470,7 @@ private void addKeyNode(Object key)
468470

469471
private void addValueNode(Object value) throws IOException
470472
{
473+
writeContext.writeValue();
471474
switch (currentState) {
472475
case IN_OBJECT: {
473476
Node node = nodes.get(nodes.size() - 1);
@@ -523,7 +526,9 @@ public JacksonFeatureSet<StreamWriteCapability> streamWriteCapabilities()
523526
@Override
524527
public JsonGenerator writeName(String name) throws JacksonException
525528
{
526-
writeContext.writeName(name);
529+
if (!writeContext.writeName(name)) {
530+
_reportError("Cannot write property name, not in Object context");
531+
}
527532
addKeyNode(name);
528533
return this;
529534
}
@@ -532,7 +537,9 @@ public JsonGenerator writeName(String name) throws JacksonException
532537
public JsonGenerator writeName(SerializableString name) throws JacksonException
533538
{
534539
if (name instanceof MessagePackSerializedString) {
535-
writeContext.writeName(name.getValue());
540+
if (!writeContext.writeName(name.getValue())) {
541+
_reportError("Cannot write property name, not in Object context");
542+
}
536543
addKeyNode(((MessagePackSerializedString) name).getRawValue());
537544
}
538545
else {
@@ -845,21 +852,9 @@ public void writeExtensionType(MessagePackExtensionType extensionType)
845852
@Override
846853
public void close() throws JacksonException
847854
{
848-
try {
855+
if (!_closed) {
849856
flush();
850-
if (StreamWriteFeature.AUTO_CLOSE_TARGET.enabledIn(_streamWriteFeatures)) {
851-
try {
852-
MessagePacker messagePacker = getMessagePacker();
853-
messagePacker.close();
854-
}
855-
catch (IOException e) {
856-
throw _wrapIOFailure(e);
857-
}
858-
}
859-
}
860-
finally {
861-
_closed = true;
862-
_releaseBuffers();
857+
super.close();
863858
}
864859
}
865860

@@ -954,7 +949,9 @@ public void assignCurrentValue(Object v)
954949
@Override
955950
protected void _closeInput() throws IOException
956951
{
957-
messagePacker.close();
952+
if (StreamWriteFeature.AUTO_CLOSE_TARGET.enabledIn(_streamWriteFeatures)) {
953+
messagePacker.close();
954+
}
958955
}
959956

960957
@Override
@@ -965,7 +962,9 @@ protected void _releaseBuffers()
965962
@Override
966963
protected void _verifyValueWrite(String typeMsg) throws JacksonException
967964
{
968-
writeContext.writeValue();
965+
if (!writeContext.writeValue()) {
966+
_reportError("Cannot " + typeMsg + ", expecting a property name");
967+
}
969968
}
970969

971970
private MessagePacker getMessagePacker()

0 commit comments

Comments
 (0)