Skip to content

JsonDeserializer.Deserialize throws "invalid start of a value" on a valid JSON #78611

@ivonin

Description

@ivonin

Description

JsonDeserializer.Deserialize(Stream) method throws a JsonException ('0x93' is an invalid start of a value) when I try to deserialize JSON from file.

The file is a valid JSON encoded with UTF8 (with BOM). The exact same code works as intended with the exact same file on net6.0.

Reproduction Steps

using System.Text.Json;

using var stream = File.OpenRead(@"sample.txt");
var res = JsonSerializer.Deserialize<Dictionary<string, string>>(stream);

sample.txt

I was unable to produce a more concise fragment of the problematic file as it seems to randomly stop causing an error when I try to remove some fields.

Expected behavior

JSON object inside a file is deserialized as a Dictionary.

Actual behavior

 ---> System.Text.Json.JsonReaderException: '0x93' is an invalid start of a value. LineNumber: 132 | BytePositionInLine: 64.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker)
   at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, JsonReaderException ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ContinueDeserialize[TValue](ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.ReadFromStream[TValue](Stream utf8Json, JsonTypeInfo jsonTypeInfo)

Regression?

The code works fine when targeting net6.0, only fails when targeting net7.0.

Known Workarounds

No response

Configuration

dotnet sdk 7.0.100
macOS 12.6.1, Apple M1 Pro
ARM64

From the reports of my colleagues this behaviour doesn't seem to occur on Windows, but I personally haven't checked.

Other information

The JSON value that seems to cause the problem starts with a cyrillic "Г" character encoded in UTF8 as 0xD0 0x93. The exception message mentions 0x93 as the start of a value, so the problem might be somehow UTF8-specific, something to do with multi-byte codepoints handling. It's just a guess though.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions