Skip to content

Commit c258dac

Browse files
authored
Merge pull request #106 from asomov/snakeyaml-engine
SnakeYAML Engine
2 parents d9298f8 + 3f7c4af commit c258dac

File tree

10 files changed

+261
-210
lines changed

10 files changed

+261
-210
lines changed

yaml/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
</dependency>
2929

3030
<dependency>
31-
<groupId>org.yaml</groupId>
32-
<artifactId>snakeyaml</artifactId>
33-
<version>1.23</version>
31+
<groupId>org.snakeyaml</groupId>
32+
<artifactId>snakeyaml-engine</artifactId>
33+
<version>1.0</version>
3434
</dependency>
3535

3636
<!-- and for testing need annotations -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fasterxml.jackson.dataformat.yaml;
2+
3+
import java.io.IOException;
4+
import java.io.Writer;
5+
6+
import org.snakeyaml.engine.v1.api.StreamDataWriter;
7+
8+
public class WriterWrapper implements StreamDataWriter {
9+
private final Writer _writer;
10+
11+
public WriterWrapper(Writer _writer) {
12+
this._writer = _writer;
13+
}
14+
15+
@Override
16+
public void flush() {
17+
try {
18+
_writer.flush();
19+
} catch (IOException e) {
20+
throw new RuntimeException(e);
21+
}
22+
}
23+
24+
@Override
25+
public void write(String str) {
26+
try {
27+
_writer.write(str);
28+
} catch (IOException e) {
29+
throw new RuntimeException(e);
30+
}
31+
}
32+
33+
@Override
34+
public void write(String str, int off, int len) {
35+
try {
36+
_writer.write(str, off, len);
37+
} catch (IOException e) {
38+
throw new RuntimeException(e);
39+
}
40+
}
41+
}

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.io.*;
44
import java.nio.charset.Charset;
55

6-
import org.yaml.snakeyaml.DumperOptions;
6+
import org.snakeyaml.engine.v1.common.SpecVersion;
77

88
import com.fasterxml.jackson.core.*;
99
import com.fasterxml.jackson.core.base.TextualTSFactory;
@@ -42,7 +42,7 @@ public class YAMLFactory
4242
/**********************************************************************
4343
*/
4444

45-
protected DumperOptions.Version _version; // enum, is serializable
45+
protected SpecVersion _version; // enum, is serializable
4646

4747
/**
4848
* Default constructor used to create factory instances.

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,28 @@
66
import java.util.Arrays;
77
import java.util.Collections;
88
import java.util.Map;
9+
import java.util.Optional;
910
import java.util.regex.Pattern;
1011

11-
import org.yaml.snakeyaml.DumperOptions;
12-
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
13-
import org.yaml.snakeyaml.emitter.Emitter;
14-
import org.yaml.snakeyaml.events.*;
15-
import org.yaml.snakeyaml.nodes.Tag;
12+
import org.snakeyaml.engine.v1.api.DumpSettings;
13+
import org.snakeyaml.engine.v1.api.DumpSettingsBuilder;
14+
import org.snakeyaml.engine.v1.common.Anchor;
15+
import org.snakeyaml.engine.v1.common.FlowStyle;
16+
import org.snakeyaml.engine.v1.common.ScalarStyle;
17+
import org.snakeyaml.engine.v1.common.SpecVersion;
18+
import org.snakeyaml.engine.v1.emitter.Emitter;
19+
import org.snakeyaml.engine.v1.events.AliasEvent;
20+
import org.snakeyaml.engine.v1.events.DocumentEndEvent;
21+
import org.snakeyaml.engine.v1.events.DocumentStartEvent;
22+
import org.snakeyaml.engine.v1.events.ImplicitTuple;
23+
import org.snakeyaml.engine.v1.events.MappingEndEvent;
24+
import org.snakeyaml.engine.v1.events.MappingStartEvent;
25+
import org.snakeyaml.engine.v1.events.ScalarEvent;
26+
import org.snakeyaml.engine.v1.events.SequenceEndEvent;
27+
import org.snakeyaml.engine.v1.events.SequenceStartEvent;
28+
import org.snakeyaml.engine.v1.events.StreamEndEvent;
29+
import org.snakeyaml.engine.v1.events.StreamStartEvent;
30+
import org.snakeyaml.engine.v1.nodes.Tag;
1631

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

103123
/**
@@ -168,23 +188,23 @@ private Feature(boolean defaultState) {
168188

169189
protected Writer _writer;
170190

171-
protected DumperOptions _outputOptions;
191+
protected DumpSettings _outputOptions;
172192

173193
// for field names, leave out quotes
174-
private final static DumperOptions.ScalarStyle STYLE_NAME = DumperOptions.ScalarStyle.PLAIN;
194+
private final static ScalarStyle STYLE_NAME = ScalarStyle.PLAIN;
175195

176196
// numbers, booleans, should use implicit
177-
private final static DumperOptions.ScalarStyle STYLE_SCALAR = DumperOptions.ScalarStyle.PLAIN;
197+
private final static ScalarStyle STYLE_SCALAR = ScalarStyle.PLAIN;
178198
// Strings quoted for fun
179-
private final static DumperOptions.ScalarStyle STYLE_QUOTED = DumperOptions.ScalarStyle.DOUBLE_QUOTED;
199+
private final static ScalarStyle STYLE_QUOTED = ScalarStyle.DOUBLE_QUOTED;
180200
// Strings in literal (block) style
181-
private final static DumperOptions.ScalarStyle STYLE_LITERAL = DumperOptions.ScalarStyle.LITERAL;
201+
private final static ScalarStyle STYLE_LITERAL = ScalarStyle.LITERAL;
182202

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

187-
private final static DumperOptions.ScalarStyle STYLE_PLAIN = DumperOptions.ScalarStyle.PLAIN;
207+
private final static ScalarStyle STYLE_PLAIN = ScalarStyle.PLAIN;
188208

189209
/*
190210
/**********************************************************************
@@ -215,7 +235,7 @@ private Feature(boolean defaultState) {
215235
public YAMLGenerator(ObjectWriteContext writeContext, IOContext ioCtxt,
216236
int streamWriteFeatures, int yamlFeatures,
217237
Writer out,
218-
org.yaml.snakeyaml.DumperOptions.Version version)
238+
SpecVersion version)
219239
throws IOException
220240
{
221241
super(writeContext, streamWriteFeatures);
@@ -225,22 +245,22 @@ public YAMLGenerator(ObjectWriteContext writeContext, IOContext ioCtxt,
225245

226246
_outputOptions = buildDumperOptions(streamWriteFeatures, yamlFeatures, version);
227247

228-
_emitter = new Emitter(_writer, _outputOptions);
248+
_emitter = new Emitter( _outputOptions, new WriterWrapper(_writer));
229249
// should we start output now, or try to defer?
230-
_emitter.emit(new StreamStartEvent(null, null));
250+
_emitter.emit(new StreamStartEvent());
231251
Map<String,String> noTags = Collections.emptyMap();
232252

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

235-
_emitter.emit(new DocumentStartEvent(null, null, startMarker,
236-
version, // for 1.10 was: ((version == null) ? null : version.getArray()),
255+
_emitter.emit(new DocumentStartEvent(startMarker, Optional.empty(),
256+
// for 1.10 was: ((version == null) ? null : version.getArray()),
237257
noTags));
238258
}
239259

240-
protected DumperOptions buildDumperOptions(int streamWriteFeatures, int yamlFeatures,
241-
org.yaml.snakeyaml.DumperOptions.Version version)
260+
protected DumpSettings buildDumperOptions(int streamWriteFeatures, int yamlFeatures,
261+
SpecVersion version)
242262
{
243-
DumperOptions opt = new DumperOptions();
263+
DumpSettingsBuilder opt = new DumpSettingsBuilder();
244264
// would we want canonical?
245265
if (Feature.CANONICAL_OUTPUT.enabledIn(_formatWriteFeatures)) {
246266
opt.setCanonical(true);
@@ -260,11 +280,7 @@ protected DumperOptions buildDumperOptions(int streamWriteFeatures, int yamlFeat
260280
opt.setIndicatorIndent(1);
261281
opt.setIndent(2);
262282
}
263-
// 14-May-2018: [dataformats-text#84] allow use of platform linefeed
264-
if (Feature.USE_PLATFORM_LINE_BREAKS.enabledIn(_formatWriteFeatures)) {
265-
opt.setLineBreak(DumperOptions.LineBreak.getPlatformLineBreak());
266-
}
267-
return opt;
283+
return opt.build();
268284
}
269285

270286
/*
@@ -425,8 +441,8 @@ public final void flush() throws IOException
425441
public void close() throws IOException
426442
{
427443
if (!isClosed()) {
428-
_emitter.emit(new DocumentEndEvent(null, null, false));
429-
_emitter.emit(new StreamEndEvent(null, null));
444+
_emitter.emit(new DocumentEndEvent( false));
445+
_emitter.emit(new StreamEndEvent());
430446
super.close();
431447

432448
/* 25-Nov-2008, tatus: As per [JACKSON-16] we are not to call close()
@@ -460,12 +476,12 @@ public final void writeStartArray() throws IOException
460476
FlowStyle style = _outputOptions.getDefaultFlowStyle();
461477
String yamlTag = _typeId;
462478
boolean implicit = (yamlTag == null);
463-
String anchor = _objectId;
464-
if (anchor != null) {
479+
Optional<Anchor> anchor = Optional.ofNullable(_objectId).map(s -> new Anchor(s));
480+
if (anchor.isPresent()) {
465481
_objectId = null;
466482
}
467-
_emitter.emit(new SequenceStartEvent(anchor, yamlTag,
468-
implicit, null, null, style));
483+
_emitter.emit(new SequenceStartEvent(anchor, Optional.ofNullable(yamlTag),
484+
implicit, style));
469485
}
470486

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

483499
@Override
@@ -488,12 +504,11 @@ public final void writeStartObject() throws IOException
488504
FlowStyle style = _outputOptions.getDefaultFlowStyle();
489505
String yamlTag = _typeId;
490506
boolean implicit = (yamlTag == null);
491-
String anchor = _objectId;
492-
if (anchor != null) {
507+
Optional<Anchor> anchor = Optional.ofNullable(_objectId).map(s -> new Anchor(s));
508+
if (anchor.isPresent()) {
493509
_objectId = null;
494510
}
495-
_emitter.emit(new MappingStartEvent(anchor, yamlTag,
496-
implicit, null, null, style));
511+
_emitter.emit(new MappingStartEvent(anchor, Optional.ofNullable(yamlTag), implicit, style));
497512
}
498513

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

511526
/*
@@ -522,7 +537,7 @@ public void writeString(String text) throws IOException,JsonGenerationException
522537
return;
523538
}
524539
_verifyValueWrite("write String value");
525-
DumperOptions.ScalarStyle style = STYLE_QUOTED;
540+
ScalarStyle style = STYLE_QUOTED;
526541
if (Feature.MINIMIZE_QUOTES.enabledIn(_formatWriteFeatures) && !isBooleanContent(text)) {
527542
// If this string could be interpreted as a number, it must be quoted.
528543
if (Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS.enabledIn(_formatWriteFeatures)
@@ -752,7 +767,7 @@ public void writeObjectRef(Object id)
752767
throws IOException
753768
{
754769
_verifyValueWrite("write Object reference");
755-
AliasEvent evt = new AliasEvent(String.valueOf(id), null, null);
770+
AliasEvent evt = new AliasEvent(Optional.of(String.valueOf(id)).map(s -> new Anchor(s)));
756771
_emitter.emit(evt);
757772
}
758773

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

800-
protected void _writeScalar(String value, String type, DumperOptions.ScalarStyle style) throws IOException
815+
protected void _writeScalar(String value, String type, ScalarStyle style) throws IOException
801816
{
802817
_emitter.emit(_scalarEvent(value, style));
803818
}
@@ -812,27 +827,26 @@ private void _writeScalarBinary(Base64Variant b64variant,
812827
}
813828
final String lf = _lf();
814829
String encoded = b64variant.encode(data, false, lf);
815-
_emitter.emit(new ScalarEvent(null, TAG_BINARY, EXPLICIT_TAGS, encoded,
816-
null, null, STYLE_BASE64));
830+
_emitter.emit(new ScalarEvent(Optional.empty(), Optional.ofNullable(TAG_BINARY), EXPLICIT_TAGS, encoded, STYLE_BASE64));
831+
817832
}
818833

819-
protected ScalarEvent _scalarEvent(String value, DumperOptions.ScalarStyle style)
834+
protected ScalarEvent _scalarEvent(String value, ScalarStyle style)
820835
{
821836
String yamlTag = _typeId;
822837
if (yamlTag != null) {
823838
_typeId = null;
824839
}
825-
String anchor = _objectId;
826-
if (anchor != null) {
840+
Optional<Anchor> anchor = Optional.ofNullable(_objectId).map(s -> new Anchor(s));
841+
if (anchor.isPresent()) {
827842
_objectId = null;
828843
}
829844
// 29-Nov-2017, tatu: Not 100% sure why we don't force explicit tags for
830845
// type id, but trying to do so seems to double up tag output...
831-
return new ScalarEvent(anchor, yamlTag, NO_TAGS, value,
832-
null, null, style);
846+
return new ScalarEvent(anchor, Optional.ofNullable(yamlTag), NO_TAGS, value, style);
833847
}
834848

835849
protected String _lf() {
836-
return _outputOptions.getLineBreak().getString();
850+
return _outputOptions.getBestLineBreak();
837851
}
838852
}

0 commit comments

Comments
 (0)