Skip to content

Allow passing ParserImpl by a subclass or overwrite the events #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ private Feature(boolean defaultState) {
/**********************************************************************
*/


/**
* @deprecated since 2.14, use other constructor
*/
Expand All @@ -192,16 +191,28 @@ 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, 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, Reader reader,
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);
}

Expand Down Expand Up @@ -294,8 +305,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();
}
}
}

Expand Down Expand Up @@ -434,7 +447,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
Expand Down Expand Up @@ -564,6 +577,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();
Expand Down