From 46aeb007fc974cabf2f990c69fcb85c372926fd3 Mon Sep 17 00:00:00 2001 From: HeikoBoettger Date: Fri, 19 Jul 2024 14:20:03 +0200 Subject: [PATCH 1/5] Allow to replace the ParserImpl by a subclass or overwrite the events 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. --- .../jackson/dataformat/yaml/YAMLParser.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java index f85fa310..2147af97 100644 --- a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java +++ b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java @@ -190,18 +190,22 @@ public YAMLParser(IOContext ctxt, BufferRecycler br, { this(ctxt, parserFeatures, formatFeatures, null, codec, reader); } - + public YAMLParser(IOContext ctxt, int parserFeatures, int formatFeatures, LoaderOptions loaderOptions, ObjectCodec codec, Reader reader) + { + this(ctxt, parserFeatures, formatFeatures, codec, + new ParserImpl(new StreamReader(reader), (loaderOptions == null) ? new LoaderOptions() : loaderOptions)) + } + + protected YAMLParser(IOContext ctxt, int parserFeatures, int formatFeatures, + ObjectCodec codec, ParserImpl yamlParser) { super(ctxt, parserFeatures); _objectCodec = codec; _formatFeatures = formatFeatures; _reader = reader; - if (loaderOptions == null) { - loaderOptions = new LoaderOptions(); - } - _yamlParser = new ParserImpl(new StreamReader(reader), loaderOptions); + _yamlParser = yamlParser; _cfgEmptyStringsToNull = Feature.EMPTY_STRING_AS_NULL.enabledIn(formatFeatures); } @@ -415,12 +419,22 @@ protected JsonLocation _locationFor(Mark m) // Note: SHOULD override 'getTokenLineNr', 'getTokenColumnNr', but those are final in 2.0 + /** + * Since the parserImpl cannot be replaced allow subclasses to at least be able to + * influence the events being consumed. + * + * A particular use case is working around the lack of anchor and alias support to + * emit additional events. + */ + protected Event getEvent() { + return _yamlParser.getEvent(); + } + /* /********************************************************** /* Parsing /********************************************************** */ - @SuppressWarnings("deprecation") @Override public JsonToken nextToken() throws IOException From c7ee5eec03b3f52a0c62285f7f97e4af428f9d2a Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 19 Jul 2024 10:55:13 -0700 Subject: [PATCH 2/5] Fix compilation issues --- .../jackson/dataformat/yaml/YAMLParser.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java index 2147af97..ace9dc98 100644 --- a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java +++ b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java @@ -192,14 +192,22 @@ public YAMLParser(IOContext ctxt, BufferRecycler br, } public YAMLParser(IOContext ctxt, int parserFeatures, int formatFeatures, - LoaderOptions loaderOptions, ObjectCodec codec, Reader reader) + LoaderOptions loaderOptions, ObjectCodec codec, Reader reader) { - this(ctxt, parserFeatures, formatFeatures, codec, - new ParserImpl(new StreamReader(reader), (loaderOptions == null) ? new LoaderOptions() : loaderOptions)) + this(ctxt, parserFeatures, formatFeatures, codec, reader, + new ParserImpl(new StreamReader(reader), + (loaderOptions == null) ? new LoaderOptions() : loaderOptions)); } - + + /** + * Constructor to overload by custom parser sub-classes that want to replace + * {@link ParserImpl} passed. + * + * @since 2.18 + */ protected YAMLParser(IOContext ctxt, int parserFeatures, int formatFeatures, - ObjectCodec codec, ParserImpl yamlParser) + ObjectCodec codec, Reader reader, + ParserImpl yamlParser) { super(ctxt, parserFeatures); _objectCodec = codec; @@ -298,8 +306,10 @@ protected void _closeInput() throws IOException { * Reader (granted, we only do that for UTF-32...) this * means that buffer recycling won't work correctly. */ - if (_ioContext.isResourceManaged() || isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)) { - _reader.close(); + if (_reader != null) { + if (_ioContext.isResourceManaged() || isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)) { + _reader.close(); + } } } @@ -435,6 +445,7 @@ protected Event getEvent() { /* Parsing /********************************************************** */ + @SuppressWarnings("deprecation") @Override public JsonToken nextToken() throws IOException From 8562bf4b88db8b92ab1adf8130d60dc77ae61881 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 19 Jul 2024 10:56:28 -0700 Subject: [PATCH 3/5] Add actual overload of `getEvent()` access --- .../jackson/dataformat/yaml/YAMLParser.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java index ace9dc98..125da83b 100644 --- a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java +++ b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java @@ -429,17 +429,6 @@ protected JsonLocation _locationFor(Mark m) // Note: SHOULD override 'getTokenLineNr', 'getTokenColumnNr', but those are final in 2.0 - /** - * Since the parserImpl cannot be replaced allow subclasses to at least be able to - * influence the events being consumed. - * - * A particular use case is working around the lack of anchor and alias support to - * emit additional events. - */ - protected Event getEvent() { - return _yamlParser.getEvent(); - } - /* /********************************************************** /* Parsing @@ -459,7 +448,7 @@ public JsonToken nextToken() throws IOException while (true) { Event evt; try { - evt = _yamlParser.getEvent(); + evt = getEvent(); } catch (org.yaml.snakeyaml.error.YAMLException e) { if (e instanceof org.yaml.snakeyaml.error.MarkedYAMLException) { throw com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException.from @@ -589,6 +578,19 @@ public JsonToken nextToken() throws IOException } } + /** + * Since the parserImpl cannot be replaced allow subclasses to at least be able to + * influence the events being consumed. + * + * A particular use case is working around the lack of anchor and alias support to + * emit additional events. + * + * @since 2.18 + */ + protected Event getEvent() { + return _yamlParser.getEvent(); + } + protected JsonToken _decodeScalar(ScalarEvent scalar) throws IOException { String value = scalar.getValue(); From e1a31dd20ee4578b1b85982444de04a891d91b10 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 19 Jul 2024 10:57:18 -0700 Subject: [PATCH 4/5] ... --- .../java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java index 125da83b..eed7a159 100644 --- a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java +++ b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java @@ -179,7 +179,6 @@ private Feature(boolean defaultState) { /********************************************************************** */ - /** * @deprecated since 2.14, use other constructor */ @@ -190,7 +189,7 @@ public YAMLParser(IOContext ctxt, BufferRecycler br, { this(ctxt, parserFeatures, formatFeatures, null, codec, reader); } - + public YAMLParser(IOContext ctxt, int parserFeatures, int formatFeatures, LoaderOptions loaderOptions, ObjectCodec codec, Reader reader) { From 128c1a4ab4c6b63a5ee40a036ea08cc1489401ac Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 20 Jul 2024 17:23:33 -0700 Subject: [PATCH 5/5] Update release notes --- release-notes/CREDITS-2.x | 6 ++++++ release-notes/VERSION-2.x | 2 ++ 2 files changed, 8 insertions(+) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 61d57c37..6de6aae9 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -271,3 +271,9 @@ David Pujol (@PujolDavid) * Contributed #469: (csv) Allow CSV to differentiate between `null` and empty fields (foo,,bar vs. foo,"",bar) (2.18.0) + +Heiko Boettger (@HeikoBoettger) + +* Contributed #482: (yaml) Allow passing `ParserImpl` by a subclass or overwrite the events + (2.18.0) + diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index b3ef1a69..727c5415 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -21,6 +21,8 @@ Active Maintainers: #469: (csv) Allow CSV to differentiate between `null` and empty fields (foo,,bar vs. foo,"",bar) (contributed by David P) +#482: (yaml) Allow passing `ParserImpl` by a subclass or overwrite the events + (contributed by Heiko B) 2.17.2 (05-Jul-2024)