Skip to content

Commit

Permalink
Fix #163
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 12, 2019
1 parent 65a0693 commit 3a839ce
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Expand Up @@ -84,3 +84,7 @@ Vincent Boulaye (vboulaye@github)
* Implemented #15: Add a `CsvParser.Feature.SKIP_EMPTY_LINES` to allow
skipping empty rows
(2.10.1)

Piyush Kumar (piyushkumar13@github)
* Reported #163: (yaml) `SequenceWriter` does not create multiple docs in a single yaml file
(2.10.2)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Expand Up @@ -10,6 +10,8 @@ Modules:

2.10.2 (not yet released)

#163: (yaml) `SequenceWriter` does not create multiple docs in a single yaml file
(reported by Piyush K)
#166: (csv) Incorrect `JsonParseException` Message for missing separator char
(reported by gimiz@github)

Expand Down
Expand Up @@ -216,6 +216,8 @@ private Feature(boolean defaultState) {

protected DumperOptions _outputOptions;

protected final org.yaml.snakeyaml.DumperOptions.Version _docVersion;

// for field names, leave out quotes
private final static DumperOptions.ScalarStyle STYLE_UNQUOTED_NAME = DumperOptions.ScalarStyle.PLAIN;

Expand Down Expand Up @@ -252,6 +254,8 @@ private Feature(boolean defaultState) {
*/
protected String _typeId;

protected int _rootValueCount;

/*
/**********************************************************
/* Life-cycle
Expand All @@ -267,19 +271,14 @@ public YAMLGenerator(IOContext ctxt, int jsonFeatures, int yamlFeatures,
_ioContext = ctxt;
_formatFeatures = yamlFeatures;
_writer = out;
_docVersion = version;

_outputOptions = buildDumperOptions(jsonFeatures, yamlFeatures, version);

_emitter = new Emitter(_writer, _outputOptions);
// should we start output now, or try to defer?
_emitter.emit(new StreamStartEvent(null, null));
Map<String,String> noTags = Collections.emptyMap();

boolean startMarker = Feature.WRITE_DOC_START_MARKER.enabledIn(yamlFeatures);

_emitter.emit(new DocumentStartEvent(null, null, startMarker,
version, // for 1.10 was: ((version == null) ? null : version.getArray()),
noTags));
_emit(new StreamStartEvent(null, null));
_emitStartDocument();
}

protected DumperOptions buildDumperOptions(int jsonFeatures, int yamlFeatures,
Expand Down Expand Up @@ -479,8 +478,11 @@ public final void flush() throws IOException
public void close() throws IOException
{
if (!isClosed()) {
_emitter.emit(new DocumentEndEvent(null, null, false));
_emitter.emit(new StreamEndEvent(null, null));
// 11-Dec-2019, tatu: Should perhaps check if content is to be auto-closed...
// but need END_DOCUMENT regardless

_emitEndDocument();
_emit(new StreamEndEvent(null, null));
super.close();

/* 25-Nov-2008, tatus: As per [JACKSON-16] we are not to call close()
Expand Down Expand Up @@ -518,7 +520,7 @@ public final void writeStartArray() throws IOException
if (anchor != null) {
_objectId = null;
}
_emitter.emit(new SequenceStartEvent(anchor, yamlTag,
_emit(new SequenceStartEvent(anchor, yamlTag,
implicit, null, null, style));
}

Expand All @@ -531,7 +533,7 @@ public final void writeEndArray() throws IOException
// just to make sure we don't "leak" type ids
_typeId = null;
_writeContext = _writeContext.getParent();
_emitter.emit(new SequenceEndEvent(null, null));
_emit(new SequenceEndEvent(null, null));
}

@Override
Expand All @@ -546,7 +548,7 @@ public final void writeStartObject() throws IOException
if (anchor != null) {
_objectId = null;
}
_emitter.emit(new MappingStartEvent(anchor, yamlTag,
_emit(new MappingStartEvent(anchor, yamlTag,
implicit, null, null, style));
}

Expand All @@ -559,7 +561,7 @@ public final void writeEndObject() throws IOException
// just to make sure we don't "leak" type ids
_typeId = null;
_writeContext = _writeContext.getParent();
_emitter.emit(new MappingEndEvent(null, null));
_emit(new MappingEndEvent(null, null));
}

/*
Expand Down Expand Up @@ -815,7 +817,7 @@ public void writeObjectRef(Object id)
{
_verifyValueWrite("write Object reference");
AliasEvent evt = new AliasEvent(String.valueOf(id), null, null);
_emitter.emit(evt);
_emit(evt);
}

@Override
Expand All @@ -840,6 +842,15 @@ protected final void _verifyValueWrite(String typeMsg)
if (status == JsonWriteContext.STATUS_EXPECT_NAME) {
_reportError("Can not "+typeMsg+", expecting field name");
}
if (_writeContext.inRoot()) {
// Start-doc emitted when creating generator, but otherwise need it; similarly,
// need matching end-document to close earlier open one
if (_writeContext.getCurrentIndex() > 0) {
_emitEndDocument();
_emitStartDocument();
}
}

}

@Override
Expand All @@ -861,7 +872,7 @@ protected void _releaseBuffers() {

protected void _writeScalar(String value, String type, DumperOptions.ScalarStyle style) throws IOException
{
_emitter.emit(_scalarEvent(value, style));
_emit(_scalarEvent(value, style));
}

private void _writeScalarBinary(Base64Variant b64variant,
Expand All @@ -874,7 +885,7 @@ private void _writeScalarBinary(Base64Variant b64variant,
}
final String lf = _lf();
String encoded = _base64encode(b64variant, data, lf);
_emitter.emit(new ScalarEvent(null, TAG_BINARY, EXPLICIT_TAGS, encoded,
_emit(new ScalarEvent(null, TAG_BINARY, EXPLICIT_TAGS, encoded,
null, null, STYLE_BASE64));
}

Expand Down Expand Up @@ -976,4 +987,24 @@ private boolean _valueNeedsQuoting(String name) {
protected String _lf() {
return _outputOptions.getLineBreak().getString();
}

// @since 2.10.2
protected void _emitStartDocument() throws IOException
{
Map<String,String> noTags = Collections.emptyMap();
boolean startMarker = Feature.WRITE_DOC_START_MARKER.enabledIn(_formatFeatures);
_emit(new DocumentStartEvent(null, null, startMarker,
_docVersion, // for 1.10 was: ((version == null) ? null : version.getArray()),
noTags));
}

// @since 2.10.2
protected void _emitEndDocument() throws IOException {
_emit(new DocumentEndEvent(null, null, false));
}

// @since 2.10.2
protected final void _emit(Event e) throws IOException {
_emitter.emit(e);
}
}
@@ -1,12 +1,11 @@
package com.fasterxml.jackson.dataformat.yaml.failing;
package com.fasterxml.jackson.dataformat.yaml;

import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collections;

import com.fasterxml.jackson.databind.*;

import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;

// for [dataformats-text#163]
public class MultipleDocumentsWriteTest extends ModuleTestBase
{
Expand All @@ -26,25 +25,21 @@ public void testWriteMultipleDocsBeans() throws Exception
}
w.close();

String yaml = w.toString();

// !!! TODO: actual expected multi-doc contents:
assertEquals("foo", yaml);
String yaml = w.toString().trim();
assertEquals("---\nvalue: 42\n---\nvalue: 28", yaml);
}

public void testWriteMultipleDocsLists() throws Exception
{
ObjectMapper mapper = newObjectMapper();
StringWriter w = new StringWriter();
try (SequenceWriter seqW = mapper.writer().writeValues(w)) {
seqW.write(Collections.singleton(42));
seqW.write(Arrays.asList(28,12));
seqW.write(Collections.singleton(28));
}
w.close();

String yaml = w.toString();

// !!! TODO: actual expected multi-doc contents:
assertEquals("foo", yaml);
String yaml = w.toString().trim();
assertEquals("---\n- 28\n- 12\n---\n- 28", yaml);
}
}
Expand Up @@ -116,7 +116,6 @@ public void testUUIDs() throws Exception
{
UUID uuid = new UUID(0, 0);
String yaml = MAPPER.writeValueAsString(uuid);

UUID result = MAPPER.readValue(yaml, UUID.class);

assertEquals(uuid, result);
Expand Down

0 comments on commit 3a839ce

Please sign in to comment.