DCS: Add more formal BOM-detection to Json encoding stream wrappers.#130635
Open
StephenMolloy wants to merge 5 commits into
Open
DCS: Add more formal BOM-detection to Json encoding stream wrappers.#130635StephenMolloy wants to merge 5 commits into
StephenMolloy wants to merge 5 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates JsonEncodingStreamWrapper to centralize JSON encoding detection (including BOM handling) across both buffer- and stream-based read paths, and adds regression tests ensuring BOM-prefixed JSON payloads deserialize correctly.
Changes:
- Added a shared
DetectEncoding(ReadOnlySpan<byte>, out int bomLength)helper and used it from both buffer and stream paths. - Updated buffer and stream reading logic to skip BOM bytes so only the JSON payload is processed.
- Added new
[Theory]tests covering UTF-8/UTF-16LE/UTF-16BE BOM scenarios for both stream and buffer deserialization.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonEncodingStreamWrapper.cs | Centralizes encoding detection and skips BOMs for both buffer and stream read paths. |
| src/libraries/System.Runtime.Serialization.Json/tests/DataContractJsonSerializer.cs | Adds BOM-focused theory tests and a helper to construct BOM-prefixed JSON byte payloads. |
…ort input ReadEncoding used ReadAtLeast, which loops until the requested minimum number of bytes is accumulated and therefore blocks a consumer waiting for bytes an untrusted producer may never send. Replace it with a single Stream.Read that performs one underlying read and decides the encoding from however many bytes were available, defaulting to UTF-8 when there are too few. This recognizes every byte order mark whenever the leading bytes are present while allowing valid short documents (for example a single-byte JSON value) to proceed without waiting for a fixed minimum. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request improves the handling of JSON byte order marks (BOM) in the DataContractJsonSerializer by centralizing and enhancing encoding detection logic, and adds comprehensive tests to ensure correct behavior when processing streams and buffers with BOMs.
Encoding detection improvements:
DetectEncodingmethod to reliably detect the encoding of a JSON document by examining its leading bytes for BOMs or inferring from initial bytes when no BOM is present, consolidating logic previously duplicated across code paths.JsonEncodingStreamWrapperto use the newDetectEncodingmethod, ensuring BOMs are correctly skipped and only the actual JSON payload is processed. [1] [2]Test coverage enhancements:
Fixes #80160