Skip to content

Commit c94cb96

Browse files
Allow passing ParserImpl by a subclass or overwrite the events (#482)
Makes it possible to overwrite the getEvent calls on the parser to add additional functionality such as filtering, modifying and inserting events. Alternatively provide a constructor to allow using subclasses of the snake yaml ParserImpl. Note: didn't replace the internal parserImpl-field since other subclasses might already use it otherwise would make sense to use the Parser-interface instead.
1 parent 1c338a3 commit c94cb96

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

release-notes/CREDITS-2.x

+6
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,9 @@ David Pujol (@PujolDavid)
271271
* Contributed #469: (csv) Allow CSV to differentiate between `null` and empty
272272
fields (foo,,bar vs. foo,"",bar)
273273
(2.18.0)
274+
275+
Heiko Boettger (@HeikoBoettger)
276+
277+
* Contributed #482: (yaml) Allow passing `ParserImpl` by a subclass or overwrite the events
278+
(2.18.0)
279+

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Active Maintainers:
2121
#469: (csv) Allow CSV to differentiate between `null` and empty
2222
fields (foo,,bar vs. foo,"",bar)
2323
(contributed by David P)
24+
#482: (yaml) Allow passing `ParserImpl` by a subclass or overwrite the events
25+
(contributed by Heiko B)
2426

2527
2.17.2 (05-Jul-2024)
2628

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

+35-9
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ private Feature(boolean defaultState) {
179179
/**********************************************************************
180180
*/
181181

182-
183182
/**
184183
* @deprecated since 2.14, use other constructor
185184
*/
@@ -192,16 +191,28 @@ public YAMLParser(IOContext ctxt, BufferRecycler br,
192191
}
193192

194193
public YAMLParser(IOContext ctxt, int parserFeatures, int formatFeatures,
195-
LoaderOptions loaderOptions, ObjectCodec codec, Reader reader)
194+
LoaderOptions loaderOptions, ObjectCodec codec, Reader reader)
195+
{
196+
this(ctxt, parserFeatures, formatFeatures, codec, reader,
197+
new ParserImpl(new StreamReader(reader),
198+
(loaderOptions == null) ? new LoaderOptions() : loaderOptions));
199+
}
200+
201+
/**
202+
* Constructor to overload by custom parser sub-classes that want to replace
203+
* {@link ParserImpl} passed.
204+
*
205+
* @since 2.18
206+
*/
207+
protected YAMLParser(IOContext ctxt, int parserFeatures, int formatFeatures,
208+
ObjectCodec codec, Reader reader,
209+
ParserImpl yamlParser)
196210
{
197211
super(ctxt, parserFeatures);
198212
_objectCodec = codec;
199213
_formatFeatures = formatFeatures;
200214
_reader = reader;
201-
if (loaderOptions == null) {
202-
loaderOptions = new LoaderOptions();
203-
}
204-
_yamlParser = new ParserImpl(new StreamReader(reader), loaderOptions);
215+
_yamlParser = yamlParser;
205216
_cfgEmptyStringsToNull = Feature.EMPTY_STRING_AS_NULL.enabledIn(formatFeatures);
206217
}
207218

@@ -294,8 +305,10 @@ protected void _closeInput() throws IOException {
294305
* Reader (granted, we only do that for UTF-32...) this
295306
* means that buffer recycling won't work correctly.
296307
*/
297-
if (_ioContext.isResourceManaged() || isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)) {
298-
_reader.close();
308+
if (_reader != null) {
309+
if (_ioContext.isResourceManaged() || isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)) {
310+
_reader.close();
311+
}
299312
}
300313
}
301314

@@ -434,7 +447,7 @@ public JsonToken nextToken() throws IOException
434447
while (true) {
435448
Event evt;
436449
try {
437-
evt = _yamlParser.getEvent();
450+
evt = getEvent();
438451
} catch (org.yaml.snakeyaml.error.YAMLException e) {
439452
if (e instanceof org.yaml.snakeyaml.error.MarkedYAMLException) {
440453
throw com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException.from
@@ -564,6 +577,19 @@ public JsonToken nextToken() throws IOException
564577
}
565578
}
566579

580+
/**
581+
* Since the parserImpl cannot be replaced allow subclasses to at least be able to
582+
* influence the events being consumed.
583+
*
584+
* A particular use case is working around the lack of anchor and alias support to
585+
* emit additional events.
586+
*
587+
* @since 2.18
588+
*/
589+
protected Event getEvent() {
590+
return _yamlParser.getEvent();
591+
}
592+
567593
protected JsonToken _decodeScalar(ScalarEvent scalar) throws IOException
568594
{
569595
String value = scalar.getValue();

0 commit comments

Comments
 (0)