Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions yaml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.23</version>
<groupId>org.snakeyaml</groupId>
<artifactId>snakeyaml-engine</artifactId>
<version>1.0</version>
</dependency>

<!-- and for testing need annotations -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.fasterxml.jackson.dataformat.yaml;

import java.io.IOException;
import java.io.Writer;

import org.snakeyaml.engine.v1.api.StreamDataWriter;

public class WriterWrapper implements StreamDataWriter {
private final Writer _writer;

public WriterWrapper(Writer _writer) {
this._writer = _writer;
}

@Override
public void flush() {
try {
_writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void write(String str) {
try {
_writer.write(str);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void write(String str, int off, int len) {
try {
_writer.write(str, off, len);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.*;
import java.nio.charset.Charset;

import org.yaml.snakeyaml.DumperOptions;
import org.snakeyaml.engine.v1.common.SpecVersion;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.base.TextualTSFactory;
Expand Down Expand Up @@ -42,7 +42,7 @@ public class YAMLFactory
/**********************************************************************
*/

protected DumperOptions.Version _version; // enum, is serializable
protected SpecVersion _version; // enum, is serializable

/**
* Default constructor used to create factory instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,28 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
import org.yaml.snakeyaml.emitter.Emitter;
import org.yaml.snakeyaml.events.*;
import org.yaml.snakeyaml.nodes.Tag;
import org.snakeyaml.engine.v1.api.DumpSettings;
import org.snakeyaml.engine.v1.api.DumpSettingsBuilder;
import org.snakeyaml.engine.v1.common.Anchor;
import org.snakeyaml.engine.v1.common.FlowStyle;
import org.snakeyaml.engine.v1.common.ScalarStyle;
import org.snakeyaml.engine.v1.common.SpecVersion;
import org.snakeyaml.engine.v1.emitter.Emitter;
import org.snakeyaml.engine.v1.events.AliasEvent;
import org.snakeyaml.engine.v1.events.DocumentEndEvent;
import org.snakeyaml.engine.v1.events.DocumentStartEvent;
import org.snakeyaml.engine.v1.events.ImplicitTuple;
import org.snakeyaml.engine.v1.events.MappingEndEvent;
import org.snakeyaml.engine.v1.events.MappingStartEvent;
import org.snakeyaml.engine.v1.events.ScalarEvent;
import org.snakeyaml.engine.v1.events.SequenceEndEvent;
import org.snakeyaml.engine.v1.events.SequenceStartEvent;
import org.snakeyaml.engine.v1.events.StreamEndEvent;
import org.snakeyaml.engine.v1.events.StreamStartEvent;
import org.snakeyaml.engine.v1.nodes.Tag;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.base.GeneratorBase;
Expand Down Expand Up @@ -97,7 +112,12 @@ public enum Feature implements FormatFeature
* If disabled, Unix linefeed ({@code \n}) will be used.
* <p>
* Default value is `false` for backwards compatibility.
*
* This setting does not do anything. Regardless of its value, SnakeYAML Engine will use the line break defined
* in System.getProperty("line.separator")
* @deprecated
*/
@Deprecated
USE_PLATFORM_LINE_BREAKS(false),

/**
Expand Down Expand Up @@ -168,23 +188,23 @@ private Feature(boolean defaultState) {

protected Writer _writer;

protected DumperOptions _outputOptions;
protected DumpSettings _outputOptions;

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

// numbers, booleans, should use implicit
private final static DumperOptions.ScalarStyle STYLE_SCALAR = DumperOptions.ScalarStyle.PLAIN;
private final static ScalarStyle STYLE_SCALAR = ScalarStyle.PLAIN;
// Strings quoted for fun
private final static DumperOptions.ScalarStyle STYLE_QUOTED = DumperOptions.ScalarStyle.DOUBLE_QUOTED;
private final static ScalarStyle STYLE_QUOTED = ScalarStyle.DOUBLE_QUOTED;
// Strings in literal (block) style
private final static DumperOptions.ScalarStyle STYLE_LITERAL = DumperOptions.ScalarStyle.LITERAL;
private final static ScalarStyle STYLE_LITERAL = ScalarStyle.LITERAL;

// Which flow style to use for Base64? Maybe basic quoted?
// 29-Nov-2017, tatu: Actually SnakeYAML uses block style so:
private final static DumperOptions.ScalarStyle STYLE_BASE64 = STYLE_LITERAL;
private final static ScalarStyle STYLE_BASE64 = STYLE_LITERAL;

private final static DumperOptions.ScalarStyle STYLE_PLAIN = DumperOptions.ScalarStyle.PLAIN;
private final static ScalarStyle STYLE_PLAIN = ScalarStyle.PLAIN;

/*
/**********************************************************************
Expand Down Expand Up @@ -215,7 +235,7 @@ private Feature(boolean defaultState) {
public YAMLGenerator(ObjectWriteContext writeContext, IOContext ioCtxt,
int streamWriteFeatures, int yamlFeatures,
Writer out,
org.yaml.snakeyaml.DumperOptions.Version version)
SpecVersion version)
throws IOException
{
super(writeContext, streamWriteFeatures);
Expand All @@ -225,22 +245,22 @@ public YAMLGenerator(ObjectWriteContext writeContext, IOContext ioCtxt,

_outputOptions = buildDumperOptions(streamWriteFeatures, yamlFeatures, version);

_emitter = new Emitter(_writer, _outputOptions);
_emitter = new Emitter( _outputOptions, new WriterWrapper(_writer));
// should we start output now, or try to defer?
_emitter.emit(new StreamStartEvent(null, null));
_emitter.emit(new StreamStartEvent());
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()),
_emitter.emit(new DocumentStartEvent(startMarker, Optional.empty(),
// for 1.10 was: ((version == null) ? null : version.getArray()),
noTags));
}

protected DumperOptions buildDumperOptions(int streamWriteFeatures, int yamlFeatures,
org.yaml.snakeyaml.DumperOptions.Version version)
protected DumpSettings buildDumperOptions(int streamWriteFeatures, int yamlFeatures,
SpecVersion version)
{
DumperOptions opt = new DumperOptions();
DumpSettingsBuilder opt = new DumpSettingsBuilder();
// would we want canonical?
if (Feature.CANONICAL_OUTPUT.enabledIn(_formatWriteFeatures)) {
opt.setCanonical(true);
Expand All @@ -260,11 +280,7 @@ protected DumperOptions buildDumperOptions(int streamWriteFeatures, int yamlFeat
opt.setIndicatorIndent(1);
opt.setIndent(2);
}
// 14-May-2018: [dataformats-text#84] allow use of platform linefeed
if (Feature.USE_PLATFORM_LINE_BREAKS.enabledIn(_formatWriteFeatures)) {
opt.setLineBreak(DumperOptions.LineBreak.getPlatformLineBreak());
}
return opt;
return opt.build();
}

/*
Expand Down Expand Up @@ -425,8 +441,8 @@ 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));
_emitter.emit(new DocumentEndEvent( false));
_emitter.emit(new StreamEndEvent());
super.close();

/* 25-Nov-2008, tatus: As per [JACKSON-16] we are not to call close()
Expand Down Expand Up @@ -460,12 +476,12 @@ public final void writeStartArray() throws IOException
FlowStyle style = _outputOptions.getDefaultFlowStyle();
String yamlTag = _typeId;
boolean implicit = (yamlTag == null);
String anchor = _objectId;
if (anchor != null) {
Optional<Anchor> anchor = Optional.ofNullable(_objectId).map(s -> new Anchor(s));
if (anchor.isPresent()) {
_objectId = null;
}
_emitter.emit(new SequenceStartEvent(anchor, yamlTag,
implicit, null, null, style));
_emitter.emit(new SequenceStartEvent(anchor, Optional.ofNullable(yamlTag),
implicit, style));
}

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

@Override
Expand All @@ -488,12 +504,11 @@ public final void writeStartObject() throws IOException
FlowStyle style = _outputOptions.getDefaultFlowStyle();
String yamlTag = _typeId;
boolean implicit = (yamlTag == null);
String anchor = _objectId;
if (anchor != null) {
Optional<Anchor> anchor = Optional.ofNullable(_objectId).map(s -> new Anchor(s));
if (anchor.isPresent()) {
_objectId = null;
}
_emitter.emit(new MappingStartEvent(anchor, yamlTag,
implicit, null, null, style));
_emitter.emit(new MappingStartEvent(anchor, Optional.ofNullable(yamlTag), implicit, style));
}

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

/*
Expand All @@ -522,7 +537,7 @@ public void writeString(String text) throws IOException,JsonGenerationException
return;
}
_verifyValueWrite("write String value");
DumperOptions.ScalarStyle style = STYLE_QUOTED;
ScalarStyle style = STYLE_QUOTED;
if (Feature.MINIMIZE_QUOTES.enabledIn(_formatWriteFeatures) && !isBooleanContent(text)) {
// If this string could be interpreted as a number, it must be quoted.
if (Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS.enabledIn(_formatWriteFeatures)
Expand Down Expand Up @@ -752,7 +767,7 @@ public void writeObjectRef(Object id)
throws IOException
{
_verifyValueWrite("write Object reference");
AliasEvent evt = new AliasEvent(String.valueOf(id), null, null);
AliasEvent evt = new AliasEvent(Optional.of(String.valueOf(id)).map(s -> new Anchor(s)));
_emitter.emit(evt);
}

Expand Down Expand Up @@ -797,7 +812,7 @@ protected void _releaseBuffers() {
// ... and sometimes we specifically DO want explicit tag:
private final static ImplicitTuple EXPLICIT_TAGS = new ImplicitTuple(false, false);

protected void _writeScalar(String value, String type, DumperOptions.ScalarStyle style) throws IOException
protected void _writeScalar(String value, String type, ScalarStyle style) throws IOException
{
_emitter.emit(_scalarEvent(value, style));
}
Expand All @@ -812,27 +827,26 @@ private void _writeScalarBinary(Base64Variant b64variant,
}
final String lf = _lf();
String encoded = b64variant.encode(data, false, lf);
_emitter.emit(new ScalarEvent(null, TAG_BINARY, EXPLICIT_TAGS, encoded,
null, null, STYLE_BASE64));
_emitter.emit(new ScalarEvent(Optional.empty(), Optional.ofNullable(TAG_BINARY), EXPLICIT_TAGS, encoded, STYLE_BASE64));

}

protected ScalarEvent _scalarEvent(String value, DumperOptions.ScalarStyle style)
protected ScalarEvent _scalarEvent(String value, ScalarStyle style)
{
String yamlTag = _typeId;
if (yamlTag != null) {
_typeId = null;
}
String anchor = _objectId;
if (anchor != null) {
Optional<Anchor> anchor = Optional.ofNullable(_objectId).map(s -> new Anchor(s));
if (anchor.isPresent()) {
_objectId = null;
}
// 29-Nov-2017, tatu: Not 100% sure why we don't force explicit tags for
// type id, but trying to do so seems to double up tag output...
return new ScalarEvent(anchor, yamlTag, NO_TAGS, value,
null, null, style);
return new ScalarEvent(anchor, Optional.ofNullable(yamlTag), NO_TAGS, value, style);
}

protected String _lf() {
return _outputOptions.getLineBreak().getString();
return _outputOptions.getBestLineBreak();
}
}
Loading