Skip to content

Commit a2b21c4

Browse files
authored
Refactor code so it doesn't hit mono aot limitation (#118698)
1 parent 108fa78 commit a2b21c4

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/IReadBufferState.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public abstract ValueTask<TReadBufferState> ReadAsync(
2626

2727
public abstract void Advance(long bytesConsumed);
2828

29-
public abstract Utf8JsonReader GetReader(JsonReaderState jsonReaderState);
29+
// This would normally be implemented as returning a Utf8JsonReader, but this pattern hits a limitation
30+
// in mono aot that is not trivial to fix. For now use the alternative pattern of returning via an out
31+
// argument. Tracking issue: https://github.com/dotnet/runtime/issues/118697
32+
public abstract void GetReader(JsonReaderState jsonReaderState, out Utf8JsonReader reader);
3033
}
3134
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.ReadHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ internal bool ContinueDeserialize<TReadBufferState, TStream>(
136136
out T? value)
137137
where TReadBufferState : struct, IReadBufferState<TReadBufferState, TStream>
138138
{
139-
Utf8JsonReader reader = bufferState.GetReader(jsonReaderState);
139+
bufferState.GetReader(jsonReaderState, out Utf8JsonReader reader);
140140

141141
try
142142
{

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/PipeReadBufferState.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ public async ValueTask<PipeReadBufferState> ReadAsync(PipeReader utf8Json, Cance
7575

7676
public void Read(PipeReader utf8Json) => throw new NotImplementedException();
7777

78-
public Utf8JsonReader GetReader(JsonReaderState jsonReaderState)
78+
public void GetReader(JsonReaderState jsonReaderState, out Utf8JsonReader reader)
7979
{
8080
if (_sequence.IsSingleSegment)
8181
{
82-
return new Utf8JsonReader(
82+
reader = new Utf8JsonReader(
8383
#if NET
8484
_sequence.FirstSpan,
8585
#else
@@ -89,7 +89,7 @@ public Utf8JsonReader GetReader(JsonReaderState jsonReaderState)
8989
}
9090
else
9191
{
92-
return new Utf8JsonReader(_sequence, IsFinalBlock, jsonReaderState);
92+
reader = new Utf8JsonReader(_sequence, IsFinalBlock, jsonReaderState);
9393
}
9494
}
9595

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/StreamReadBufferState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ public void Advance(long bytesConsumed)
157157
_offset = 0;
158158
}
159159

160-
public Utf8JsonReader GetReader(JsonReaderState jsonReaderState)
160+
public void GetReader(JsonReaderState jsonReaderState, out Utf8JsonReader reader)
161161
{
162-
return new Utf8JsonReader(
162+
reader = new Utf8JsonReader(
163163
_buffer.AsSpan(_offset, _count),
164164
IsFinalBlock,
165165
jsonReaderState);

0 commit comments

Comments
 (0)