Repository navigation
Deserialize escaped ULID JSON strings and dictionary keys - #119
Open
dajiaohuang wants to merge 1 commit into
Open
dajiaohuang wants to merge 1 commit into
dajiaohuang wants to merge 1 commit into
Conversation
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.
Problem
The advertised System.Text.Json converter rejects a valid ULID when its JSON string contains an escape. For example,
JsonSerializer.Deserialize<Ulid>("\"\\u00301ARZ3NDEKTSV4RRFFQ69G5FAV\"")throwsJsonException: Ulid invalid: length must be 26, although the decoded string is the ordinary 26-character ULID01ARZ3NDEKTSV4RRFFQ69G5FAV.The converter measures and parses the raw UTF-8 token, not its decoded string. Dictionary keys and multi-segment input have the same problem.
Change
Decode escaped JSON tokens with
Utf8JsonReader.GetString()before checking the decoded length and parsing. Keep the existing byte-span/sequence path for unescaped tokens. The escape detection uses APIs available on the supported .NET 6 target; no new dependency or parsing-policy change is introduced.Four regression cases exercise
JsonSerializer.Deserializefor ordinary values and dictionary keys, using contiguous and segmented input. All use a synthetic ULID.Validation
JsonException, and pass after the change.Windows x64, SDK 10.0.100. The default net7.0 test assemblies ran on .NET 8.0.22 with
DOTNET_ROLL_FORWARD=Majorbecause native .NET 7 is not installed. Existing EOL/dependency/assembly warnings remain. Unity editor, browser execution and big-endian hardware were not tested; this does not change the existing handling of invalid Base32 text.