Skip to content

Commit

Permalink
Add JsonFactory.Feature to disable charset detection (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
yawkat committed Feb 21, 2023
1 parent 6eddff3 commit 36cd882
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
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

0 comments on commit 36cd882

Please sign in to comment.