Skip to content

Add JsonFactory.Feature.CHARSET_DETECTION to disable charset detection (default to UTF-8) #921

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
merged 1 commit into from
Feb 21, 2023
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
12 changes: 11 additions & 1 deletion src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,17 @@ public enum Feature
*
* @since 2.6
*/
USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING(true)
USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING(true),

/**
* Feature to control charset detection for byte-based inputs ({@code byte[]}, {@link InputStream}...). When
* this feature is enabled (the default), the factory will allow UTF-16 and UTF-32 inputs and try to detect
* them, as specified by RFC 4627. When this feature is disabled the factory will assume UTF-8, as specified
* by RFC 8259.
*
* @since 2.15
*/
CHARSET_DETECTION(true),

;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public JsonParser constructParser(int parserFeatures, ObjectCodec codec,
int factoryFeatures) throws IOException
{
int prevInputPtr = _inputPtr;
JsonEncoding enc = detectEncoding();
JsonEncoding enc = JsonFactory.Feature.CHARSET_DETECTION.enabledIn(factoryFeatures) ? detectEncoding() : JsonEncoding.UTF8;
int bytesProcessed = _inputPtr - prevInputPtr;

if (enc == JsonEncoding.UTF8) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.core.read;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.testsupport.MockDataInput;
import com.fasterxml.jackson.core.util.JsonParserDelegate;

Expand Down Expand Up @@ -703,6 +704,20 @@ private void _testHandlingOfInvalidSpaceFromResource(boolean useStream) throws E
p.close();
}

public void testInvalidUtf8ValidUtf16() throws IOException {
JsonFactory factory = new JsonFactoryBuilder()
.disable(JsonFactory.Feature.CHARSET_DETECTION)
.build();
JsonParser parser = factory.createParser(new byte[]{0x22, 0x00, 0x22, 0x5b, 0x22, 0x00});
try {
//noinspection StatementWithEmptyBody
while (parser.nextToken() != null) {}
fail("Should have failed");
} catch (JsonParseException e) {
verifyException(e, "unquoted character");
}
}

/*
/**********************************************************
/* Helper methods
Expand Down